find spatial coordinates (x,y) for a pixel value in color image? (Opposite of impixel function)
3 views (last 30 days)
Show older comments
Tanmoy
on 20 May 2015
Commented: Walter Roberson
on 31 May 2015
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 Comments
Accepted Answer
Walter Roberson
on 20 May 2015
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 Comments
Walter Roberson
on 31 May 2015
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.
More Answers (0)
See Also
Categories
Find more on Motion Planning in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!