Finding a specific colour within a picture.

hey guys. This is my approach but it doesn't work:
im = imread('img.jpg');
n = 1;
figure, imshow(im)
[x,y] = ginput(n);
x = floor(x);
y = floor(y);
colour = im(y,x,:);
[r,c,t] = size(im);
R = colour(1,1,1);
G = colour(1,1,2);
B = colour(1,1,3);
RGB = [R G B];
Red = im(:,:,1);
Green = im(:,:,2);
Blue = im(:,:,3);
%[i1 j1] = find(Red == colour(:,:,1));
%[i2 j2] = find(Green == colour(:,:,2));
%[i3 j3] = find(Blue == colour(:,:,3));
[i j] = find(im == colour); %This is the main problem
I get error: Array dimensions must match for binary array op. I also want to know if there is a way to find colors close to my color instead of exact match.

Réponses (3)

[i j] = find(all(im == repmat(color,r,c,3),3));
Which is why it is easier to instead
[i j] = find(Red == R && Green == G && Blue == B);
The difficulty with finding colors "close" to a specific color, is in defining what "close" means for colors. Is medium-bright red "close" to bright red, or is medium-bright red "close" to medium-bright purple ?
One way that is as arbitrarily meaningful as any other:
[i j] = find( sqrt((double(Red) - R).^2 + (double(Green) - G).^2 + (double(Blue) - B).^2) <= COLORTOLERANCE );
Image Analyst
Image Analyst le 5 Nov 2011

0 votes

Yes, that's a terrible way. Try my color segmentation demos. I have a few different (better) methods.
evin2165
evin2165 le 7 Juin 2016

0 votes

how to detection common rgb values in matlab ??

6 commentaires

What are "common rgb values" ? Are you trying to find all of the locations in an image that have exactly the same RGB combinations? If you are then,
[uniquergb, ~, uidx] = unique( reshape(YourRGBImage, [], 3) );
rgbidx = reshape(uidx, size(YourRGBImage,1), []);
Now uniquergb will be an N x 3 array in which each row is a unique RGB combiantion, and rgbidx will be a 2D array the same size as the image, in which every value is an index into uniquergb -- so each location tells you which of the unique RGB combinations the pixel was.
... but you should probably be considering using rgb2ind() instead of this.
Walter, thank you, this is good idea.
I need now to solve exactly the same task, to find unique RGB combinations into YourRGBImage with 256 x 256 x 256 colors.
But when I realized proposed line:
[uniquergb, ~, uidx] = unique( reshape(YourRGBImage, [], 3) );
I didn't obtain uniquergb as N x 3 array, it was 256 x 1 array, uidx was vector 3 times number of pixels in Your RGBImage.
rgbidx, which I have obtained from the next proposed line
was the matrix of three color components of the YourRGBImage, red, green, and blue, placed along horizontal line.
What is wrong, do you have any idea?
As well, I try to apply rgb2ind(), as you advised, but such operation changed original image, so this way is not too convenient.
Thanks a lot for your help
@Valeriy, why do you think you need to find unique colors? What is your use case? Of course you could just create the 3-D histogram and look for non-zero counts, but why? Why would you want to do that?
counts = zeros(256,256,256);
[rows, columns, numberOfColorChannels] = size(rgbImage);
for col = 1 : columns
for row = 1 : rows
r = rgbImage(row, col, 1);
g = rgbImage(row, col, 2);
b = rgbImage(row, col, 3);
counts(r, g, b) = counts(r, g, b) + 1;
end
end
Valeriy
Valeriy le 1 Juin 2021
@Image Analyst, thank you for your question.
>What is your use case? Why would you want to do that?
I have ~3k x 3k pixels image with 3 x 8 bits colors with color spots in it. The nature of spots is different, so I suppose that for each spot's origin set of RGB components will be separated from spots of other origins. The first question is how many spots origin I have? Next question is to estimate the concentration of each spot. My idea is to calculate for such an image 3D map of gamut concentration, gamut density: calculate not only gamut of the image, but as well amount of the image pixels into each gamut map voxel. It would be a kind of RGB spectroscopy.
>Of course you could just create the 3-D histogram and look for non-zero counts, but why?
Yes, I already have realized such an algorithm, It works, it clearly shows me the number of "islands" or "clouds" on the gamut density map, so the number of different origins into an image. My idea is correct, it works. Concentration could be estimated from ratio of volume of each "cloud" to the total volume of the image.
But such a solution has some drawbacks: calculation of such distribution for one image on my most powerful computer takes 14 hours. So I decided at first to find a set of unique colors and organize the 1D loop along with this set. I suppose that such a loop will be much, much quicker. This is the reason for my search for a set of unique colors.
I did some experiments with unique command, joining r,g,b components into one vector, like
rgb = r + 256*g + 65536*b,
applying to its unique command and recovering from its result set of r, g, b unique components, but something was wrong, it doesn't work as it has.
Any constructive ideas are welcome
Image Analyst
Image Analyst le 1 Juin 2021
Yeah, just as I thought. I'm almost certain that what you asked to do is not what you really want to or need to do. Please post the original image, along with a marked up/annotated one that says what parts of the image you want to find and measure. And is the number of spots the same in all your images or does it vary? Is there a size range for your spots, or can they range from one single individual pixel up to the whole entire image area?
Valeriy
Valeriy le 2 Juin 2021
Déplacé(e) : DGM le 12 Fév 2023
>Yeah, just as I thought. I'm almost certain that what you asked to do is not what you really want to or need to do.
Sure, it was only part of the task. Now you know all.
>Please post the original image, along with a marked up/annotated one that says what parts of the image you want to find and measure.
I attached part of the image, which necessary to analyze. Blue channel is partly saturated, but this is test image to elaborate correct procedure. Complete image is bigger than allowed 5 Mb.
>And is the number of spots the same in all your images or does it vary?
It is variable, different for different images.
>Is there a size range for your spots, or can they range from one single individual pixel up to the whole entire image area?
As you can see from attached example, spots size is variable, and even worse, spot borders are not well determined, not well "focused". So expected "clouds" won't have sharp borders
Sorry to delay my reply. I was occupied to test version with loop along set of unique colors. Unfortunately, it is also rather long. For 330k unique color and image like I attached it took ~12 hours of calculations. 330k colors well corresponds to the similar value, which Color Inspector 3D shows, so I belive that my estimation of unique colors number now is correct.
I think to filter from unique colors list low repeating values like 1..3 and organize loop only for higher values. Hope it will decrease calculation time because such scan by such low values takes most of the time.
I appreciate a lot your valuable help and discussion, thank you.

Connectez-vous pour commenter.

Catégories

Déplacé(e) :

DGM
le 12 Fév 2023

Community Treasure Hunt

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

Start Hunting!

Translated by