# Color Isolation using MATLAB

by 4hathacker  |  in Image Processing at  9:03 AM

Hello everyone...!!!

Colors have very important role in images. The articles so far presented have dealt with RGB Color Space and HSV Color Space. In TV Screens, Laptops, or Desktops, RGB Color Space has gained much more popularity than others. The main disadvantage with RGB is the correlation of its color channels, which makes it almost infeasible to segment specific colors just by using one of the color channels. One more thing to be added is its susceptibility to shadows and illuminations, which results in distortion in colors.

In this article, color isolaton is discussed with RGB and CIE-L*a*b* color space using transformation functions provided in the MATLAB. CIE-L*a*b* color space is a less sensitive color space and humans perceive it more closely as compared to RGB. The most important reason for this, is that CIE-L*a*b* isolates colors into its a* and b* channels. So, one could rely on just one of the two color channels for generating a color mask.

Here color isolation was performed for lion.jpg, a full of colors, image to isolate pink color. Firstly, some thresholding was applied for our ROIs such that high values of R and low G and B values were kept. Then, morphological dilation(region growing) was done to expand the ROI.

thresholds = [190 140 170];                               % for maskRGB
red_bin = img(:,:,1) > thresholds(1);                 % Red thresholding
green_bin = img(:,:,2) < thresholds(2);             % Green thresholding
blue_bin = img(:,:,3) < thresholds(3);               % Blue thresholding
output2 = red_bin & green_bin & blue_bin;     % Final image
output2(1:100,:) = 0;
maskRGB = imdilate(output2,strel('disk', 2));
cform = makecform('srgb2lab');                        % Construct structure of Transform
img_lab = applycform(img,cform);                   % Transform applying
maskLab = (img_lab(:,:,2) > 200);                    % Threshold a* channel for maskLab
R = img(:,:,1); % store R channel in new matrix
G = img(:,:,2); % store G channel in new matrix
B = img(:,:,3); % store B channel in new matrix
img_gray = rgb2gray(img);                               % all pixels except ROI turned to Grayscale
R1 = R; G1 = G; B1 = B;                                   % Keeping a copy of each color channel
R1(maskRGB == 0) = img_gray(maskRGB == 0);
G1(maskRGB == 0) = img_gray(maskRGB == 0);
B1(maskRGB == 0) = img_gray(maskRGB == 0);
R(maskLab == 0) = img_gray(maskLab == 0);
G(maskLab == 0) = img_gray(maskLab == 0);
B(maskLab == 0) = img_gray(maskLab == 0);

Separate maskRGB and maskLab were prepared and operated over the ROI. The pixels outside ROI turned to grayscale. For maskLab, makecform() function is used for tranformation from RGB to CIE-L*a*b* Color space. And the new color channels obtained were joined together to display the final result.

img_final_RGB = cat(3,R1,G1,B1);                      % joining results from maskRGB
img_final_Lab = cat(3,R,G,B);                              % joining results from maskLab

figure