如何在 step() 函数内部调用的视频帧上绘制边界和质心
How to plot boundary and centroids on video frames called inside step() function
我在 for
循环中调用一些图像,然后对这些图像进行一些处理。之后,我使用 step
函数在视频播放器中显示这些帧及其遮罩。如何为蒙版图像内的对象添加边界?另外,如何使边界变粗并在蒙版图像中绘制蒙版中每个斑点的质心?下面是代码的粗略草图。
videoPlayer = vision.VideoPlayer();
maskPlayer = vision.VideoPlayer();
for ii = 1:nfiles
filenameii = [............]
frame= imread(filenameii);
mask = dOB(frame,BackgroundImg);
% some processing on the images
mask= bwareaopen(mask,27);
boundaries = bwboundaries(mask,'noholes');
B=boundaries{1};
Centroid = regionprops(mask,'centroid');
Centroids = cat(1, Centroid.Centroid);
plot(B(:,2),B(:,1),'g','LineWidth',3);
plot(Centroids(:,1), Centroids(:,2), 'r+', 'MarkerSize', 10); step(videoPlayer,frame);
step(maskPlayer, mask);
P.S:我知道如何使用 hold on
在图形上显示它,但我希望在视频播放器中显示之前直接在图像上完成。任何指导将不胜感激。
只需先在蒙版上绘制像素,然后再在视频播放器中显示即可。你所拥有的确实有效,但它会为面具玩家绘制图形内部的边界。因此,使用从 bwboundaries
检测到的边界,从这些坐标创建线性索引并将图像中的值设置为白色。可能更简单的是使用检测到的蒙版并使用 bwperim
to automatically produce a mask that contains the boundaries of the blobs. I also see that you are filling in the holes of the mask, so you can use imfill
directly on the output of your post-processing so that it gives you an image instead of coordinates. You would then use this mask to directly index into your image and set the coordinates of the boundaries of the blob to your desired colour. If you desire to make the perimeter thicker, a simple image dilation with imdilate
using the appropriately sized square structuring element will help. Simply define the size of the neighbourhood of this structuring element to be the thickness of the perimeter that you desire. Finally, if you want to insert the centroids into the mask and since you have the MATLAB Computer Vision System Toolbox, use the insertMarker
函数,这样您就可以为每个质心使用一组点并将它们直接放在图像中。但是,您必须确保将掩码从 logical
更改为更适合图像的数据类型。 uint8
应该可以。因此,将图像转换为这种类型,然后将所有非零值乘以 255,以确保蒙版中保持白色。对于 insertMarker
,您想要插入大小为 10 的红色加号,因此我们需要确保我们调用 insertMarker
来反映这一点。另外,因为你想要一个彩色图像,你必须让你的蒙版人工着色,并为每个平面单独绘制你想要的颜色。既然要绿色,这对应RGB值(0,255,0)
。
因此,我修改了您的代码,使其能够执行此操作。此外,我计算了 filled 掩码的质心,而不是原来的。我们不想错误地报告有间隙的物体的质心......除非那是你的目标,但我们假设你不是:
videoPlayer = vision.VideoPlayer();
maskPlayer = vision.VideoPlayer();
% New - Specify colour you want
clr = [0 255 0]; % Default is green
% New - Define thickness of the boundaries in pixels.
thickness = 3;
% New - Create structuring element
se = strel('square', thickness);
for ii = 1:nfiles
filenameii = [............]
frame = imread(filenameii);
mask = dOB(frame, BackgroundImg);
% some processing on the images
mask = bwareaopen(mask,27);
%boundaries = bwboundaries(mask,'noholes');
%B=boundaries{1};
% New code - fill in the holes
mask = imfill(mask, 'holes');
Centroid = regionprops(mask,'centroid');
% New code - Create a boundary mask
mask_p = bwperim(mask, 8);
% New code - Make the boundaries thicker
mask_p = imdilate(mask_p, se);
% New code - create a colour image out of the mask
[red, green, blue] = deal(255*uint8(mask));
% Paint the perimeter of the blobs in the desired colour
red(mask_p) = clr(1); green(mask_p) = clr(2); blue(mask_p) = clr(3);
Centroids = cat(1, Centroid.Centroid);
%plot(B(:,2),B(:,1),'g','LineWidth',3);
%plot(Centroids(:,1), Centroids(:,2), 'r+', 'MarkerSize', 10);
% New - Make mask into RGB image for marker painting and to
% show to the user
mask_p = cat(3, red, green, blue);
% New - Insert the centroids directly in the mask image
mask_p = insertMarker(mask_p, Centroids, '+', 'color', 'r', 'size', 10);
step(videoPlayer, frame);
% New - Show new mask in the player
step(maskPlayer, mask_p);
end
我在 for
循环中调用一些图像,然后对这些图像进行一些处理。之后,我使用 step
函数在视频播放器中显示这些帧及其遮罩。如何为蒙版图像内的对象添加边界?另外,如何使边界变粗并在蒙版图像中绘制蒙版中每个斑点的质心?下面是代码的粗略草图。
videoPlayer = vision.VideoPlayer();
maskPlayer = vision.VideoPlayer();
for ii = 1:nfiles
filenameii = [............]
frame= imread(filenameii);
mask = dOB(frame,BackgroundImg);
% some processing on the images
mask= bwareaopen(mask,27);
boundaries = bwboundaries(mask,'noholes');
B=boundaries{1};
Centroid = regionprops(mask,'centroid');
Centroids = cat(1, Centroid.Centroid);
plot(B(:,2),B(:,1),'g','LineWidth',3);
plot(Centroids(:,1), Centroids(:,2), 'r+', 'MarkerSize', 10); step(videoPlayer,frame);
step(maskPlayer, mask);
P.S:我知道如何使用 hold on
在图形上显示它,但我希望在视频播放器中显示之前直接在图像上完成。任何指导将不胜感激。
只需先在蒙版上绘制像素,然后再在视频播放器中显示即可。你所拥有的确实有效,但它会为面具玩家绘制图形内部的边界。因此,使用从 bwboundaries
检测到的边界,从这些坐标创建线性索引并将图像中的值设置为白色。可能更简单的是使用检测到的蒙版并使用 bwperim
to automatically produce a mask that contains the boundaries of the blobs. I also see that you are filling in the holes of the mask, so you can use imfill
directly on the output of your post-processing so that it gives you an image instead of coordinates. You would then use this mask to directly index into your image and set the coordinates of the boundaries of the blob to your desired colour. If you desire to make the perimeter thicker, a simple image dilation with imdilate
using the appropriately sized square structuring element will help. Simply define the size of the neighbourhood of this structuring element to be the thickness of the perimeter that you desire. Finally, if you want to insert the centroids into the mask and since you have the MATLAB Computer Vision System Toolbox, use the insertMarker
函数,这样您就可以为每个质心使用一组点并将它们直接放在图像中。但是,您必须确保将掩码从 logical
更改为更适合图像的数据类型。 uint8
应该可以。因此,将图像转换为这种类型,然后将所有非零值乘以 255,以确保蒙版中保持白色。对于 insertMarker
,您想要插入大小为 10 的红色加号,因此我们需要确保我们调用 insertMarker
来反映这一点。另外,因为你想要一个彩色图像,你必须让你的蒙版人工着色,并为每个平面单独绘制你想要的颜色。既然要绿色,这对应RGB值(0,255,0)
。
因此,我修改了您的代码,使其能够执行此操作。此外,我计算了 filled 掩码的质心,而不是原来的。我们不想错误地报告有间隙的物体的质心......除非那是你的目标,但我们假设你不是:
videoPlayer = vision.VideoPlayer();
maskPlayer = vision.VideoPlayer();
% New - Specify colour you want
clr = [0 255 0]; % Default is green
% New - Define thickness of the boundaries in pixels.
thickness = 3;
% New - Create structuring element
se = strel('square', thickness);
for ii = 1:nfiles
filenameii = [............]
frame = imread(filenameii);
mask = dOB(frame, BackgroundImg);
% some processing on the images
mask = bwareaopen(mask,27);
%boundaries = bwboundaries(mask,'noholes');
%B=boundaries{1};
% New code - fill in the holes
mask = imfill(mask, 'holes');
Centroid = regionprops(mask,'centroid');
% New code - Create a boundary mask
mask_p = bwperim(mask, 8);
% New code - Make the boundaries thicker
mask_p = imdilate(mask_p, se);
% New code - create a colour image out of the mask
[red, green, blue] = deal(255*uint8(mask));
% Paint the perimeter of the blobs in the desired colour
red(mask_p) = clr(1); green(mask_p) = clr(2); blue(mask_p) = clr(3);
Centroids = cat(1, Centroid.Centroid);
%plot(B(:,2),B(:,1),'g','LineWidth',3);
%plot(Centroids(:,1), Centroids(:,2), 'r+', 'MarkerSize', 10);
% New - Make mask into RGB image for marker painting and to
% show to the user
mask_p = cat(3, red, green, blue);
% New - Insert the centroids directly in the mask image
mask_p = insertMarker(mask_p, Centroids, '+', 'color', 'r', 'size', 10);
step(videoPlayer, frame);
% New - Show new mask in the player
step(maskPlayer, mask_p);
end