title: A few questions about color segmentation with L*a*b*
body: <p>I am trying to identify the onset of red/yellow color above the inner cone in the following image with color segmentation.</p>
  
  <p><img src="https://i.stack.imgur.com/j86kO.jpg" alt="Inner light blue and outer dark blue cone and a red/yellow region above the inner cone"></p>
  
  <p>To do this, I implemented <a href="http://www.mathworks.com/help/images/examples/color-based-segmentation-using-the-l-a-b-color-space.html" rel="nofollow noreferrer">lab color segmentation</a>, </p>
  
  <pre><code>clear all
  close all
  %Plot image
  flame = imread('flamePic.JPG');
  flame = imrotate(flame,270);
  figure(1), imshow(flame), title('Flame');
  
  
  %Find color of small region
  load regioncoordinates;
  
  nColors = 6;
  sample_regions = false([size(flame,1) size(flame,2) nColors]);
  
  for count = 1:nColors
    sample_regions(:,:,count) = roipoly(flame,region_coordinates(:,1,count),...
                                        region_coordinates(:,2,count));
  end
  
  %imshow(sample_regions(:,:,2)),title('sample region for red');
  
  %Convert your fabric RGB image into an L*a*b* image using makecform and applycform.
  cform = makecform('srgb2lab');
  lab_fabric = applycform(flame,cform);
  
  %Calculate the mean 'a*' and 'b*' value for each area that you extracted with roipoly. These values serve as your color markers in 'a*b*' space.
  a = lab_fabric(:,:,2);
  b = lab_fabric(:,:,3);
  color_markers = repmat(0, [nColors, 2]);
  
  for count = 1:nColors
    color_markers(count,1) = mean2(a(sample_regions(:,:,count)));
    color_markers(count,2) = mean2(b(sample_regions(:,:,count)));
  end
  
  %For example, the average color of the red sample region in 'a*b*' space is
  disp(sprintf('[%0.3f,%0.3f]',color_markers(2,1),color_markers(2,2)));
  
  %Create an array that contains your color labels, i.e., 0 = background, 1 = red, 2 = green, 3 = purple, 4 = magenta, and 5 = yellow.
  color_labels = 0:nColors-1;
  
  %Initialize matrices to be used in the nearest neighbor classification.
  a = double(a);
  b = double(b);
  distance = repmat(0,[size(a), nColors]);
  
  %Perform classification
  for count = 1:nColors
    distance(:,:,count) = ( (a - color_markers(count,1)).^2 + ...
                        (b - color_markers(count,2)).^2 ).^0.5;
  end
  
  [value, label] = min(distance,[],3);
  label = color_labels(label);
  clear value distance;
  
  %Step 4: Display Results of Nearest Neighbor Classification
  %The label matrix contains a color label for each pixel in the fabric image. Use the label matrix to separate objects in the original fabric image by color.
  
  rgb_label = repmat(label,[1 1 3]);
  segmented_images = repmat(uint8(0),[size(flame), nColors]);
  
  for count = 1:nColors
    color = flame;
    color(rgb_label ~= color_labels(count)) = 0;
    segmented_images(:,:,:,count) = color;
  end
  
  %Demonstrate red objects
  figure(2),
  imshow(segmented_images(:,:,:,1)), title('Color1');
  figure(3),
  imshow(segmented_images(:,:,:,2)), title('Color2');
  figure(4),
  imshow(segmented_images(:,:,:,3)), title('Color3');
  figure(5),
  imshow(segmented_images(:,:,:,4)), title('Color4');
  figure(6),
  imshow(segmented_images(:,:,:,5)), title('Color5');
  figure(7),
  imshow(segmented_images(:,:,:,6)), title('Color6');
  </code></pre>
  
  <p>Running this algorithm on the above image gives me the following:</p>
  
  <p><img src="https://i.stack.imgur.com/WqQPO.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/1zNmM.png" alt="enter image description here"></p>
  
  <p>I am not exactly sure what color is being separated, but there is plenty of red that has not been identified. In addition, I am unable to distinguish between the inner, whiter cone, and the outer, dark blue cone. I think it has to do with how I sample the colors and the regions  of interest with <code>roipoly</code> but I am not sure how I can improve it. Do you have any tips on how I can improve the segmentation for this specific application?</p>