【问题标题】:How to draw a filled circle on a video frame using matlab如何使用matlab在视频帧上画一个实心圆
【发布时间】:2014-06-06 12:01:27
【问题描述】:

我有一个“倒立摆”视频,我试图找到移动部件的中点。我正在使用计算机视觉工具箱

我使用检测到的坐标更改中点的颜色。假设X 是检测到的中点的帧行号,Y 是列号。

while ~isDone(hVideoFileReader)

    frame = step(hVideoFileReader);
    ...
    frame(X-3:X+3, Y-3:Y+3, 1) = 1; % # R=1 make the defined region red
    frame(X-3:X+3, Y-3:Y+3, 2) = 0; % # G=0
    frame(X-3:X+3, Y-3:Y+3, 3) = 0; % # B=0

    step(hVideoPlayer, frame);

end

然后我很容易得到一个红色方块。但我想在检测到的点上添加一个红色的实心圆圈,而不是一个正方形。我该怎么做?

【问题讨论】:

    标签: matlab image-processing matlab-cvst


    【解决方案1】:

    您可以使用insertShape 函数。示例:

    img = imread('peppers.png');
    img = insertShape(img, 'FilledCircle', [150 280 35], ...
        'LineWidth',5, 'Color','blue');
    imshow(img)
    

    位置参数指定为[x y radius]


    编辑:

    这是我们手动绘制圆形(具有透明度)的替代方法:

    % some RGB image
    img = imread('peppers.png');
    [imgH,imgW,~] = size(img);
    
    % circle parameters
    r = 35;                    % radius
    c = [150 280];             % center
    t = linspace(0, 2*pi, 50); % approximate circle with 50 points
    
    % create a circular mask
    BW = poly2mask(r*cos(t)+c(1), r*sin(t)+c(2), imgH, imgW);
    
    % overlay filled circular shape by using the mask
    % to fill the image with the desired color (for all three channels R,G,B)
    clr = [0 0 255];            % blue color
    a = 0.5;                    % blending factor
    z = false(size(BW));
    mask = cat(3,BW,z,z); img(mask) = a*clr(1) + (1-a)*img(mask);
    mask = cat(3,z,BW,z); img(mask) = a*clr(2) + (1-a)*img(mask);
    mask = cat(3,z,z,BW); img(mask) = a*clr(3) + (1-a)*img(mask);
    
    % show result
    imshow(img)
    

    我正在使用 Image Processing Toolbox 中的 poly2mask 函数来创建圆形遮罩(来自 post 的想法)。如果您无权访问此功能,可以使用以下替代方法:

    [X,Y] = ndgrid((1:imgH)-c(2), (1:imgW)-c(1));
    BW = (X.^2 + Y.^2) < r^2;
    

    这样您就可以获得仅使用核心 MATLAB 函数的解决方案(没有工具箱!)

    【讨论】:

    【解决方案2】:

    如果您有安装了计算机视觉系统工具箱的旧版 MATLAB,您可以使用vision.ShapeInserter 系统对象。

    【讨论】:

    【解决方案3】:

    感谢@Dima,我创建了一个 shapeInserter 对象。

    greenColor = uint8([0 255 0]); 
    hFilledCircle = vision.ShapeInserter('Shape','Circles',...
                                  'BorderColor','Custom',...
                                  'CustomBorderColor', greenColor ,...
                                  'Fill', true, ...
                                  'FillColor', 'Custom',...
                                  'CustomFillColor', greenColor );
    ...
    
    fc = int32([Y X 7;]);
    
    frame = step(hFilledCircle, frame, fc);
    

    然后我将它应用到检测点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多