刚刚写了imshow, 想了想发现imwrite和imshow是完全一致的, 所以根据上篇文章简单写写imwrite用法。
上篇文章链接: http://blog.csdn.net/watkinsong/article/details/38535341
采用图像:
imwrite() 中, 如果参数为uint8类型, 那么期待的参数像素值范围为0-255, 如果参数矩阵为double类型, 那么期待的像素值范围为0-255.
在imwrite中, 如果你将读取的图像转换为double类型, 直接用imwrite保存图像, 会出现全是白色的图像的情况 , 因为在转化图像类型的时候, 图像的像素值虽然变为double数据类型, 但是取值范围仍然是0-255, imwrite处理double类型的时候期待的数据范围是0-1, 会把大于1的值当作1处理, 这样几乎所有的像素都变成了白色。
根据上一篇文章, 这里直接简单给出所有的测试代码以及保存的图像结果:
%% this file is used to show how to use imshow or imagesc and so on % there is some different when you use imshow to display double image or uint8 image % here all the code will process gray image, RGB color image will not suitable for this file's code % but you can convert RGB color image to gray image if you want to exercise by this code % img will be uint8 type img = imread('syz.bmp'); % imshow: when the parameter img is uint8 type, % imshow will default make the range of pixel value as [0-255] imshow(img); % this write correct pic imwrite(img, 'sw1.bmp'); fprintf('Program paused. Press enter to continue.\n'); pause; % sometimes we need to process the img by double type, % so, you may convert the data type to double dimg = double(img); % but, right now you will not get the correct display, % because if the parameter of imshow is double type, if will defaultly % take range of [0-1], but now the range is [0-255] % all the value over 1 is ceilled to 1. (clamped to 1) % so, the displayed image will be a whole white picture. imshow(dimg); % this will write a white pic imwrite(dimg, 'sw2.bmp') fprintf('Program paused. Press enter to continue.\n'); pause; % how to correctly display double typed image % way 1: convert double to uint8 imshow(uint8(dimg)); imwrite(uint8(dimg), 'sw3.bmp') fprintf('Program paused. Press enter to continue.\n'); pause; % way 2: change the value of double typed image to [0-1] maxVal = max(max(dimg)); imshow(dimg / maxVal); imwrite(dimg / maxVal, 'sw4.bmp'); fprintf('Program paused. Press enter to continue.\n'); pause; %% some other occurence, the image maybe in double type, % but some normalization operation will lead to negative pixel value % In order to display the image, we need to add a value that make % all negative value to positive value % here i just minus a specific value, in real application, % a face image maybe normalized by substract mean face. normImg = dimg - 127; % then normImg will have some negative values minVal = min(min(normImg)); imshow(uint8(normImg - minVal)); imwrite(uint8(normImg - minVal), 'sw5.bmp'); fprintf('Program paused. Press enter to continue.\n'); pause; %% if you want to save image by imwrite() % if the image is double type, you need to normalize the value to [0-1] % if the image is uint8 type, it's ok to save image directly.
保存的图像:
sw1: 因为img本身就是uint8类型, 所以保存的图像是正确的
sw2: (其实下面是一张白色图像), 将图像转换为double以后, 值还是在0-255, 但是imwrite要求是0-1, 所以这个时候超过1的像素都被当作1, 所有图像像素几乎都是白色。。。。其实下面图像可以看到一个黑色的像素点。。。
sw3: 转换像素类型后正确
sw4: 将像素值归一到0-1之间, 正确
sw5: 处理存在像素值为负数的像素值后, 正确
matlab ( octave ) imwrite 保存图像详解
时间: 2024-11-03 22:34:45