我需要根据一系列坐标创建一个二进制掩码。我当前的代码如下所示,但是结果图像的边缘不平滑。我认为这并不精确,我需要确保连接的是精确坐标并将它们连接在一起。
在左侧,我绘制了点(总是42点),在右侧是代码的输出。如您所见,边缘不光滑。这是当前代码和输出:(coordinates are attached)
[im
是大小为112 x 112的图像,除X
,Y
坐标外,在填充255的区域内,其他所有位置均填充零。
function BW = mask_data(X,Y, im)
X = round(X);
Y = round(Y);
%round coordinates
X ( X < 1 ) = 1;
Y ( Y < 1 ) = 1;
BW = im > 255;
for p = 1:length(X)
BW(Y(p),X(p)) = 1;
end
BW = BW * 255;
BW = bwconvhull(BW);
BW = im2uint8(BW);
figure;
imshow(BW);
close all;
end
我相信凸凸联合船体仍然是您最好的选择。如果您的图像实际上是由单个对象组成的,则尽管在显示的代码中执行了一些多余的步骤,但是显示的算法应该可以正常工作。如果不是这种情况,那么您可能需要考虑通过在objects
调用中添加bwconvhull
选项来查找多个组件的凸包。如果您坚信结果是“不精确的”,则可能需要显示一个示例图像,其中算法实际上会失败。
根据结果不平滑的情况,从逻辑上来说,对于对象边界与您所显示的相似的112 x 112尺寸的图像,您不应期望平滑的边界。但是,如果希望使用平滑的图像,我只会平滑结果:
originalImage = imread('Adrress\to\your\image.png');
% To have the same image size as yours
originalImage = imresize(originalImage, [112 112]);
% Create a binary image
binaryImage = im2bw(originalImage);
% Create a binary convex hull image
UnionCH = bwconvhull(binaryImage);
% Smooth the results (note the change of binary class)
% Second arg (0.7) is the std dev of the Gaussian smoothing kernel
SmoothUnionCH = imgaussfilt(single(UnionCH), 0.7);
figure
subplot(131)
imshow(binaryImage)
title('Binary Image')
subplot(132)
imshow(UnionCH)
title('Binary Convex Hull Image')
subplot(133)
imshow(SmoothUnionCH,[])
title('Smooth Convex Hull Image')
当然,您可以调整平滑内核的大小。上面代码的结果: