Finding x,y coordinates of pixel by RGB color value

Hi, Is there a reverse impixel function? I want to find the x and y coordinates of pixels in a color image by searching for them only with a RGB color value. I have tried converting into a binary image and searching for the '1' values by using
BW = im2bw(RGB, level)
and then [rows,cols] = find(BW==1);
but I am confused on how to set a level to implement an RGB value so that only my specific color pixels will show up as white in the binary image.
Is there a better way to find the coordinates of pixels only by using RGB value?
thanks!

 Réponse acceptée

Try this:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Find where each color plane is your exact desired color value.
mask = redChannel == desiredRedValue && ...
greenChannel == desiredGreenValue & ...
blueChannel == desiredBlueChannel;
[rows, columns] = find(mask); % Note [rows, columns] = [y, x], NOT [x, y]
WHY do you want to do this. You probably don't need to and there is a better way if I just knew what you were going to do after this.

4 commentaires

thank you for your response. I was hoping you would answer since you seem like the expert in this type of stuff.
the stuff you provided works well but is it possible to find(mask) only in a set of given rows, columns. for instance if I only want to search from x coordinates 1-10 and y coordinates 1-10 (perhaps some sort of for loop).
thus my columns/rows output would be giving x and y values only between 1-10. I dont care about the coordinates that fall outside that range
Or create a separate rows columns matrix using only values in my range?
If not possible I could always resize the entire image just to fit the coordinates I want but I just want to see if there is another solution to the problem I gave above
Well I don't know how expert I am. I've only been doing image processing full time for the past 40 years so I've just barely scratched the surface. And I'm falling behind (all of us are) because the amount being learned and published is just too massive for anyone to know but a very small fraction of it.
I still don't know what you want to do. If you want to make a mask that is false for all rows and columns beyond 10, you can do this:
% Find where each color plane is your exact desired color value.
mask = redChannel == desiredRedValue && ...
greenChannel == desiredGreenValue & ...
blueChannel == desiredBlueChannel;
mask(11:end, :) = false;
mask(1:10, 11:end) = false;
[rows, columns] = find(mask); % Note [rows, columns] = [y, x], NOT [x, y]
but your explanation didn't help. Tell me what the chunks of code are going to do after you have the rows and columns arrays. What are you going to do with them?
Thank you for your response. I've only been doing image processing for about the past 40 Minutes so I too have just barely scratched the surface. I am trying to catch ahead though.
My previous inquiry was too confusing. I was looking for a false range mask for rows and a completely DIFFERENT false range mask for columns. For instance if i only wanted to search in X coordinates of 10-20 and Y coordinates of 90-100? Is there a way to syntax a DIFFERENT false ranges for columns and rows.
Eventually I will be finding the distance between coordinates. And, yes I have gone through your demos. However, I believe this is the best way to go forward because I figured out that the desiredColor value can be a range in each channel and thus I can be easily searching for many different RGB values at the same time.
I don't know what a "false range" is.
If you want to know what coordinates have x between 10 and 20 and y between 90 and 100, then do
indexes = columns >= 10 & columns <= 20 & rows >= 90 & rows <= 100;
% Extract only those in the range
c2 = columns(indexes);
r2 = rows(indexes);

Connectez-vous pour commenter.

Plus de réponses (1)

Franklin Prashanth C
Franklin Prashanth C le 22 Jan 2018
Modifié(e) : Image Analyst le 23 Jan 2018
How can I get these xy coordinate values, so that I can use it.?
[rows, cols] = find(BW==1); or
[rows, columns] = find(mask);

1 commentaire

It just depends on whether you want the columns called "col" or "columns" and whether your binary image is called BW or mask. The ==1 is not necessary and can get deleted.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by