Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.96 KB | None | 0 0
  1. % Demo macro to very, very simple color detection in
  2. % HSV (Hue, Saturation, Value) color space.
  3. % Requires the Image Processing Toolbox.  Developed under MATLAB R2010a.
  4. % by ImageAnalyst
  5. function SimpleColorDetectionByHue()
  6. clc;    % Clear command window.
  7. clear% Delete all variables.
  8. close all% Close all figure windows except those created by imtool.
  9. imtool close all;   % Close all figure windows created by imtool.
  10. workspace% Make sure the workspace panel is showing.
  11.  
  12. % Change the current folder to the folder of this m-file.
  13. % (The "cd" line of code below is from Brett Shoelson of The Mathworks.)
  14. if(~isdeployed)
  15.     cd(fileparts(which(mfilename))); % From Brett
  16. end
  17.  
  18. ver % Display user's toolboxes in their command window.
  19.  
  20.  
  21.  
  22.  
  23.  
  24.     % Continue with the demo.  Do some initialization stuff.
  25.     close all;
  26.     fontSize = 16;
  27.     figure;
  28.     % Maximize the figure.
  29.     set(gcf, 'Position', get(0, 'ScreenSize'));
  30.  
  31.     [baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
  32.     fullImageFileName = fullfile(folder, baseFileName);
  33.    
  34.     % Read in image into an array.
  35.     [rgbImage storedColorMap] = imread(fullImageFileName);
  36.     [rows columns numberOfColorBands] = size(rgbImage);
  37.     % If it's monochrome (indexed), convert it to color.
  38.     % Check to see if it's an 8-bit image needed later for scaling).
  39.    
  40.     if numberOfColorBands == 1
  41.         if isempty(storedColorMap)
  42.             % Just a simple gray level image, not indexed with a stored color map.
  43.             % Create a 3D true color image where we copy the monochrome image into all 3 (R, G, & B) color planes.
  44.             rgbImage = cat(3, rgbImage, rgbImage, rgbImage);
  45.         else
  46.             % It's an indexed image.
  47.             rgbImage = ind2rgb(rgbImage, storedColorMap);
  48.             % ind2rgb() will convert it to double and normalize it to the range 0-1.
  49.             % Convert back to uint8 in the range 0-255, if needed.
  50.             if eightBit
  51.                 rgbImage = uint8(255 * rgbImage);
  52.             end
  53.         end
  54.     end
  55.    
  56.     % Display the original image.
  57.     subplot(3, 4, 1);
  58.     imshow(rgbImage);
  59.     drawnow; % Make it display immediately.
  60.    
  61.     % Convert RGB image to HSV
  62.     hsvImage = rgb2hsv(rgbImage);
  63.     % Extract out the H, S, and V images individually
  64.     hImage = hsvImage(:,:,1);
  65.     sImage = hsvImage(:,:,2);
  66.     vImage = hsvImage(:,:,3);
  67.    
  68.     % Assign the low and high thresholds for each color band.
  69.    
  70.     % Use values that I know work for the onions and peppers demo images.
  71.     redThreshMin = 0.0;
  72.     redThreshMax = 0.1;
  73.     redSatMin = 0.4;
  74.     redSatMax = 1;
  75.     redValThreshMin = 0.8;
  76.     redValThreshMax = 1.0;
  77.    
  78.     % Now apply each color band's particular thresholds to the color band
  79.     redHueMask = (hImage >= redThreshMin) & (hImage <= redThreshMax);
  80.     redSatMask = (sImage >= redSatMin) & (sImage <= redSatMax);
  81.     redValMask = (vImage >= redValThreshMin) & (vImage <= redValThreshMax);
  82.  
  83.     redObjectsMask = uint8(redHueMask & redSatMask & redValMask);
  84.     subplot(3, 4, 2);
  85.     imshow(redObjectsMask, []);
  86.     caption = sprintf('Mask of Only\nThe Red Objects');
  87.     title(caption, 'FontSize', fontSize);
  88.  
  89.     smallestAcceptableArea = 10000; % Keep areas only if they're bigger than this.
  90.    
  91.     % Open up a new figure, since the existing one is full.
  92.     % Maximize the figure.
  93.     % Get rid of small objects.  Note: bwareaopen returns a logical.
  94.    
  95.     redObjectsMask = uint8(bwareaopen(redObjectsMask, smallestAcceptableArea));
  96.     subplot(3, 4, 3);
  97.     imshow(redObjectsMask, []);
  98.     fontSize = 13;
  99.     caption = sprintf('bwareaopen() removed objects\nsmaller than %d pixels', smallestAcceptableArea);
  100.     title(caption, 'FontSize', fontSize);
  101.  
  102.     % Smooth the border using a morphological closing operation, imclose().
  103.     structuringElement = strel('disk', 4);
  104.     redObjectsMask = imclose(redObjectsMask, structuringElement);
  105.     subplot(3, 3, 2);
  106.     imshow(redObjectsMask, []);
  107.     fontSize = 16;
  108.     title('Border smoothed', 'FontSize', fontSize);
  109.  
  110.     % Fill in any holes in the regions, since they are most likely red also.
  111.     redObjectsMask = uint8(imfill(redObjectsMask, 'holes'));
  112.     subplot(3, 3, 3);
  113.     imshow(redObjectsMask, []);
  114.     title('Regions Filled', 'FontSize', fontSize);
  115.  
  116.     % You can only multiply integers if they are of the same type.
  117.     % (yellowObjectsMask is a logical array.)
  118.     % We need to convert the type of yellowObjectsMask to the same data type as hImage.
  119.     redObjectsMask = cast(redObjectsMask, class(rgbImage));
  120.  
  121.     % Use the yellow object mask to mask out the yellow-only portions of the rgb image.
  122.     maskedImageR = redObjectsMask .* rgbImage(:,:,1);
  123.     maskedImageG = redObjectsMask .* rgbImage(:,:,2);
  124.     maskedImageB = redObjectsMask .* rgbImage(:,:,3);
  125.    
  126.     % Concatenate the masked color bands to form the rgb image.
  127.     maskedRGBImage = cat(3, maskedImageR, maskedImageG, maskedImageB);
  128.     % Show the masked off, original image.
  129.     subplot(3, 3, 8);
  130.     imshow(maskedRGBImage);
  131.     fontSize = 13;
  132.     caption = sprintf('Masked Original Image\nShowing Only the Yellow Objects');
  133.     title(caption, 'FontSize', fontSize);
  134.     % Show the original image next to it.
  135.     subplot(3, 3, 7);
  136.     imshow(rgbImage);
  137.     title('The Original Image (Again)', 'FontSize', fontSize);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement