一个用于灰度标定的matlab函数

       处理图像时,导致像素值跨越由负数到正数的较宽范围的计算是很常见的(我们在计算的时候一般都是用的double类型哈)。尽管在中间计算过程中不会导致问题,但当我们想要利用8位(uint8)或16位(uint16)格式保存或观看一幅图像时,就会出现问题(负数部分可能会丢失?)。在这种情况下,我们通常希望把图像标度在[0,255]或者[0,65535]之间。下面的称为gscale的自定义函数,就能实现此项功能,此外,该函数能将输出灰度级映射到一个指定的范围。

      函数使用格式为: g=scale(f,method,low,high)

      其中,f是被标定的图像,method的有效值是'full8'(默认)和'full16'或者'minmax','full80'表示输出的指定范围为[0,255],而'full16'把输出标定为[0,65535]。如果使用这两个值之一,那么后面的low,high参数将会被忽略。 选用minmax,则必须提供后面的两个参数low和high。(low和high限定在[0,1]之间,但是程序本身会根据输入的类别做出适当的标定,然后将输出转换为与输入f相同的类型,例如,f是uint8类型,'minmax'的[low,high]为[0,0.5],则输出图像同样为uint8类型,其值在[0,128]范围)。若输入图像f为double类型,且其值在[0,1]之外,那么程序在运行的第一步会将其转换到[0,1]范围内。

    代码如下:

    

functon g=gscale(f,vargin)
% g=gscale(f,'full8')scales the intensities of f to the full 8-bit
% intensity range [0,255].This is the default if there is only one input
% argument.
% g=gscale(f,'full16')scales the intensities of f to the full 16-bit
% intensity range [0,65535].
% g=gscale(f,'minmax',low,high) scales the intensities of f to the
% range[low,high].These values must be provided,and the must be in the
% range [0,1],independently of the class of the input f. Gscale performs
% any necessary scaling. If the input is of class double, and its values
% are not in the range [0,1],then this function scales it to [0,1] at
% first.
% The class of the output is the same as the class of input.
if isempty(vargin) %if there is only one parameter,it must be f.
    method='full8';
else
    method=varargin{1};
end

if strcmp(class(f),'double')&&(max(f(:))>1||min(f(:))<0)
    f=mat2gray(f); 
end   %if the class of f is double and it's not in the range [0,1],scale f to the range[0,1]

switch method 
    case 'full8'
        g=im2uint8(mat2gray(double(f)));
    case 'full16'
        g=im2uint16(mat2gray(double(f)));
    case 'minmax'
        low=varargin{2};
        high=varargin{3};
        if low>1||low<0||high>1||high<0
            error('Parameters low and high must be in the range [0,1]')
        end
        if strcmp(class(f),'double')
            low_in=min(f(:));
            high_in=max(f(:));
        elseif strcmp(class(f),'uint8')
            low_in = double(min(f(:)))./255;
            high_in = double(max(f(:)))./255;
        elseif strcmp(class(f),'uint16')
            low_in = double(min(f(:)))./65535;
            high_in = double(max(f(:)))./65535;
        end
        %imadjust automatically matches the class of the input
        g= imadjust(f,[low_in high_in],[low high]);
    otherwise
        error('unknown method.')
end
    

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值