Let's try to perform color isolation for our lake fence photograph. The goal will be to convert the whole image to grayscale, except the fence tips, that should remain red. Here are the steps to accomplish this:
>> img = imread('my_image_color.bmp'), >> [output2] = RGBThreshold(img,[160 130 130]); >> output2(1:100,:) = 0; >> mask = imdilate(output2,strel('disk', 2));
>> 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
rgb2gray
). All these pixels have the value zero in our mask. So:>> img_gray = rgb2gray(img); >> R(mask == 0) = img_gray(mask == 0); >> G(mask == 0) = img_gray(mask == 0); >> B(mask == 0) = img_gray(mask == 0);
>> img_final = cat(3,R,G,B); >> figure, imshow(img_final)
Not bad for such a quick process, right?
In this example, you learned how to perform color isolation in an image in a quick and quite efficient manner. We followed the same steps as before, to generate our mask for the fence tips, altering just our structuring element to achieve a less crude segmentation. Then, we split the color channels and used the inverse of our mask (the pixels having a zero value) to transform all regions, except the ones in the mask, to grayscale. To achieve this, the values of all pixels equal to zero in the mask were set to their grayscale equivalent, for all color channels. When the process was finished, we joined the resulting color channels to acquire our final image. The result may not be optimal, but it is certainly very good, considering that we did not manually choose the ROIs to be isolated, but instead we performed image thresholding to generate the mask.