binary mask for the coordinates using MATLAB

9 ビュー (過去 30 日間)
Neda
Neda 2020 年 5 月 1 日
編集済み: Ameer Hamza 2020 年 5 月 1 日
I need to create a binary mask. I have some coordinates and I make those coordinates and inside that region equal to 1, and background equal to zero. The image size is 600*800
Here is what I have done, but the problem is ROI located not in the correct position and located on the right bottom of the image. I appreciate if someone could point me to the right direction.
X and Y coordinates are attached here, The first col is X and the second Y. Basically, [X(1) Y(1)] is refereing to the pixels on the image.
Here the left is what snippet did produce and the right image is my expectation
Here is my snippet:
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
%connect the end of curve
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);
figure;
imshow(BW);
% close all;
end
  4 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 5 月 1 日
Can you attach the im image here?
Neda
Neda 2020 年 5 月 1 日
Image is 600 * 800 filled with zeros

サインインしてコメントする。

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 5 月 1 日
編集済み: Ameer Hamza 2020 年 5 月 1 日
What about just shifting the x and y coordinates
x = H_D{1}(:,1);
y = H_D{1}(:,2);
im = zeros(600, 800);
mask = Create_mask(x-300, y-300, im);
Also, I recommend that instead of creating your own function to convert x, y coordinates to mask, you can do it with built-in function in a single line
x = H_D{1}(:,1);
y = H_D{1}(:,2);
im = poly2mask(x-300, y-300, 600, 800);
  2 件のコメント
Neda
Neda 2020 年 5 月 1 日
Thank you for your suggestion. Why shifting by 300?
Ameer Hamza
Ameer Hamza 2020 年 5 月 1 日
編集済み: Ameer Hamza 2020 年 5 月 1 日
I just made a guess. It appeared similar to the image you have shown in the question. I think that the actual points themself cannot create the mask you want. Some sort of shifting is necessary.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by