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.

matlab ( octave ) imshow显示图像详解

时间: 2024-08-24 08:22:28

matlab ( octave ) imshow显示图像详解的相关文章

matlab的pdist函数详解

Pairwise distance between pairs of object(Pdist函数用于各种距离的生成) 语法: D=pdist(x) D=pdist(x,distance) 解释: D=pdist(x) 计算m*n的数据矩阵中对象之间的欧几里得距离.矩阵中每一行作为observation,每一列作为variables(即计算矩阵中每一行之间的距离),D得到的是一个长度为m(m-1)/2的距离向量,距离是按顺序排列的(2,1),(3,1)--.(m,1),(3,2)--..(m,2

matlab ( octave ) imwrite 保存图像详解

刚刚写了imshow, 想了想发现imwrite和imshow是完全一致的, 所以根据上篇文章简单写写imwrite用法. 上篇文章链接: http://blog.csdn.net/watkinsong/article/details/38535341 采用图像: imwrite() 中, 如果参数为uint8类型, 那么期待的参数像素值范围为0-255, 如果参数矩阵为double类型, 那么期待的像素值范围为0-255. 在imwrite中, 如果你将读取的图像转换为double类型, 直接

PCA (主成分分析)详解 (写给初学者) 结合matlab(转载)

一.简介 PCA(Principal Components Analysis)即主成分分析,是图像处理中经常用到的降维方法,大家知道,我们在处理有关数字图像处理方面的问题时,比如经常用的图像的查询问题,在一个几万或者几百万甚至更大的数据库中查询一幅相近的图像.这时,我们通常的方法是对图像库中的图片提取响应的特征,如颜色,纹理,sift,surf,vlad等等特征,然后将其保存,建立响应的数据索引,然后对要查询的图像提取相应的特征,与数据库中的图像特征对比,找出与之最近的图片.这里,如果我们为了提

MATLAB图像处理_plot的使用详解

MATLAB plot函数详解 matlab中plot是最简单的一个画二维图的工具:不过其用法也是很多的,比如线性方面: plot画图的颜色线型    y         黄色           ·             点线 m         粉红           ○             圈线 c         亮蓝           ×             ×线 r         大红           +             +字线 g         绿色 

详解matlab之简易2048制作

详解matlab之简易2048制作 详解matlab之简易2048制作 一制作之前 1关于初始化 2运行时出现2的位置问题 3移动方向问题 4关于数字合并 5关于游戏怎么结束 6其他还有什么就看着需要办吧 二实现过程 1关于空位置补充一个2 2关于移动后的操作 三关于具体某个方向的操作 1上操作 2下操作 3左操作 4右操作 四命令窗口模拟显示 五关于matlab的GUI实现 一):制作之前 小游戏2048出来也算很久了,基本上大家都知道,通俗易懂,玩法也很简单,前几天兴趣来了,通过matlab

图像小波变换原理_图像小波变换的matlab实现详解

小波变换的原理 所谓的小波的小是针对傅里叶波而言,傅里叶波指的是在时域空间无穷震荡的正弦(或余弦波). 相对而言,小波指的是一种能量在时域非常集中的波,它的能量有限,都集中在某一点附近,而且积分的值为零,这说明它与傅里叶波一样是正交波. 举一些小波的例子: 可以看到,能量集中在x轴0值附近,以y轴的0值为基线,上下两个区域的波形面积相等. 众所周知,图像的傅里叶变换是将图像信号分解为各种不同频率的正弦波.同样,小波变换是将图像信号分解为由原始小波位移和缩放之后的一组小波. 小波在图像处理里被称为

【转】小波与小波包、小波包分解与信号重构、小波包能量特征提取 暨 小波包分解后实现按频率大小分布重新排列(Matlab 程序详解)

转:https://blog.csdn.net/cqfdcw/article/details/84995904 小波与小波包.小波包分解与信号重构.小波包能量特征提取   (Matlab 程序详解) -----暨 小波包分解后解决频率大小分布重新排列问题 本人当前对小波理解不是很深入,通过翻阅网络他人博客,进行汇总总结,重新调试Matlab代码,实现对小波与小波包.小波包分解与信号重构.小波包能量特征提取,供大家参考,后续将继续更新! 本人在分析信号的过程中发现,按照网上所述的小波包分解方法理解

xilinx vivado zynq vdma仿真及应用详解(一)

很多人用zynq平台做视频图像开发,但是对vdma了解比较少,上手起来稍微有些困难,我针对这一现象,做了一个基于vivado和modelsim的仿真和应用测试工程,并写篇文章做些介绍,希望能对大家有帮助. 一:xilinx vdma IP例化以及接口介绍 上面图片就是在vivado2015.4中例化vdma的界面,首先对参数做些介绍: Frame Buffers :选择vdma缓存几帧图像,这里默认是写通道和读通道都设置相同的缓存帧数,具体设置多少帧合适一般根据应用来定,比如读写带宽相同,想用d

【数字图像处理】七.MFC图像增强之图像普通平滑、高斯平滑、Laplacian、Sobel、Prewitt锐化详解

本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行讲解,主要通过MFC单文档视图实现显示BMP图像增强处理,包括图像普通平滑.高斯平滑.不同算子的图像锐化知识.希望该篇文章对你有所帮助,尤其是初学者和学习图像处理的学生. [数字图像处理]一.MFC详解显示BMP格式图片 [数字图像处理]二.MFC单文档分割窗口显示图片 [数字图像处理]三.MFC实现图像灰度.采样和量化功能详解 [数字图像处理]四.MFC对话框绘制灰度直方图 [数字图像