Drawing two shapes on one image

3 次查看(过去 30 天)
med-sweng
med-sweng 2013-12-23
This is part of what I have done for drawing a shape on my images:
...
...
k = convhull(x,y);
I=imread('img.png');
imshow(I)
hold on
plot(x(k),y(k),'r-',x,y,'b+');
BW = roipoly(I, x(k), y(k) );
What should I do in order to receive the binary result as in BW, but that contains the other shape (i.e; xx, yy)?
I kept hold on, and was able to plot the second shape, but the issue was how to get the binary result of the two shapes at the image.
Thanks.

回答(2 个)

Youssef  Khmou
Youssef Khmou 2013-12-23
编辑:Youssef Khmou 2013-12-24
You can transform I into logical matrix first ( binary), then perform the other operations to add the shape after executing the command 'hold on', , here is an example :
I=imread('circuit.tif');
B=im2bw(I);
% example of drawing shape in the binary image B
% first shape.
p1=150+rand(100,1)*200;
p2=100+rand(100,1)*100;
% second shape.
p3=10+rand(100,1)*50;
p4=10+rand(100,1)*60;
k=convhull(p1,p2);
k2=convhull(p3,p4);
imshow(B), hold on, plot(p1(k),p2(k),'-r',p3(k2),p4(k2),'-g'), hold off;
  2 个评论
med-sweng
med-sweng 2013-12-23
@Youssef KHMOU. Thanks for you reply. The issue is in adding another shape. If I add another shape, I want to see it alongside with the first shape. How can this be performed?
Youssef  Khmou
Youssef Khmou 2013-12-24
you can try the edited code above, it it working fine, i hope you will explain better your situation it the edited code is not working .

请先登录,再进行评论。


Image Analyst
Image Analyst 2013-12-24
Try this:
% Read in first shape and display it.
I=imread('img.png'); % I'm assuming it's binary. Make it binary if not.
imshow(I)
[rows, columns, numberOfColorChannels] = size(I);
hold on
% Now add second shape, assuming we have just a list of x,y coordinates.
% First make it a binary image.
secondBinaryImage = poly2mask(x, y, rows, columns);
% Get the convex hull of it
secondBinaryImage = bwconvhull(secondBinaryImage);
% Then OR it in with the first binary image.
outputBinaryImage = I | secondBinaryImage; % OR operation.
imshow(outputBinaryImage); % Display result.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by