使用matlab识别实时视频源中的白盒
我目前正在使用matlab编写一个项目,该项目使用两个相对较大的正方形来识别实时视频提要中的一张纸。但是,纸张可以是任何颜色,也可以有任何图像。我用白色框表示彩色图像,黑色框表示非彩色图像。尽管这些盒子在视频中显示得非常清晰,但我不知道如何进行编码,这样算法就只针对这些盒子,而不仅仅是视频中最大的两个白色区域 这是迄今为止的代码使用matlab识别实时视频源中的白盒,matlab,image-processing,Matlab,Image Processing,我目前正在使用matlab编写一个项目,该项目使用两个相对较大的正方形来识别实时视频提要中的一张纸。但是,纸张可以是任何颜色,也可以有任何图像。我用白色框表示彩色图像,黑色框表示非彩色图像。尽管这些盒子在视频中显示得非常清晰,但我不知道如何进行编码,这样算法就只针对这些盒子,而不仅仅是视频中最大的两个白色区域 这是迄今为止的代码 %% Creating Video Player % Create the webcam object. cam = webcam(); % Capture on
%% Creating Video Player
% Create the webcam object.
cam = webcam();
% Capture one frame to get its size.
videoFrame = snapshot(cam);
frameSize = size(videoFrame);
% Create the video player object.
videoPlayer = vision.VideoPlayer('Position', [100 100 [frameSize(2), frameSize(1)]+30]);
%% Loop to Find Poster
runLoop = true;
frameCount = 0;
while runLoop && frameCount < 1000
% Get the next frame.
videoFrame = snapshot(cam);
videoFrameGray = rgb2gray(videoFrame);
frameCount = frameCount + 1;
%% Thresholding
BW = imbinarize(videoFrameGray,.75);
% Craeting structure for Area size and box
rp = regionprops(BW, 'BoundingBox', 'Area');
if length(rp) > 1
% Sorting Struct
[values,ind] = sort([rp.Area],'descend');
% Getting top 2 boxes
bb1 = rp(ind(1)).BoundingBox;
bb2 = rp(ind(2)).BoundingBox;
if bb1(:,3) > 50 && bb2(:,3) > 50 && bb1(:,4) > 50 && bb2(:,4) < 150 && bb1(:,3) < 150 && bb2(:,3) < 150 && bb1(:,4) < 150 && bb2(:,4) <150
% Scan Box Dimensions
bb1BoxHeight = bb1(:,4);
bb1BoxWidth = bb1(:,3);
bb2BoxHeight = bb2(:,4);
bb2BoxWidth = bb2(:,3);
% Box top left points
bb1Position = bb1(:,1);
bb2Position = bb2(:,1);
% Makes sure that bb1 is our top left box not bottom right
if bb2Position < bb1Position
temp = bb1;
bb1 = bb2;
bb2 = temp;
end
% Creating Box the size of out target image
boxPolygonBig = [(bb1(:,1)), (bb1(:,2));... % top-left
(bb2(:,1) + bb2BoxWidth), bb1(:,2);... % top-right
(bb2(:,1) + bb2BoxWidth), (bb2(:,2) + bb2BoxWidth);... % bottom-right
(bb1(:,1)), (bb2(:,2) + bb2BoxHeight);... % bottom-left
bb1(:,1), bb1(:,2)]; % top-left again to close the polygon
% Getting X and Y mins and Max to convert polygon points to
% rectangle
bottomX = min(boxPolygonBig(:,1));
bottomY = min(boxPolygonBig(:,2));
topX = max(boxPolygonBig(:,1));
topY = max(boxPolygonBig(:,2));
height = topY - bottomY;
width = topX - bottomX;
% Display a bounding box around the poster being tracked.
videoFrame = insertShape(BW, 'Rectangle', [bottomX bottomY width height], 'LineWidth', 3);
end
end
%%
% Display the annotated video frame using the video player object.
step(videoPlayer, BW);
% Check whether the video player window has been closed.
runLoop = isOpen(videoPlayer);
end
% Clean up.
clear cam;
release(videoPlayer);
%%创建视频播放器
%创建网络摄像头对象。
cam=网络摄像头();
%捕获一帧以获取其大小。
视频帧=快照(cam);
frameSize=大小(视频帧);
%创建视频播放器对象。
videoPlayer=vision.videoPlayer('Position',[100100[帧大小(2),帧大小(1)]+30]);
%%循环查找海报
runLoop=true;
帧数=0;
运行循环时&&frameCount<1000
%获取下一帧。
视频帧=快照(cam);
videoFrameGray=rgb2gray(视频帧);
帧数=帧数+1;
%%阈值
BW=imbinarize(videoFrameGray,.75);
%适用于面积大小和箱体的板条箱结构
rp=区域属性(BW,‘边界框’、‘区域’);
如果长度(rp)>1
%排序结构
[values,ind]=排序([rp.Area],'down');
%获得前两个盒子
bb1=rp(ind(1))。边界框;
bb2=rp(ind(2))。边界框;
如果bb1(:,3)>50&&bb2(:,3)>50&&bb1(:,4)>50&&bb2(:,4)<150&&bb1(:,3)<150&&bb2(:,3)<150&&bb1(:,4)<150&&bb2(:,4)我使用参数解决了给定图像的问题。正方形的偏心率很低。首先,我删除了偏心率没有什么意义的非常小的区域
videoFrameGray = rgb2gray(imread('FXxLf.png'));
BW = imbinarize(videoFrameGray,.75);
figure
imshow(BW);
% Craeting structure for Area size and box
rp = regionprops(BW, 'BoundingBox', 'Area', 'Eccentricity');
% skip all small areas
rp = rp([rp.Area] > 100);
% Sorting Struct based on eccentricity
[values,ind] = sort([rp.Eccentricity],'ascend');
% Getting top 2 boxes
bb1 = rp(ind(1)).BoundingBox;
bb2 = rp(ind(2)).BoundingBox;
% draw bounding boxes around the two areas
rectangle('Position',floor(bb1),'EdgeColor', [1 0 0])
rectangle('Position',floor(bb2),'EdgeColor', [1 0 0])
请注意,该方法对小区域的阈值不敏感,20到3000之间的所有值都可以使用