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

조회 수: 2 (최근 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개)

카테고리

Help CenterFile Exchange에서 Motion Planning에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by