Separate the red and purple pixels in a photo
Show older comments
Hi,
I have a problem to separate the red and purple pixels in the attached 24 bit photo.
Therefore, how can I "isolate", show and count only them (red/purple pixels), an example would be great.
What are the techniques I should consider in this kind of problems?
Thanks.

Answers (2)
Rushikesh Tade
on 5 Sep 2014
Tried to separate out the colors using K-Means Clustering using guide shown http://www.mathworks.in/help/images/examples/color-based-segmentation-using-k-means-clustering.html
input_im=imread('T2.jpg');
sz_im=size(input_im);
cform = makecform('srgb2lab');
lab_he = applycform(input_im,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 3;
% repeat the clustering 3 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
imshow(pixel_labels,[]), title('image labeled by cluster index');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = input_im;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
for k=1:nColors
figure
title_string=sprintf('objects in cluster %d',k);
imshow(segmented_images{k}), title(title_string);
end
Which provides results as follows:

Hope this helps !
3 Comments
Image Analyst
on 5 Sep 2014
That's the same demo I suggested and he already tried, with results shown after my answer. Basically it all comes down to where to draw the dividing lines in the gamut I showed in my comment. Perhaps the kmeans demo can be tweaked to get what he thinks is best. If not, he'll have to write some custom algorithm.
hu
on 5 Sep 2014
Image Analyst
on 5 Sep 2014
See my comment above in my answer.
Image Analyst
on 5 Sep 2014
0 votes
That's what this Mathworks demo does: http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html
7 Comments
hu
on 5 Sep 2014
Image Analyst
on 5 Sep 2014
Edited: Image Analyst
on 5 Sep 2014
You can specify more clusters to get smaller color regions. You can also do it manually if you want. I have several color segmentation demos in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 For example, convert to HSV colorspace and fine tune your thresholds to get just the hues you want. You might also have to combine it with thresholds on the value or saturation channels.
You will find it very instructive to look at your 3D color gamut. Here, use this code to see it. First follow the download instructions at the beginning.
% Code to run the Color Inspector 3D (an ImageJ plug in) inside MATLAB.
%
% Windows installation instructions:
% First, download and install ImageJ from here:
% http://rsb.info.nih.gov/ij/download.html
% Put the ij.jar file here in this folder: "C:\Program Files\ImageJ"
% Then download the 3D Color Inspector plug-in for ImageJ here:
% http://rsb.info.nih.gov/ij/plugins/color-inspector.html
% and save it here in this folder:
% C:/Program Files/ImageJ/plugins/Color
% Define the jar filenames
ci3DjarFileName = 'C:\Program Files\ImageJ\plugins\Color\Color_Inspector_3D.jar';
imageJjarFileName = 'C:\Program Files\ImageJ\ij.jar';
clc; % Clear the MATLAB command window.
% Make sure the Color Inspector 3D jar file is there.
if ~exist(ci3DjarFileName, 'file')
warningMessage = sprintf('Error: Color Inspector 3D jar file does not exist:\n%s', ci3DjarFileName);
warndlg(warningMessage);
return;
end
% Make sure the ImageJ file is there.
if ~exist(imageJjarFileName, 'file')
warningMessage = sprintf('Error: ImageJ jar file does not exist:\n%s', imageJjarFileName);
warndlg(warningMessage);
return;
end
% If we get to here, the files exist.
% Now we need to enable the plug-in to work with MATLAB.
% Need to add the jar files for ImageJ and the Color Inspector 3D to the javaclasspath.
javaaddpath(ci3DjarFileName)
javaaddpath(imageJjarFileName)
% User must browse for the file they want to open.
% Set up a starting/initial folder that they will start browsing from.
startingFolder = pwd; % Change to something specific if you want.
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
% Bring up the "Open File" dialog box.
[baseFileName, folder] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.bmp','Image Files (jpg,tif,png,bmp,gif)';...
'*.*','All Files (*.*)' },'Specify image file', startingFolder);
if baseFileName ~= 0
% If they didn't click Cancel, build the full filename:
fullFileName = fullfile(folder, baseFileName)
% Now pass that into the 3D Color Inspector plugin, and you're done.
color_inspector(fullFileName)
end

Now, looking at this scatterplot of the 3D color gamut, where do you think we should split it up into clusters, and how many clusters would that be? It's not so straightforward is it? You can see that there could be some judgment in where you draw the dividing lines because they are not well separated clusters but more of a continuum.
You might want to try the color segmentation tool in the Image Processing Toolbox: http://www.mathworks.com/help/images/image-segmentation-using-the-color-thesholder-app.html
Image Analyst
on 5 Sep 2014
hu, in the demo link I gave you can get the sum of the pixels for each class (color) by summing the pixel_labels
class0area = sum(sum(pixel_labels == 0));
class1area = sum(sum(pixel_labels == 1));
class2area = sum(sum(pixel_labels == 2));
hu
on 5 Sep 2014
Image Analyst
on 5 Sep 2014
No. I wrote one but it's not nearly as good as the ImageJ plug-in. But I gave you code to run it. All you have to do is to download two files.
hu
on 5 Sep 2014
Image Analyst
on 5 Sep 2014
That's fine, but don't let a preference for MATLAB blind you to the use of other tools that may help you understand and solve your problem. I hope it let you see that there is no clear threshold(s), in any color space, that will definitively work. That said, you can still segment your image by "carving up" the gamut, and you have several ways to do that, with demos in my File Exchange. Good luck. I don't have the Stats toolbox so I don't have kmeans() and I can't help you if you decide to adapt the Mathworks demo.
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!