请详细阅读imwrite的help:
if the (input) data array is double, the assumed dynamic range is [0,1]
语句"round(rand(5)*255)",使得double型变量img中几乎所有数据都大于1,超出了动态范围,故而得到的Unit8型变量a中几乎所有数据都为255。
根据help中的说明:
For ...RGB images, if the data array is double, the assumed dynamic range is [0,1]. The data array is automatically scaled by 255 before being written out as uint8. ...If logical data is written to a BMP, ...it is assumed to be a binary image and will be written with a
bitdepth of 1.
修改程序如下:
img(:,:,1)=rand(5);
img(:,:,2)=rand(5);
img(:,:,3)=rand(5);
imwrite(img,'rand.bmp','bmp')
a=imread('rand.bmp','bmp');
数值上,a=floor(img*255)+1
如果输入的是unit8或unit16型的img的话,就更容易了
详细内容参见imwrite、imread的help文件中的Date Types
|