使用matlab识别实时视频输入中的白盒 [英] identify white boxes in live video feed using matlab

查看:284
本文介绍了使用matlab识别实时视频输入中的白盒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





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


I'm currently using matlab to code a project that uses two relatively big squares to identify a piece of paper in a live video feed. However, the paper can be any colour or have any image on it. I'm using white boxes for coloured images and black for non-coloured. Despite the fact the boxes are showing very clearly in the video I can't figure out how to code so that the algorithm specifically looks just for these boxes and not just the largest two areas of white in the video.

Here is the threshed image showing the clear boxes

And Here is the code thus far.

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

解决方案

I solved your problem for the given image using the Eccentricity parameter. Squares have a low eccentricity. First, I removed the very small areas for which the eccentricity has little meaning.

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

Note that the method is not sensitive to the threshold for small areas, everything between 20 and 3000 will work.

这篇关于使用matlab识别实时视频输入中的白盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆