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.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Apr. 2011

0 Stimmen

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 Kommentare

nghia nguyen
nghia nguyen am 20 Apr. 2011
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.
nghia nguyen
nghia nguyen am 20 Apr. 2011
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.
Walter Roberson
Walter Roberson am 20 Apr. 2011
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 ?
nghia nguyen
nghia nguyen am 20 Apr. 2011
I have already solved this problem.Thanks for your help again.
nghia nguyen
nghia nguyen am 20 Apr. 2011
As you said,my background is white and I changed it to black.So problem was solved.
nghia nguyen
nghia nguyen am 25 Apr. 2011
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.
Walter Roberson
Walter Roberson am 25 Apr. 2011
Please make the image available on a web server and post the URL.
nghia nguyen
nghia nguyen am 25 Apr. 2011
Thanks.Here are links
http://www.flickr.com/photos/48230201@N02/5653553606/in/photostream
http://www.flickr.com/photos/48230201@N02/5652986417/in/photostream/

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by