Drawing two shapes on one image

4 views (last 30 days)
med-sweng
med-sweng on 23 Dec 2013
Edited: Youssef Khmou on 24 Dec 2013
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.

Answers (2)

Youssef  Khmou
Youssef Khmou on 23 Dec 2013
Edited: Youssef Khmou on 24 Dec 2013
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 Comments
med-sweng
med-sweng on 23 Dec 2013
@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 on 24 Dec 2013
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 .

Sign in to comment.


Image Analyst
Image Analyst on 24 Dec 2013
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.

Tags

Community Treasure Hunt

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

Start Hunting!