find spatial coordinates (x,y) for a pixel value in color image? (Opposite of impixel function)

4 次查看(过去 30 天)
I have a color image with 2 circle (one with red, one with blue i.e. I know the pixel value of these two component). I need to extract (x,y) coordinates of the areas covered by this two color. The opposite is done by IMPIXEL function e.g. for a known (x,y), impixel will give us PIXEL value of that location. But, in my case, I need (n*2) matrix of (x,y) spatial coordinates of known pixel values (the colored components). Any suggestion?
I am thinking about
1.
conn_c = bwconncomp(colored_image);
PixelListTo = regionprops(conn_c,'PixelList');
I did that for binary image. In current case, I need to input two different color which is not possible in bwconncomp.
2. IMPROFILE will give me (x,y) for defined endpoints of line segment, is there any way to put pixel value so that (x,y) coordinate for that pixel value (can be multiple points) can be obtained.
  2 个评论
Tanmoy
Tanmoy 2015-5-29
But actually, for example, in the attached color file,
I need to find all (x,y) coordinates of a component as (n*2) matrix (n = number of pixel points with similar color). Each connected component in the image have separate color (e.g. RGB = 255,0,50 for 1st component, RGB = 255, 0,100 for 2nd component).

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2015-5-20
R = colored_image(:,:,1);
G = colored_image(:,:,2);
B = colored_image(:,:,3);
matches1 = R == target1R & G == target1G & B == target1B;
matches2 = R == target2R & G == target2G & B == target2B;
now you can run bwconncomp on matches1 and matches2.
If you only have one circle of each color, then you could potentially skip bwconncomp and regionprops PixelIdList and just use
[x1, y1] = find(matches1);
[x2, y2] = find(matches2);
  8 个评论
Tanmoy
Tanmoy 2015-5-31
编辑:Tanmoy 2015-5-31
I know the color values (e.g. [136 0 21], [0 162 232], [255 201 14]), so, for my code, input is these 3 vector, I need (x,y) coordinates for each color. Something that works as reverse of IMPIXEL function!
Walter Roberson
Walter Roberson 2015-5-31
colored_image = imread('Tammoy217661.png');
target1R = 136; target1G = 0; target1B = 21;
target2R = 0; target2G = 162; target2B = 232;
target3R = 255; target3G = 201; target3B = 14;
R = colored_image(:,:,1);
G = colored_image(:,:,2);
B = colored_image(:,:,3);
matches1 = R == target1R & G == target1G & B == target1B;
matches2 = R == target2R & G == target2G & B == target2B;
matches3 = R == target3R & G == target3G & B == target3B;
figure; image(matches1); colormap(gray(2));
figure; image(matches2); colormap(gray(2));
figure; image(matches3); colormap(gray(2));
[r1, c1] = find(matches1);
[r2, c2] = find(matches2);
[r3, c3] = find(matches3);
This can clearly be made shorter and put into a function that searches for a particular color.
There are also numerous other ways of implementing it. For example,
locs1R = find(R == target1R);
locs1G = find(G(locs1R) == target1G);
locs1GO = locs1R(locs1G);
locs1B = find(B(locs1GO) == target1B);
locs1idx = locs1GO(locs1B);
[r1, c1] = ind2sub(size(R),locs1idx);
This might be more efficient when there are relatively few pixels of the given target, as it can end up doing few comparisons, but when the majority of locations match then it would be slower.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by