Time for action – color isolation

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:

  1. First, we will load the image and generate the same mask as before, using thresholding, cleaning, and dilation:
    >> img = imread('my_image_color.bmp'),
    >> [output2] = RGBThreshold(img,[160 130 130]);
    >> output2(1:100,:) = 0;
    >> mask = imdilate(output2,strel('disk', 2));
  2. Then, we must use the process described above to separate the color channels:
    >> 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
  3. Now it is time to perform masking. We want all pixels outside our regions of interest to turn to grayscale, which can be achieved by assigning to all channels the same values (which can be acquired by 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);
  4. Finally, we have to join our new color channels to acquire our final image and display our result:
    >> img_final = cat(3,R,G,B);
    >> figure, imshow(img_final)
    Time for action – color isolation

Not bad for such a quick process, right?

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset