Finding a specific colour within a picture.

15 views (last 30 days)
arman Yaraee
arman Yaraee on 4 Nov 2011
Moved: DGM on 12 Feb 2023
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.

Answers (3)

Walter Roberson
Walter Roberson on 4 Nov 2011
[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 on 5 Nov 2011
Yes, that's a terrible way. Try my color segmentation demos. I have a few different (better) methods.

evin2165
evin2165 on 7 Jun 2016
how to detection common rgb values in matlab ??
  6 Comments
Image Analyst
Image Analyst on 1 Jun 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 on 2 Jun 2021
Moved: DGM on 12 Feb 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.

Sign in to comment.

Categories

Find more on Convert Image Type 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!