使用matlab识别实时视频输入中的白盒

时间:2017-03-12 15:44:55

标签: matlab image-processing

我目前正在使用matlab编写一个项目,该项目使用两个相对较大的方块来识别实时视频输入中的一张纸。但是,纸张可以是任何颜色或其上有任何图像。我使用白色框表示彩色图像,黑色表示非彩色图像。尽管视频中的方框非常清晰,但我无法弄清楚如何编码,以便算法专门针对这些方框而不仅仅是视频中最大的两个白色区域。

Here is the threshed image showing the clear boxes

这是迄今为止的代码。

%% 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);

1 个答案:

答案 0 :(得分:2)

我使用Eccentricity参数解决了给定图像的问题。正方形的偏心率较低。首先,我删除了偏心率没有多大意义的非常小的区域。

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])

enter image description here

请注意,该方法对小区域的阈值不敏感,20到3000之间的所有内容都可以使用。