matlab ( octave ) imwrite 保存图像详解


文章出处:http://blog.csdn.net/watkinsong/article/details/38535595

刚刚写了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: 处理存在像素值为负数的像素值后, 正确

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: Octave工具箱是Octave的扩展包,用于增加Octave的功能和工具。虽然Octave的工具箱与Matlab的工具箱不完全相同,但它们提供了类似的功能和工具。\[1\] Octave的工具箱是由第三方作者开源贡献的,可以在https://octave.sourceforge.io/packages.php 上找到许多相似的工具箱。因此,Octave可以作为Matlab的替代品,并且可以满足大部分用户的需求。\[1\] Octave的语法与Matlab基本一致,使用起来几乎没有差别,因此可以方便地迁移和使用Matlab的代码。\[2\] 此外,Octave是免费的开源软件,不需要进行授权激活,而且安装包相对较小,不像Matlab那样占用大量的存储空间。因此,如果您只需要使用Matlab的部分功能,Octave是一个很好的选择。\[2\] #### 引用[.reference_title] - *1* *3* [Matlab真的不可替代么 - 迁移Octave实战](https://blog.csdn.net/weixin_36410316/article/details/115833578)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Mac上安装Octave替代MATLAB进行信号分析相关的工作](https://blog.csdn.net/shirukai/article/details/123046552)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值