Get the position of pixels with specifed color

Hi!
I have a problem with my fisrt IP project. I have a picture with 2 circles in it, a red circle and green circle. I want to get the position of two center and then connect them. Please tell me how can I do that. Thanks so much.

 Accepted Answer

convert the image to black and white, "label" the image, use regionprops() to find the centroid of each of the objects.
But you will have to define what you mean by "connect them": is that only for display purposes, or do you need to make a new image that has them connected (i.e., for further processing or for writing to a file) ?

8 Comments

Thanks for your help
I want to draw a line (as you said:display) that contains two centers and my target is calculating the angle between that line and a horizontal line.
I've just done something as you said.Firstly,I use power point to draw a large rectangle and two circle in it,red and blue.Then,I write this code:
GB = imread('1.png');
imshow(RGB);
I = rgb2gray(RGB);
threshold = graythresh(I);
bw = im2bw(I,threshold); %Convert RGB to BW
imshow(bw)
L = bwlabel(bw,4);
STATS = regionprops(L==1, bw, 'centroid');
line([0,132],...[0,83],'LineWidth',4,'Color','red');% determine where STATS is
I don't know why I can see in L just two value(0,1)although there are 2 objects in the picture. And I always get only value(132,83)(center of rectangle) when I use regionprops() with L==0,L==1 or l==2.
Please help me again.
If you regionprops all of L instead of L==1 then you should get an array (possibly a structure array) containing all of the centroids.
Are the circles overlapping? That would cause problems for processing in this scheme.
Is your background black or white ?
I have already solved this problem.Thanks for your help again.
As you said,my background is white and I changed it to black.So problem was solved.
Hi,I have something to ask you about this again.The last time,I asked you about an image with a large rectangle and inside that rectangle there are a red circle and a green circle.And I want to locate the center of red circle with the origin-coordinate is one of the corners of rectangle.Everything has been finished with an image I draw in power point.But when I draw that image on a real paper and using a webcam to capture that image,I cann't do the same things as I do with ppt image.When I convert the real image to binary,the rectangle,in some parts,is not separated with background and some areas of background dont't have the same color(black or white) with others.I try to reduce threshold but it is still bad.I just want to separate the rectangle with background to label it and then I do anything I do with ppt image.Can you tell me how can I do that.Sorry because my bad english.
Please make the image available on a web server and post the URL.
Thanks.Here are links
http://www.flickr.com/photos/48230201@N02/5653553606/in/photostream
http://www.flickr.com/photos/48230201@N02/5652986417/in/photostream/

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!