im2uint8函数分析

本文详细分析了Matlab中的im2uint8函数,该函数用于将不同数据类型的图像转换为uint8类型。针对logical、uint8、uint16、double、single和int16等输入类型,介绍了其转换规则。例如,对于logical类型,1转换为255,0保持为0;对于double和single类型,数据会被限制在0-255范围内并四舍五入。此外,还提到了与之相关的grayto8 MEX函数。
摘要由CSDN通过智能技术生成

环境:Win7 64位 + Matlab R2010a

本次分析的函数为im2uint8,这个函数在图像处理中要用到,主要把图像数据类转换到uint8

uint8函数有效的输入的图像数据类为:logical,uint8,uint16,double,single和int16

  • 如果输入类型为logical,则输入数据为1时转换为255,数据为0还是0。
  • 如果输入类型为uint8,则输入数据保持不变。
  • 如果输入类型为uint16,则输入数据除以255,然后四舍五入。
  • 如果输入数据为double,则输入数据乘以255,小于0的数设置为0,大于255的数设置为255,其他的数四舍五入。
  • 如果输入数据为single,处理方式和double一样。
  • 如果输入数据为int16,处理暂且没弄清楚。

 

上面输出的结果根据Matlab提供的M文件进行分析,M文件如下

function u = im2uint8(varargin)
%IM2UINT8 Convert image to 8-bit unsigned integers.
%   IM2UINT8 takes an image as input, and returns an image of class uint8.  If
%   the input image is of class uint8, the output image is identical to it.  If
%   the input image is not uint8, IM2UINT8 returns the equivalent image of class
%   uint8, rescaling or offsetting the data as necessary.
%
%   I2 = IM2UINT8(I1) converts the intensity image I1 to uint8, rescaling the
%   data if necessary.
%
%   RGB2 = IM2UINT8(RGB1) converts the truecolor image RGB1 to uint8, rescaling
%   the data if necessary.
%
%   I = IM2UINT8(BW) converts the binary image BW to a uint8 intensity image,
%   changing one-valued elements to 255.
%
%   X2 = IM2UINT8(X1,'indexed') converts the indexed image X1 to uint8,
%   offsetting the data if necessary. Note that it is not always possible to
%   convert an indexed image to uint8. If X1 is double, then the maximum value
%   of X1 must be 256 or less.  If X1 is uint16, the maximum value of X1 must be
%   255 or less.
%
%   Class Support
%   -------------
%   Intensity and truecolor images can be uint8, uint16, double, logical,
%   single, or int16. Indexed images can be uint8, uint16, double or
%   logical. Binary input images must be logical. The output image is uint8.
%
%   Example
%   -------
%       I1 = reshape(uint16(linspace(0,65535,25)),[5 5])
%       I2 = im2uint8(I1)
%
%   See also IM2DOUBLE, IM2INT16, IM2SINGLE, IM2UINT16, UINT8.

%   Copyright 1993-2004 The MathWorks, Inc.
%   $Revision: 1.20.4.5 $  $Date: 2005/11/15 00:58:23 $

iptchecknargin(1,2,nargin,mfilename);

img = varargin{1};
iptcheckinput(img,{
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值