Today I return to examining this picture of M&Ms:
url = 'http://blogs.mathworks.com/images/steve/2010/mms.jpg'; rgb = imread(url); imshow(rgb)
Previously I showed how to convert pixel colors to the L*a*b* space and how to compute a two-dimensional histogram of the a* and b* color components:
Now I want to segment the image based on one of the blobs in this histogram. I used imfreehand to draw around the blob on the left-left. I can't directly show this interactive operation here, but here's a screen shot:
Here's the code I used to draw the shape and to get the coordinates of its outline:
h = imfreehand(gca); % draw shape using mouse ab = getPosition(h);
I stored the P-by-2 matrix of a*-b* coordinates in a MAT-file so I can reproduce the results in this blog post. Here's the code copy this MAT-file from its online location to a temporary location and then load it into MATLAB.
matfile_url = 'http://blogs.mathworks.com/images/steve/2011/freehand_segment.mat'; temp_matfile = [tempname '.mat']; urlwrite(matfile_url, temp_matfile); s = load(temp_matfile); delete(temp_matfile)
Here are the first few saved a*-b* values:
ans = -71.5173 65.6792 -70.9335 65.6792 -70.9335 65.0954 -70.3497 64.5116 -69.7659 63.9277
Now I want to ask the question, "Which original image pixel values have a*-b* values that fall within this shape?" I'll answer the question using inpolygon.
lab = lab2double(applycform(rgb, makecform('srgb2lab'))); a = lab(:,:,2); b = lab(:,:,3); in = inpolygon(a, b, s.ab(:,1), s.ab(:,2)); imshow(in)
That looks promising, but I'd like to visualize this result superimposed over the original image. To do that, I'll fill in the holes, find the perimeter of the result, thicken the perimeter a bit to make it easier to see, and then overlay it in black. (NOTE: imoverlay is not part of the Image Processing Toolbox. You can download it from the MATLAB Central File Exchange.)
in_filled = imfill(in, 'holes'); in_filled_perimeter = bwperim(in_filled); in_filled_perimeter_thickened = imdilate(in_filled_perimeter, ones(3,3)); rgb2 = imoverlay(rgb, in_filled_perimeter_thickened, [0 0 0]); imshow(rgb2)
Not bad! It looks like we found all the green M&Ms.
There are a couple more things I want to try with this image before giving it a rest. I think that next time I'll look at how one might implement a nearest-neighbor classifier.
Get the MATLAB code
Published with MATLAB® 7.11
10 CommentsOldest to Newest
That’s neat! I think we can do really “hand free” in the step to compute ab (now using interactive imfreehand function) by finding connected components (regionprops function) of the “image” two-dimensional histogram (named H as your previous post). Then ab for each component is just the boundaries of the component. So we can automatically do segmentation for all color M&M.
My question is what is the main advantage of going through *a*b space rather than using directly RGB color space?
Do we reduce size of H (histogram in *a*b) just for saving computation?
Trung—The L*a*b* color space gives us a nice way to separate perceived lightness from perceived color. It is also more perceptually uniform than the RGB space is.
Hi, it´s awesome, If I would like find objects of black color, what kinf of parameters I have to vary… and if want to implement in a DSP, I can use simulink to generate code C….
thanks and regards!!!!
Emmanuel—You’ll want to identify pixels for which L, a, and b values are all close to zero. As for using Simulink for the purpose of code generation, you might want to check out the Video and Image Processing Blockset together with Real Time Workshop.
Hi, I’m confused, the third image you used that relationship to that of mms, and certainly this type of work can be done with Simulink blocks
Emmanuel—I don’t understand your question.
Oh sorry!! To identify circles, be of any color, first I have to convert the image into space L*a*b*… and other question there is a function that replaces imfreehand!!
Emmanuel—I still don’t understand either of your questions.
it’s great what you can achieve, you do look too easy, but for someone beginner is too complex, I need to read more about matlab …
Valery—Thanks for your comment. I write here mostly about what interests me personally, which is image processing algorithms, MATLAB techniques, and (sometimes) software design issues. Although many of my posts have a tutorial flavor, it isn’t usually my goal to address the needs of newcomers to MATLAB. I encourage you to dive into the “Getting Started” portion of the MATLAB documentation.