我是靠谱客的博主 谨慎溪流,最近开发中收集的这篇文章主要介绍matlab save 保存图像,matlab ( octave ) imwrite 保存图像详解,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

刚刚写了imshow, 想了想发现imwrite和imshow是完全一致的, 所以根据上篇文章简单写写imwrite用法。

上篇文章链接: http://blog.csdn.net/watkinsong/article/details/38535341

采用图像:

d6d7e509ddc0f49b34f4e842d303c172.png

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类型, 所以保存的图像是正确的

ee1efa54c2863b951ffcab2c5f051e73.png

sw2: (其实下面是一张白色图像), 将图像转换为double以后, 值还是在0-255, 但是imwrite要求是0-1, 所以这个时候超过1的像素都被当作1, 所有图像像素几乎都是白色。。。。其实下面图像可以看到一个黑色的像素点。。。

1bd90ccd908dc28092764a29017f27f6.png

sw3: 转换像素类型后正确

7b0839c0fb9968105dbf558a24e86407.png

sw4: 将像素值归一到0-1之间, 正确

94c81ba1be4e727cf8f3a7b98893db80.png

sw5: 处理存在像素值为负数的像素值后, 正确

4ef03302f8de77edf5350d823e438d45.png

最后

以上就是谨慎溪流为你收集整理的matlab save 保存图像,matlab ( octave ) imwrite 保存图像详解的全部内容,希望文章能够帮你解决matlab save 保存图像,matlab ( octave ) imwrite 保存图像详解所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部