RGB (Truecolor) ImagesAn RGB image, sometimes referred to as a "truecolor" image, is stored in MATLAB as an m-by-n-by-3 data array that defines red, green, and blue color components for each individual pixel. RGB images do not use a palette. The color of each pixel is determined by the combination of the red, green, and blue intensities stored in each color plane at the pixel's location. Graphics file formats store RGB images as 24-bit images, where the red, green, and blue components are 8 bits each. This yields a potential of 16 million colors. The precision with which a real-life image can be replicated has led to the nickname "truecolor image." An RGB MATLAB array can be of class double, uint8, or uint16. In an RGB array of class double, each color component is a value between 0 and 1. A pixel whose color components are (0,0,0) is displayed as black, and a pixel whose color components are (1,1,1) is displayed as white. The three color components for each pixel are stored along the third dimension of the data array. For example, the red, green, and blue color components of the pixel (10,5) are stored in RGB(10,5,1), RGB(10,5,2), and RGB(10,5,3), respectively. To display the truecolor image RGB, use the image function. For example,
image(RGB)
The next figure shows an RGB image of class double.
To determine the color of the pixel at (2,3), you would look at the RGB triplet stored in (2,3,1:3). Suppose (2,3,1) contains the value 0.5176, (2,3,2) contains 0.1608, and (2,3,3) contains 0.0627. The color for the pixel at (2,3) is 0.5176 0.1608 0.0627
在MATLAB中,数值一般都采用double型(64位)存储和运算,为了节省存储空间,MATLAB为图像提供了特殊的数据类型uint8(8位无符号整数),以此方式存储的图像称为8位型像。函数image能够直接显示8位图像,但8位型数据和double型数据在image中意义不一样,对于索引图像,数据矩阵中的值指定该像素的颜色种类在色图矩阵中的行数。当数据矩阵中的值为0时,表示用色图矩阵中第一行表示的颜色绘制;当数据矩阵中的值为1时,表示用色图矩阵中的第二行表示的颜色绘制该像素,数据与色图矩阵中的行数总是相差1。所以,索引图像double型和uint8型在显示方法上没有什么不同,只是8位数据矩阵的值和颜色种类之间有一个偏差1。调用格式均为image(x); colormap(map);对于灰度图像,uint8表示范围[0,255],double型表示范围[0,1]。可见,double型和uint8型灰度图像不一样,二者转换格式为:
I8=uint8 (round (I64*255));
I64=double (I8)/255;
反之,imread根据文件中的图像种类作不同的处理。当文件中的图像为灰度图像时,imread把图像存入一个8位矩阵中,把色图矩阵转换为双精度矩阵,矩阵中每个元素值在[0,1]内;当为RGB图像时,imread把数据存入到一个8位RGB矩阵中。