您的位置:首页 > 编程语言 > MATLAB

【MATLAB图像处理学习】2.灰度变换

2020-04-02 07:25 3207 查看

【使用的教材:冈萨雷斯 数字图像处理MATLAB(Digital image processing with Matlab】
前言:剩下的图像处理基本操作简介我大段跳过了 后面用到哪儿介绍到哪儿

CHAPTER 3 灰度变换及空间滤波

g(x,y)=T[f(x,y)] g(x,y)=T[f(x,y)] g(x,y)=T[f(x,y)]

f(x,y)表示原图像像素点,*g(x,y)*表示处理过后图像像素点,T

3.2 灰度变换

3.2.1 函数
imadjust

函数

imadjust
用于进行图像的灰度变换

g = imadjust(f, [low_in high_in],[low_out high_out],gamma)

[low_in high_in]中的值对应的映射成[low_out high_out]中的值。如果gamma小于1,此映射偏重更高数值输出;如果gamma大于1,此映射偏重更低数值(灰暗)输出,如果省略此参数,默认为(线性映射)。

%取负片
%[low_in high_in]和[low_out high_out]必须在[0,1]范围内
%也就是说imadjust这个函数将原像素值先进行归一化(将像素值线性缩小到[0,1]),然后再进行映射
g1 = imadjust(f,[0 1],[1 0])
g = imcomplement(f)

3.2.2 对数变换和对比度拉伸转换

g=c∗log(1+double(f)) g=c*log(1+double(f)) g=c∗log(1+double(f))

  • double将数据转换为双精度型

当监视器的线性范围为8个bit时,展现效果由高灰度值主导,导致低灰度值的细节丧失。对数变换能够压缩动态范围,例如,原来的范围为10610^6106,经过对数变换后能够减小到14左右,更好的展示细节。

gs = im2uint8(mat2gray(g))

mat2gray
对图像进行归一化操作,将像素值线性缩小到[0,1],
im2unit8
将像素值线性放大到[0,255]范围中

g = im2uint8(mat2gray(log(1 + double(f))))
imshow(g)

上述的所有代码:

f = imread('breast_digital_Xray.tif');
g1 = imadjust(f,[0 1],[1 0]);
g = imcomplement(f);
subplot(1,3,1)
imshow(f)
title('未处理');
hold on
subplot(1,3,2)
imshow(g1)
title('g1 = imadjust(f,[0 1],[1 0])');
hold on
subplot(1,3,3)
imshow(g)
title('g = imcomplement(f);');
f = imread('DFT_no_log.tif');
subplot(1,2,1)
imshow(f)
title('未处理');
hold on
g = im2uint8(mat2gray(log(1 + double(f))));
subplot(1,2,2)
imshow(g)
title('对数变换');

3.2.3 灰度变换

nargin
,
nargout
分别返回函数输入参数和输出参数的数量。

varargin
,
varargout
分别为可变长度的输入、输出参数列表。

四种变换

  1. 图像反转——neg

    g = imcomplement(f)

    s=L−1−r s = L-1-r s=L−1−r

  2. 对数变换——log

    g = c*(log(1+double(f)))

    s=clog(1+r) s = clog(1+r) s=clog(1+r)

    对数函数有个重要特征,即压缩像素值变化较大的图像的动态范围。

  1. 伽马校正——gamma

    g = imadjust(f,[],[],gam)

    s=crγ s=cr^\gamma s=crγ

如果所关注的是在计算机屏幕上精确显示图像,则伽马校正是很重要的。不恰当校正的图像看起来不是太亮,就是太暗。试图精确再现色彩也需要伽马校正的一些知识,因为改变伽马值不仅会改变亮度,而且会改变彩色图像中红、绿、蓝的比率。

  1. 对比度拉伸——stretch
if length(varargin)==1
%Use defaults.
m = mean2(f);
E = 4.0;
elseif length(varargin)==3
m=varargin{2};E=varargin{3};
else
error('Incorrect number of inputs for the stretch option.')
end
g=1./(1+(m./(f+eps)).^E);

将四种变换整合到一个函数后的代码,包括

main.m
作为例程

main.m

f = imread('bone-scan.tif');
g = intrans(f,'stretch', mean2(im2double(f)), 0.9);
figure
subplot(1,2,1)
imshow(f)
title('RAW')
hold on
subplot(1,2,2)
imshow(g)
title('STRETCH')

changeclass.m

function image = changeclass(class, varargin)
%CHANGECLASS changes the storage class of an image.
% I2 = CHANGECLASS(CLASS, I);
% RGB2 = CHANGECLASS(CLASS, RGB);
% BW2 = CHANGECLASS(CLASS, BW);
% X2 = CHANGECLASS(CLASS, X, 'indexed');
% Copyright 1993-2002 The MathWorks, Inc. Used with permission.
% $Revision: 1.2 $ $Date: 2003/02/19 22:09:58 $
switch class
case 'uint8'
image = im2uint8(varargin{:});
case 'uint16'
image = im2uint16(varargin{:});
case 'double'
image = im2double(varargin{:});
otherwise
error('Unsupported IPT data class.');
end

intrans.m

function g=intrans(f, varargin)
%INTRANS Performs intensity(gray-level) transformations.
%   G=INTRANS(F,' neg') computes the negative of input image F.
%   G=INTRANS(F,'10g',C, CLASS) computes C*10g(1+F) and
%   multiplies the result by(positive) constant C. If the last two
%   parameters are omitted,C defaults to 1. Because the log is used
%   frequently to display Fourier spectra, parameter CLASS offers the
%   option to specify the class of the output as 'uint8' or
%   'uint16'. If parameter CLASS is omitted, the output is of the
%   same class as the input.

%   G=INTRANS(F,' gamma', GAM) performs a gamna transformation on
%   the input image using parameter GAM(a required input).

%   G=INTRANS(F,' stretch',N,E) computes a contrast-stretching
%   transformation using the expression 1./(1+(M./(F+
%   eps)).E). Parameter M must be in the range [0,1]. The default
%   svalue for M is mean2(im2double(F)), and the default value for E
%   is 4.

%   For the 'neg',' gamma', and 'stretch' transformations, double
%   sinput images whose maximum value is greater than 1 are scaled
%   first using MAT2GRAY. Other images are converted to double first
%   using IM2DOUBLE. For the 1og' transformation, double images are
%   transformed without being scaled; other images are converted to
%	double first using IM2D0UBLE.

%   The output is of the same class as the input, except if a
%   different class is specified for the 1og option.

% Verify the correct number of inputs.
% error(nargchk(2,4))
% Store the class of the input for use later.
classin=class(f);
%If the input is of class double, and it is outside the range
%[0,1], and the specified transformation is not '1og', convert the
%input to the range [0,1].
if strcmp(class(f),'double')&& max(f(:)) > 1 &&...
~strcmp(varargin{1},'1og')
f=mat2gray(f);
else % Convert to double, regardless of class(f).
f=im2double(f);
end
% Determine the type of transformation specified.
method=varargin{1};
% Perform the intensity transformation specified.
switch method
case' neg'
g=imcomplement(f);
case '1og'
if length(varargin) == 1
c = 1;
elseif length(varargin) == 2
c = varargin{2};
elseif length(varargin)==3
c = varargin{2};
classin = varargin{3};
else
error('Incorrect number of inputs for the 1og option.')
end
g = c*(log(1+double(f)));
case 'gamma'
if length(varargin) < 2
error('Not enough inputs for the gamma option.')
end
gam = varargin{2};
g = imadjust(f,[],[],gam);
case 'stretch'
if length(varargin)==1
%Use defaults.
m = mean2(f);
E = 4.0;
elseif length(varargin)==3
m=varargin{2};E=varargin{3};
else
error('Incorrect number of inputs for the stretch option.')
end
g=1./(1+(m./(f+eps)).^E);
otherwise
error('Unknown enhancement method.')
end
%Convert to the class of the input image.
g = changeclass(classin,g);
  • 点赞
  • 收藏
  • 分享
  • 文章举报
一只小地瓜 发布了6 篇原创文章 · 获赞 5 · 访问量 311 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: