counting the number of objects on image with MatLab

1.4k views Asked by At

I need to count the number of chalks on image with MatLab. I tried to convert my image to grayscale image and than allocate borders. Also I tried to convert my image to binary image and do different morphological operations with it, but I didn't get desired result. May be I did something wrong. Please help me!

My image:

enter image description here

3

There are 3 answers

3
Dima On

You can use the fact that chalk is colorful and the separators are gray. Use rgb2hsv to convert the image to HSV color space, and take the saturation component. Threshold that, and then try using morphology to separate the chalk pieces.

3
ABC On

Ok so I spent a little time working on this- but unfortunately I'm out of time today and I apologize for the incomplete answer, but maybe this will get you started- (if you need more help, I'll edit this post over the weekend to give you a more complete answer :))

Here's the code-

for i=1:3
    I = RWBDS(:,:,i);  
    se = strel('rectangle', [265,50]);
    Io = imopen(I, se);
    Ie = imerode(I, se);
    Iobr = imreconstruct(Ie, I);
    Iobrd = imdilate(Iobr, se);
    Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
    Iobrcbr = imcomplement(Iobrcbr);
    Iobrcbrm = imregionalmax(Iobrcbr);
    se2 = strel('rectangle', [150,50]);
    Io2 = imerode(Iobrcbrm, se2);
    Ie2 = imdilate(Io2, se2);
    fgm{i} = Ie2;
end

fgm_final = fgm{1}+fgm{2}+fgm{3}; 
figure, imagesc(fgm_final); 

Result from imagesc

It does still pick up the edges on the side of the image, but from here you're going to use connected bwconnectedcomponents, and you'll get the lengths of the major and minor axes, and by looking at the ratios of the objects it will get rid those.

Anyways good luck!

EDIT: I played with the code a tiny bit more, and updated the code above with the new results. In cases when I was able to get rid of the side "noise" it also got rid of the side chalks. I figured I'd just leave both in.

What I did: In most cases a conversion to HSV color space is the way to go, but as shown by @rayryeng this is not the way to go here. Hue works really well when there is one type of color- if for example all chalks were red. (Logically you would think that going with the color channel would be better though, but this is no the case.) In this case, however, the only thing all chalks have in common is the relative shape. My solution basically used this concept by setting the structuring element se to something of the basic shape and ratio of the chalk and performing morphological operations- as you originally guessed was the way to go.

For more details, I suggest you read matlab's documentation on these specific functions.

And I'll let you figure out how to get the last chalk based on what I've given you :)

1
andrew On

This is also not a full solution, but hopefully it can provide a starting point for you or someone else.

Like Dima I noticed the chalk is brightly colored while the dividers are almost gray. I thought you could try and isolate gray pixels (where a gray pixel says red=blue=green) and go from there. I tried applying filters and doing morphological operations but couldn't find something satisfactory. still, I hope this helps

mim = imread('https://i.stack.imgur.com/RWBDS.jpg');

%we average all 3 color channels (note this isn't exactly equivalent to
%rgb2gray)
grayscale = uint8(mean(mim,3));

%now we say if all channels (r,g,b) are within some threshold of one another
%(there's probabaly a better way to do this)
my_gray_thresh=25;
graymask =  (abs(mim(:,:,1) - grayscale) < my_gray_thresh)...
          & (abs(mim(:,:,2) - grayscale) < my_gray_thresh)...
          & (abs(mim(:,:,3) - grayscale) < my_gray_thresh);

figure(1)
imshow(graymask);

graymask