Counting the number of objects in an image using MatLab

I need to count the number of chalk in an image using MatLab. I tried converting the image to grayscale and highlighting the borders. Also I tried to convert my image to binary and perform various morphological operations on it, but I didn't get the desired result. Maybe I did something wrong. Please help me!

My image:

enter image description here

+3


source to share


3 answers


You can use the fact that the chalk is colored and the spacers 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 pieces of chalk.



+1


source


Ok, so I wasted a little time on this, but unfortunately I don't have time today and I apologize for the incomplete answer, but maybe this will help you get started (if you need more help, I'll edit this post for 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 still raises the edges on the side of the image, but from here you will use the connected bwconnectedcomponents

one and you will get the length of the major and minor axes and looking at the relationships of the objects it will rid them of.



Good luck anyway!

EDIT: I played with the code a bit more and updated the code above with new results. In cases where I was able to get rid of the side "noise", it also got rid of the side chalk. I decided I would leave both.

What I did: In most cases, converting to HSV color space is the way to go, but as @rayryeng shows, it is not the way to go here. Hue works very well when there is one type of color, if, for example, all the crayons were red. (Logically you would think that going with a color channel would be better though, but it isn't.) In this case, however, the only thing that all chalk has is its relative shape. My solution basically used this concept, setting the structuring element se

to something from the basic shape and ratio of the chalk and performing morphological operations - as you originally assumed, this was the way to go.

For more details, I suggest you read the Matlab documentation on these specific functions.

And I will let you know how to get the last chalk based on what I gave you.

+1


source


It's also not a complete solution, but hopefully it can serve as a starting point for you or someone else.

Like Dima, I noticed that the chalk is brightly colored and the dividers are almost gray. I thought you could try isolating the gray pixels (where the gray pixel says red = blue = green) and go from there. I tried to apply filters and morphological operations but couldn't find anything satisfactory. still i hope this helps

mim = imread('http://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 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

+1


source







All Articles