官术网_书友最值得收藏!

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.

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:

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)

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:

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');

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.

主站蜘蛛池模板: 婺源县| 上蔡县| 攀枝花市| 山丹县| 乌什县| 怀安县| 大同市| 昌平区| 新巴尔虎右旗| 凯里市| 威信县| 望城县| 东乡| 务川| 蚌埠市| 无为县| 乌拉特中旗| 嘉义市| 大荔县| 灵山县| 东阳市| 葫芦岛市| 那曲县| 新巴尔虎左旗| 万源市| 富裕县| 铜梁县| 绥化市| 邵阳市| 彭州市| 伽师县| 富裕县| 无锡市| 贵港市| 江达县| 龙江县| 正安县| 晴隆县| 綦江县| 麻栗坡县| 蕉岭县|