使用MATLAB的坐标的二进制掩码

问题描述 投票:0回答:1

我需要创建一个二进制掩码。我有一些坐标,我使这些坐标在该区域内等于1,背景等于零。

这是我所做的,但是问题是ROI的位置不正确,位于图像的右下角。如果有人能指出我正确的方向,我将不胜感激。

function [X,Y, BW] = Create_mask(X,Y,im)


X = round(X);
Y = round(Y);

X ( X < 1 ) = 1;
Y ( Y < 1 ) = 1;


BW = im > 255;
for p = 1:length(X)

    BW(Y(p),X(p)) = 1;
end


for n = 0:(1/round(sqrt((X(end)-X(1))^2 + (Y(end)-Y(1))^2 ))):1

    xn = round(X(1) +(X(end) - X(1))*n);
    yn = round(Y(1) +(Y(end) - Y(1))*n);
    BW(yn,xn) = 1;

end


se = strel('disk',10);
BW = imclose(BW,se);
BW = imdilate(BW,se);
BW = imfill(BW,'holes');
im( im < 255 ) = 0;
im = imclose(im,se);
BW = BW * 255;
BW = im2uint8(BW);
% BW = imresize(BW, [256 256],'nearest');
figure;
imshow(BW);

% close all;

end

这里是输出函数:

enter image description here

我原本希望与此图像相似。这不是确切的解决方案,但显示了我的期望。

enter image description here

X and Y coordinates are attached here,第一个列为X,第二个为Y。

matlab matlab-figure mask
1个回答
0
投票

您可以通过调用函数inpolygon来做到这一点,尝试一下

function mask=createmask(x,y, dx)
cmin=min([x(:) y(:)]);
cmax=max([x(:) y(:)]);
[xi,yi]=meshgrid(cmin(1):dx:cmax(1),cmin(2):dx:cmax(2));
mask=reshape(inpolygon(xi(:),yi(:),x(:),y(:)), size(xi));
© www.soinside.com 2019 - 2024. All rights reserved.