roipoly for multiple shapes

조회 수: 4 (최근 30일)
Thomas Darwent
Thomas Darwent 2018년 8월 17일
댓글: Image Analyst 2018년 8월 20일
I'm wanting to create multiple closed unconnected shapes using roipoly. I can do this fine for 2 shapes but when I add in the code for a 3rd shape it always connects the last coord of the 3rd shape to the first coord of the 1st shape.
if true
xmask_temp = [];
ymask_temp = [];
junk = [];
for i=1
A=imread(['Image0001.bmp']);
[junk,xmask_temp,ymask_temp]=roipoly(A);
a = 1+size(xmask_temp,1);
xmask(1:size(xmask_temp,1),i)=xmask_temp;
ymask(1:size(ymask_temp,1),i)=ymask_temp;
[junk,xmask_temp,ymask_temp]=roipoly(A);
b = a-1+size(xmask_temp,1);
xmask(a:b,i)=xmask_temp;
ymask(a:b,i)=ymask_temp;
b = b+1;
[junk,xmask_temp,ymask_temp]=roipoly(A); % where the problem starts
c = b-1+size(xmask_temp,1);
xmask(b:c,i)=xmask_temp;
ymask(b:c,i)=ymask_temp;
end
B = roipoly(A,xmask,ymask);
imshow(B);

채택된 답변

Image Analyst
Image Analyst 2018년 8월 17일
In the loop, call poly2mask() to get a new mask for the new polygon they draw. Then OR it in with the image.
Before the loop
mask = false(rows, columns); % Initialize final mask
Then in the loop
thisMask = poly2mask(xVertices, yVertices, rows, columns);
mask = mask | thisMask; % Combine this mask with the final mask.
That way you build up the final mask by ORing in each individual mask one at a time. Change variable names to whatever you're using, of course.
  댓글 수: 2
Thomas Darwent
Thomas Darwent 2018년 8월 20일
Hi, Thanks for your help. This method does build up an image of unconnected shapes but for the application we are using we need to build this image using roipoly.
Image Analyst
Image Analyst 2018년 8월 20일
Why do you say "but"? You still use roipoly(). You just also use poly2mask to create a mask from those vertices, and then OR in that mask with a cumulative mask you're building.
If you can't figure it out, attach 'Image0001.bmp' and I'll make a full demo.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Author Block Masks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by