"Why do you have M&Ms on your desk?" my friend Nausheen wanted to know. Well, the truth is, for playing around with color images, M&Ms are simply irresistable.
url = 'https://blogs.mathworks.com/images/steve/2010/mms.jpg'; rgb = imread(url); imshow(rgb)
I can think a few different things we could try with this image. First let's tackle the question, "What color is green?"
That is, can we quantify the color of the green M&Ms? There are a couple of challenges. The first is that there is some variation in lighting in this image from top to bottom. For example, suppose we pick two pixels from within the regions marked below:
imshow(rgb) hold on plot(212, 26, 'wo', 'MarkerSize', 12) plot(235, 378, 'wo', 'MarkerSize', 12) hold off
Let's display an image containing two giant pixels, one chosen from the upper M&M and the other chosen from the lower one.
twopixels = [rgb(26,212,:), rgb(378,235,:)]; imshow(twopixels, 'InitialMagnification', 'fit')
You can see that's quite a difference in shade. But even within the same M&M there's a lot of color variation. Let's examine two pixels on a single M&M.
imshow(rgb) axis([220 250 370 400]) hold on plot(228, 382, 'wo', 'MarkerSize', 12) plot(237, 389, 'wo', 'MarkerSize', 12) hold off
twopixels = [rgb(382,228,:), rgb(389,237,:)]; imshow(twopixels, 'InitialMagnification', 'fit')
So where do we go from here? Well, next time I'll probably explore one or two other color spaces, and I also plan to show you how to compute and display a two-dimensional histogram.
Get the MATLAB code
Published with MATLAB® 7.11
Comments are closed.
12 CommentsOldest to Newest
clc,clear all,close all url = 'https://blogs.mathworks.com/images/steve/2010/mms.jpg'; rgb = imread(url); [N,M,z]=size(rgb); Im_out=zeros(N,M); %pre-allocate output image se=strel('disk',2); % search each layer for k=1:3 I=rgb(:,:,k); %background canclation background = imopen(I,se); I2=I-background; %adjust intensity %imadjust(I2,[0 0.1],[0 1]) I3=imadjust(I2); %thresholding I4=im2bw(I3,graythresh(I3)); % cancle the noise object Im_out(:,:,k)=bwareaopen(I4,95); end %sum three output and convert to logic Im_out=(Im_out(:,:,1)+Im_out(:,:,2)+Im_out(:,:,3))>0; s=regionprops(Im_out,'basic') imshow(rgb) title(sprintf('Number of Detected m''s = %d ',length(s))) % centroids = cat(1, s.Centroid); % plot(centroids(:,1), centroids(:,2), 'b*') hold on for k = 1 : length(s) rectangle('Position', s(k,1).BoundingBox,'EdgeColor','k','LineWidth',2); end hold off