繁体   English   中英

如何使用Matlab在视频帧上绘制实心圆

[英]How to draw a filled circle on a video frame using matlab

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

在此处输入图片说明

我使用检测到的坐标更改中点的颜色。 假设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

然后我很容易有一个红色正方形。 但是我想在检测到的点上添加一个红色的实心圆,而不是正方形。 我怎样才能做到这一点?

您可以使用insertShape函数。 例:

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

位置参数指定为[xy 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)

我使用的poly2mask功能从图像处理工具箱创建圆形遮罩(想法从这个职位 )。 如果您无权使用此功能,请使用以下替代方法:

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

这样,您仅使用核心MATLAB函数即可获得解决方案(无需工具箱!)

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

感谢@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);

然后,我将其应用于检测到的点。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM