Aqua Phoenix
     >>  Lectures >>  Matlab 4  
 

Navigator
   
 
       
   

4.2 Conditionals - Examples

4.2.1 Color Clipping

In this example, we will change the color palette of an image by clipping all dark pixels below some threshold, that is these dark pixels will be set to intensity 0 (or 255). The resulting image then only contains color values that are above the threshold intensity.

For the first example, the threshold is set to 120. Any color value below 120 will be set to 0. We will make use of a triple loop, where the outermost loop iterates over rows, the middle loop iterates over columns, and the innermost loop iterates over the three color values Red, Green, and Blue.

cartman=imread('cartmancop.jpg');
l=size(cartman);
for i=1:l(1)
  for j=1:l(2)
    for k=1:3
      if (cartman(i,j,k) < 120)
        cartman(i,j,k) = 0;
      end
    end
  end
  end
end
image(cartman);

Figure 4.3
Click image to enlarge, or click here to open
The conditional in this example tests whether or not a color value is below the threshold of 120, and if this condition holds then the intensity is set to 0.

Instead of setting the intensity to 0, we could also set it to 255 for a different effect.

cartman=imread('cartmancop.jpg');
l=size(cartman);
for i=1:l(1)
  for j=1:l(2)
    for k=1:3
      if (cartman(i,j,k) < 120)
        cartman(i,j,k) = 255;
      end
    end
  end
  end
end
image(cartman);

Figure 4.4
Click image to enlarge, or click here to open
Finally, we can attenuate and amplify the color intensities depending on a threshold. Consider setting the intensity of a pixel to 0 if it is below the threshold and 255 if it is above the threshold:

cartman=imread('cartmancop.jpg');
l=size(cartman);
for i=1:l(1)
  for j=1:l(2)
    for k=1:3
      if (cartman(i,j,k) > 100)
        cartman(i,j,k) = 255;
      else
        cartman(i,j,k) = 0;
      end
    end
  end
  end
end
image(cartman);
With this effect the image is reduced to a very small number of colors, namely the most extreme ones: Black (0,0,0), Red (255,0,0), Green (0,255,0), Blue (0,0,255), Yellow (255,255,0), Magenta (255,0,255), Cyan (0,255,255), and White (255,255,255).

Figure 4.5
Click image to enlarge, or click here to open