首页 > 代码库 > matlab ( octave ) imshow显示图像详解
matlab ( octave ) imshow显示图像详解
最近在用octave (类似于matlab的计算软件, 函数和matlab一致) 写程序的时候, 在显示图像和保存图像的时候遇到了一些小问题, 所以简单的总结了一下。
本文用的图像为灰度图像:
imread() 返回的图像类型是uint8类型, 这时用imshow显示图像的时候, imshow会认为输入矩阵的范围在0-255, 如果imshow的参数为double类型的,那么imshow认为输入矩阵的值为0-1.
很多时候需要将图像转换为double类型的, 但是转换以后直接使用imshow显示的是一片白色, 是因为当imshow显示图像的时候, 会认为double类型的图像矩阵的范围在0-1, 超过1的像素值当作1处理, 这样就是几乎所有的像素都是白色。
情况1:
% 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); fprintf('Program paused. Press enter to continue.\n'); pause;
这个时候直接显示读入的图像是正确的, 因为这个时候图像的类型是uint8
显示结果:
情况2:
当把图像转换double数据类型后:
% 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); fprintf('Program paused. Press enter to continue.\n'); pause;
这个时候, 因为imshow的参数为double类型, imshow认为参数的值为0-1, 但是实际还是0-255, 这样超过1的值被认为是1, 显示几乎全是白色
情况3: 如何正确的显示 double 类型的图像呢, 有两种方式
% how to correctly display double typed image % way 1: convert double to uint8 imshow(uint8(dimg)); 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); fprintf('Program paused. Press enter to continue.\n'); pause;
方式1 将图像转换为uint8类型, 方式2将图像像素值归约到0-1之间
显示结果:
这两个显示结果是一样的。
情况4: 有些时候,图像经过处理以后会存在值为负数的像素, 如何显示呢?
这需要将所有的负数处理掉, 可以将所有的像素减去这个图像的最小的像素值, 最小的像素值为负数, 那么图像的像素值就都为正数了。
%% 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)); fprintf('Program paused. Press enter to continue.\n'); pause;
显示结果:
下面是整个示例代码:
%% 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); 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); 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)); 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); 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)); 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.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。