Take the 2-minute tour ×

Actually, I am new in Matlab. I don't know much about it. How can I count number of color in am image?

For an example I have an image of a DNA microarray (which is an RGB image) and I want to count how many yellow dot, red dot and green dot it has.

share|improve this question
Is it RGB or binary image? –  bonCodigo Dec 15 '12 at 6:18
we are using RGB image –  user1905776 Dec 15 '12 at 7:11
Your question is very hard to understand, specially since most here are unlikely to have a background in biology and what a microarray is. Anyway, on such typical experiment, one cares not only about the color on the array, but also about the intensity. Don't you want to know how many unique colors (RGB combinations) you have? Or do you really just want to know how many are yellow, red and green? Then, you need a definition for yellow, red and green dots. Specially since the "yellow" dots come from mixing the green and red fluorophores. How much is enough for you to consider yellow? –  carandraug Dec 15 '12 at 11:57
add comment

2 Answers

If you are using RGB then it too depends on whether it's 24, 32 or 64 bit. e.g. for 24 bit image, there are 256*256*256 = 16,777,216 of possible combinations of RGB. ;)

Wouldn't it be easier to separate the colours R, G, B, then you count each of those separated subplots ? ;)

Try this out: get each R, G, B channels out. Then do a histogram on it to count each R, G, B pixels.

DNA = imread('peppers.png');
redChannel = DNA(:, :, 1);
greenChannel = DNA(:, :, 2);
blueChannel = DNA(:, :, 3);

For red:
[redCount grayCount] = imhist(redPlane);
subplot(2, 2, 2);
imshow( DNA(:,:,2));
>> redcount
ans = 38385 // SAMPLE count

You may use a loop from 1:3 if you prefer so.

share|improve this answer
thanks for your help..actually the pic is like this. there are several yellow dots, red dots and green dots. then i want to count how much red, green and yellow dots..for an example there will be 50 red dots, 20 yellow dots, 15 green dots. The image is like DNA microarray image.. can you help me.. –  user1905776 Dec 15 '12 at 6:54
add comment

If you have single pixel dots and the "red", "yellow" and "green" means exactly [255 0 0], [0 255 255] and [0 255 0], then your question can be answered quite simple:

red_dots = (DNA(:,:,1)==255); 
n_red_dots = sum(red_dots(:));

yellow_dots = (DNA(:,:,2)==255 & DNA(:,:,3)==255);
n_yellow_dots = sum(yellow_dots(:));

green_dots = (DNA(:,:,2)==255); 
n_green_dots = sum(green_dots(:));

if your colors are not that exact, but still single-pixel (or they are around the same size and you are rather interested in rough proportions), you can do thresholding:

redish_dots = (DNA(:,:,1)>200 & DNA(:,:,2)<40 & DNA(:,:,3)<40);
n_redish_dots = sum(redish_dots(:));

and so on...

share|improve this answer
add comment

Your Answer


