This time we will perform color isolation for our lake fence photograph, using just channel a*. The goal will remain at converting the whole image to grayscale, except the fence tips that should remain red. For comparison purpose, we will also perform the RGB process. We will follow these steps:
>> img = imread('my_image_color.bmp'), >> [output2] = RGBThreshold(img,[160 130 130]); >> output(1:100,:) = 0; >> maskRGB = imdilate(output2,strel('disk', 2));
>> cform = makecform('srgb2lab'), % Make the transform structure >> img_lab = applycform(img,cform); % Apply transform >> maskLab = (img_lab(:,:,2) > 150); % Threshold a* channel
>> 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); >> R1 = R; G1 = G; B1 = B; % Keep 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);
>> img_final_RGB = cat(3,R1,G1,B1); >> img_final_Lab = cat(3,R,G,B); >> subplot(2,2,1),imshow(maskRGB),title('RGB Mask') >> subplot(2,2,2),imshow(maskLab),title('L*a*b* Mask') >> subplot(2,2,3),imshow(img_final_RGB),title('RGB Result') >> subplot(2,2,4),imshow(img_final_Lab),title('L*a*b* Result')
imtool
in both results, to zoom in on some details. The following image shows the bottom right area of the image, cropped, and zoomed to 200%. On the left, we have the RGB isolation result and on the right, the CIE-L*a*b* isolation result:This example has made clear the twofold superiority of using CIE-L*a*b* for color isolation. First, the red color isolation mask is generated using just one color channel, therefore, one threshold value. Second, the final isolation result is better and more coherent; as such it does not need morphology adjustments. The process followed is identical with the one explained before, so no new methods are presented at this point.
You should by now have grasped the process of masking. Furthermore, we have so far written some custom-made functions to perform various tasks. So, why don't you have a go at both; write a function that will take in an image along with three color thresholds and return the isolated color image derived from the thresholds. To make your function more versatile, add two more functionalities: the support for more than one color space and the support for regional color isolation (selecting the ROI with the color we want to isolate).
You shouldn't have too much trouble writing a function defined as follows:
function [output] = ROIColorIsolation(input, thresh, cspace) % Function for color isolation in a user-defined image ROI % Inputs: % input - Input image % thresh - Thresholds matrix ([1st 2nd 3rd]) % cspace - Color space for mask selection (0: RGB % 1: CIE-L*a*b*) % Output: % output - Output image (masked)
Let's test your code for correctness. You will call the function twice; once for the RGB case and once for the CIE-L*a*b* case. The channels you don't want to use in the process should have a threshold value of zero. To mix things up a little, let's use the book's cover page image to test your function. Hopefully, you will get some similar results, using the R and G channels for the RGB method (they both should have high values) and channel b* for the CIE-L*a*b* method (it also should have high values, denoting yellow color).
>> steps = imread('Steps.bmp'), >> [resultRGB] = ROIColorIsolation(steps,[150 150 0], 0); >> [resultLab] = ROIColorIsolation(steps,[0 0 150], 1);
In both the cases, our ROI selection is around the flowers at the lower-left part of the image as shown in the following screenshot:
The results should look something like the following screenshot:
>> subplot(3,1,1),imshow(steps),title('Original Image') >> subplot(3,1,2),imshow(resultRGB),title('RGB - ROI color isolation') >> subplot(3,1,3),imshow(resultLab),title('L*a*b* - ROI color isolation')