【问题标题】:Find Specified Color In Each Frame Of Real-time Video在实时视频的每一帧中查找指定颜色
【发布时间】:2014-10-31 10:22:14
【问题描述】:

我是 Matlab 的新手。

我的老板给了我一个任务,我卡在了最后一步。

任务是: 用户应该在实时视频的快照中指定一个矩形,然后我应该检测每个视频帧中该矩形的平均颜色。

第一个用户使用以下代码在视频快照中指定矩形的边界:

    test = getsnapshot(vid);
    imwrite(test,'mytest.png','png');
    testsnap = imread('mytest.png');
    Rectpos=getrect;    

然后我计算每个R , G , B 的平均度数:

    bbx=boundingboxPixels(testsnap,Rectpos(1),Rectpos(2),Rectpos(3),Rectpos(4));
    rRect=mean(bbx(:,:,1));
    gRect=mean(bbx(:,:,2));
    bRect=mean(bbx(:,:,3));

boundingboxPixels 方法是这样的:

function pixel_vals = boundingboxPixels(img, x_init, y_init, x_width, y_width)

    if x_init > size(img,2)
        error('x_init lies outside the bounds of the image.'); end
    if y_init > size(img,1)
        error('y_init lies outside the bounds of the image.'); end

    if y_init+y_width > size(img,1) || x_init+x_width > size(img,2) || ...
            x_init < 1 || y_init < 1
        warning([...
            'Given rectangle partially falls outside image. ',...
            'Resizing rectangle...']);
    end

    x_min   = max(1, uint16(x_init));
    y_min   = max(1, uint16(y_init));
    x_max   = min(size(img,2), x_min+uint16(x_width));
    y_max   = min(size(img,1), y_min+uint16(y_width));
    x_range = x_min : x_max;
    y_range = y_min : y_max;

    Upper = img( x_range, y_min  , :);
    Left  = img(   x_min, y_range, :);
    Right = img(   x_max, y_range, :);
    Lower = img( x_range, y_max  , :);

    pixel_vals = [...
        Upper
        permute(Left, [2 1 3])
        permute(Right, [2 1 3])
        Lower];

end

然后从视频的每一帧中获取RGB颜色的计算平均值和阈值:

tv=getdata(vid,1);//vid is real-time video            
r=tv(:,:,1,1);
g=tv(:,:,2,1);
b=tv(:,:,3,1);

redVal = (r >= rRect-thr) & (r <= rRect+thr);
greenVal = (g >= gRect-thr) & (g <= gRect+thr);
blueVal = (b >= bRect-thr) & (b <= bRect+thr);

现在我应该如何使用redVal , greenVal , blueVal 来检测这种颜色?

【问题讨论】:

  • 只需将它们与 (&) 相加,您就会得到一个逻辑矩阵,其中的矩阵描述了阈值颜色所在的位置。
  • @Steffen 谢谢,但我如何将矩阵位置转换为颜色值矩阵?
  • 我认为 redVal 描述了检测到红色成分的位置,绿色和蓝色相同。通过 and-ing,您将找到所有三个组件都在范围内的位置。既然您指定了要检测的颜色,为什么要使用检测到的位置来获取检测到的颜色?
  • 创建一个蒙版,为您提供颜色范围内的 2D 位置(如 Steffen 所建议):mask = redVal &amp; greenVal &amp; blueVal;。现在你应该能够创建任何你想要的东西,例如everythingBlackButColorWhichIsInRange = cat(3,r.*mask,g.*mask,b.*mask) ;-)

标签: matlab real-time rgb detection


【解决方案1】:

正如 Steffen 所说,通过在数组之间添加 &amp; 解决了问题

【讨论】:

    猜你喜欢
    • 2013-08-08
    • 2017-06-19
    • 2018-05-24
    • 2020-07-20
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多