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

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 Kommentare

Example of color component
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).

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

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 Kommentare

dont understand target1. Matlab tells me it is undefined function
target1R is the red component of the first kind of pixel you want to match. target1G is the green component of the first kind of pixel you want to match. and so on.
When you are trying to find pixels then in general you need to match all three components, the red, green, and blue.
In the particular case where you know that your image divides perfectly by color component, that a pixel that has the right red component will never have a non-zero blue or green component, then you can simplify the search. Using RedValueToMatch to represent the red pixel component value you want to match, and BlueValueToMatch to represent the blue pixel component value you want to match, under these conditions (which never apply for standard JPEG files!)
Example:
%change the two values according to your situation
RedValueToMatch = 240; %the red circle was drawn with (240,0,0)
BlueValueToMatch = 133; %the blue circle was drawn with (0,0,133)
matches1 = colored_image(:,:,1) == RedValueToMatch;
matches2 = colored_image(:,:,2) == BlueValueToMatch;
conn_c1 = bwconncomp(matches1);
PixelListTo1 = regionprops(conn_c1,'PixelList');
conn_c2 = bwconncomp(matches2);
PixelListTo2 = regionprops(conn_c2,'PixelList');
I have tried your procedure, but it gives me all (x,y) of color components exist in a single matrix. But, I need (x,y) matrix for each connected component.
For example, if 3 colored connected component in the image, then, I want output as something like STRUCTURE element whose fields are S.{xy1} = [2 3; 2 5; 2 6;........] S.{xy2} = [20 80; 21 80; 22 80;........ ] S.{xy3} = [58 90; 58 98; 58 91;.....]
You might be able to use some of the color segmentation demos in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
It's customary when asking advice on images or pictures, to upload an image or screenshot to help avoid any misunderstandings about what is wanted, so we don't have to "work blind" and guess. Can you do that?
Could you attach a sample image, and give the values you are trying to match against?
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!
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.

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