Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Demo macro to very, very simple color detection in
- % HSV (Hue, Saturation, Value) color space.
- % Requires the Image Processing Toolbox. Developed under MATLAB R2010a.
- % by ImageAnalyst
- function SimpleColorDetectionByHue()
- clc; % Clear command window.
- clear; % Delete all variables.
- close all; % Close all figure windows except those created by imtool.
- imtool close all; % Close all figure windows created by imtool.
- workspace; % Make sure the workspace panel is showing.
- % Change the current folder to the folder of this m-file.
- % (The "cd" line of code below is from Brett Shoelson of The Mathworks.)
- if(~isdeployed)
- cd(fileparts(which(mfilename))); % From Brett
- end
- ver % Display user's toolboxes in their command window.
- % Continue with the demo. Do some initialization stuff.
- close all;
- fontSize = 16;
- figure;
- % Maximize the figure.
- set(gcf, 'Position', get(0, 'ScreenSize'));
- [baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
- fullImageFileName = fullfile(folder, baseFileName);
- % Read in image into an array.
- [rgbImage storedColorMap] = imread(fullImageFileName);
- [rows columns numberOfColorBands] = size(rgbImage);
- % If it's monochrome (indexed), convert it to color.
- % Check to see if it's an 8-bit image needed later for scaling).
- if numberOfColorBands == 1
- if isempty(storedColorMap)
- % Just a simple gray level image, not indexed with a stored color map.
- % Create a 3D true color image where we copy the monochrome image into all 3 (R, G, & B) color planes.
- rgbImage = cat(3, rgbImage, rgbImage, rgbImage);
- else
- % It's an indexed image.
- rgbImage = ind2rgb(rgbImage, storedColorMap);
- % ind2rgb() will convert it to double and normalize it to the range 0-1.
- % Convert back to uint8 in the range 0-255, if needed.
- if eightBit
- rgbImage = uint8(255 * rgbImage);
- end
- end
- end
- % Display the original image.
- subplot(3, 4, 1);
- imshow(rgbImage);
- drawnow; % Make it display immediately.
- % Convert RGB image to HSV
- hsvImage = rgb2hsv(rgbImage);
- % Extract out the H, S, and V images individually
- hImage = hsvImage(:,:,1);
- sImage = hsvImage(:,:,2);
- vImage = hsvImage(:,:,3);
- % Assign the low and high thresholds for each color band.
- % Use values that I know work for the onions and peppers demo images.
- redThreshMin = 0.0;
- redThreshMax = 0.1;
- redSatMin = 0.4;
- redSatMax = 1;
- redValThreshMin = 0.8;
- redValThreshMax = 1.0;
- % Now apply each color band's particular thresholds to the color band
- redHueMask = (hImage >= redThreshMin) & (hImage <= redThreshMax);
- redSatMask = (sImage >= redSatMin) & (sImage <= redSatMax);
- redValMask = (vImage >= redValThreshMin) & (vImage <= redValThreshMax);
- redObjectsMask = uint8(redHueMask & redSatMask & redValMask);
- subplot(3, 4, 2);
- imshow(redObjectsMask, []);
- caption = sprintf('Mask of Only\nThe Red Objects');
- title(caption, 'FontSize', fontSize);
- smallestAcceptableArea = 10000; % Keep areas only if they're bigger than this.
- % Open up a new figure, since the existing one is full.
- % Maximize the figure.
- % Get rid of small objects. Note: bwareaopen returns a logical.
- redObjectsMask = uint8(bwareaopen(redObjectsMask, smallestAcceptableArea));
- subplot(3, 4, 3);
- imshow(redObjectsMask, []);
- fontSize = 13;
- caption = sprintf('bwareaopen() removed objects\nsmaller than %d pixels', smallestAcceptableArea);
- title(caption, 'FontSize', fontSize);
- % Smooth the border using a morphological closing operation, imclose().
- structuringElement = strel('disk', 4);
- redObjectsMask = imclose(redObjectsMask, structuringElement);
- subplot(3, 3, 2);
- imshow(redObjectsMask, []);
- fontSize = 16;
- title('Border smoothed', 'FontSize', fontSize);
- % Fill in any holes in the regions, since they are most likely red also.
- redObjectsMask = uint8(imfill(redObjectsMask, 'holes'));
- subplot(3, 3, 3);
- imshow(redObjectsMask, []);
- title('Regions Filled', 'FontSize', fontSize);
- % You can only multiply integers if they are of the same type.
- % (yellowObjectsMask is a logical array.)
- % We need to convert the type of yellowObjectsMask to the same data type as hImage.
- redObjectsMask = cast(redObjectsMask, class(rgbImage));
- % Use the yellow object mask to mask out the yellow-only portions of the rgb image.
- maskedImageR = redObjectsMask .* rgbImage(:,:,1);
- maskedImageG = redObjectsMask .* rgbImage(:,:,2);
- maskedImageB = redObjectsMask .* rgbImage(:,:,3);
- % Concatenate the masked color bands to form the rgb image.
- maskedRGBImage = cat(3, maskedImageR, maskedImageG, maskedImageB);
- % Show the masked off, original image.
- subplot(3, 3, 8);
- imshow(maskedRGBImage);
- fontSize = 13;
- caption = sprintf('Masked Original Image\nShowing Only the Yellow Objects');
- title(caption, 'FontSize', fontSize);
- % Show the original image next to it.
- subplot(3, 3, 7);
- imshow(rgbImage);
- title('The Original Image (Again)', 'FontSize', fontSize);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement