Basic color image manipulations

Let's start with the very basics. Importing a color image and accessing its pixels is pretty much the same process as in the case of grayscale images. We can see it using the color version of the image used in Chapter 1, Basic Image Manipulations. To open both the color version and the grayscale version, we will use imread twice:

>> img_gray = imread('my_image.bmp'),
>> img_color = imread('my_image_color.bmp'),

Examining the workspace will reveal the aforementioned difference between grayscale and color images, which is the dimensionality. As we can see in the following screenshot, the grayscale version is 485-by-686 and the color version is 485-by-686-by-3.

Basic color image manipulations

To display both the grayscale and color images, as well as the three color channels of the latter separately on the same figure, we will type in:

>> subplot(2,3,1),imshow(img_gray);title('Grayscale image')
>> subplot(2,3,2),imshow(img_color);title('Color image')
>> subplot(2,3,4),imshow(img_color(:,:,1));title('Red channel')
>> subplot(2,3,5),imshow(img_color(:,:,2));title('Green channel')
>> subplot(2,3,6),imshow(img_color(:,:,3));title('Blue channel')

The resulting image will be as shown in the following screenshot:

Basic color image manipulations

From the highlighted code shown previously, we can see that indexing in the third dimension does the trick, leading to successful displaying of the three color channels. In these images, you can better understand the meaning of separate color channels. A good point to focus on are the red tips of the fence, which have very bright red values and dark green and blue values. We can better understand this by using the Inspect Pixel Values Basic color image manipulations option of imtool and clicking on a pixel of red shade:

>> imtool(img_color)
Basic color image manipulations

From this example, we can see that imtool can be used for color images in the same way we showed in the Chapter 1, Basic Image Manipulations. This time, the three values displayed for each pixel are not the same (as in the case of grayscale images), but instead represent the R, G, and B values of the pixel.

While imrotate also has the same usage as in grayscale images, fliplr and flipud do not. In fact, using them for color images in the same way we did for grayscale ones results in an error message:

Basic color image manipulations

The resulting message implies that the color image is not a two-dimensional matrix, thus cannot be flipped using fliplr. Instead, we must use flipdim for all our color image mirroring tasks. Therefore, we only have the following way to perform horizontal and vertical mirroring:

>> img_color_lr = flipdim(img_color,2);
>> img_color_ud = flipdim(img_color,1);
>> subplot(1,2,1),imshow(img_color_lr);title('Left-right mirroring'),
>> subplot(1,2,2),imshow(img_color_ud);title('Up-down mirroring'),

Basic color image manipulations

The rest of the functions we covered in Chapter 1, Basic Image Manipulations, remain almost identical when using them for color images. More specifically, imresize, imcrop, and imwrite can be used in the same fashion you already know. We will be using these functions as we discuss some more complicated color image processes in the rest of this chapter.

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

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