图像处理怎么学matlab,Matlab数字图像处理学习(1)-亮度变换
发布日期:2022-02-18 13:08:09 浏览次数:9 分类:技术文章

本文共 6416 字,大约阅读时间需要 21 分钟。

亮度变换函数:

1、imadjust(f,[low_in high_in],[low_out

high_out],gamma)

gamma = 1时是线性映射,gamma < 1时高亮度区域被压缩,gamma

> 1时低亮度区域被压缩。

2、imcomplement(f)

求取图像的反片

3、对数变换

用于傅里叶频谱取值范围较大,其中高值部分占优,从而导致频谱中低亮度值可视细节丢失,通过对数变换可以将动态范围降低,从而便于我们处理,公式为:log(1

+ double(f));

4、对比度拉伸函数

公式为:g = 1 / (1 + (m ./ (double(f) +

eps)).^E),其中m是均值,E控制斜率,一般取4;

书中将以上变换整理到一个函数中,代码如下:

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, 'log', C,

CLASS) computes C*log(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 gamma transformation on

% the input image using

parameter GAM (a required input). %

% G = INTRANS(F, 'stretch', M,

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

% value for M is

mean2(im2double(F)), and the default value for E

% is 4.

%

% For the 'neg', 'gamma', and

'stretch' transformations, double

% input images whose maximum

value is greater than 1 are scaled

% first using

MAT2GRAY. Other images are converted to double

first

% using

IM2DOUBLE. For the 'log' transformation, double

images are

% transformed without being

scaled; other images are converted to

% double first using

IM2DOUBLE.

%

% The output is of the same

class as the input, except if a

% different class is specified

for the 'log' option.

% Copyright 2002-2004 R. C.

Gonzalez, R. E. Woods, & S. L. Eddins

% Digital Image Processing

Using MATLAB, Prentice-Hall, 2004

% $Revision: 1.7

$ $Date: 2003/10/13 00:45:53 $

% Verify the correct number of inputs.

error(nargchk(2, 4, nargin))

% 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 'log', convert

the

% input to the range [0, 1].

if strcmp(class(f), 'double') & max(f(:))

> 1 & ...

~strcmp(varargin{1}, 'log')

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 'log'

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 log 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);

5、直方图均衡化

histeq(f,nval),其中nval为输出图像的灰度级数,默认为64;

6、直方图规定化

直方图均衡化可以提高图像的对比度,但对于图像灰度值过于集中于0值区域时,灰度变换函数是灰度直方图的累加和的曲线,此时曲线在低值区域会出现陡峰,因此把灰度级低端过于集中的像素映射到灰度级的高端,并不能提高图像的对比度。

直方图规定化将解决该问题,期望的直方图应在灰度级有较小的几种范围,在保留图像直方图大体形状,其函数为:

histeq(f,p)

p为期望直方图,其原理和直方图均衡化一样,只不过直方图均衡化的期望直方图为等于1的直线,而直方图规定化是一个任意的曲线而已,在实际中期望直方图可以通过双峰高斯函数来估计,书中提供双峰函数的生成代码:

function p = twomodegauss(m1, sig1, m2, sig2, A1, A2, k)

%TWOMODEGAUSS Generates a two-mode Gaussian function.

% P = TWOMODEGAUSS(M1, SIG1,

M2, SIG2, A1, A2, K) generates a

% two-mode, Gaussian-like

function in the interval [0,1]. P is a

% 256-element vector normalized

so that SUM(P) equals 1. The mean

% and standard deviation of the

modes are (M1, SIG1) and (M2,

% SIG2), respectively. A1 and

A2 are the amplitude values of the

% two modes. Since the output is normalized, only the relative

% values of A1 and A2 are

important. K is an offset value that

% raises the "floor" of the

function. A good set of values to try

% is M1=0.15, S1=0.05, M2=0.75,

S2=0.05, A1=1, A2=0.07, and

% K=0.002.

% Copyright 2002-2004 R. C.

Gonzalez, R. E. Woods, & S. L. Eddins

% Digital Image Processing

Using MATLAB, Prentice-Hall, 2004

% $Revision: 1.6

$ $Date: 2003/10/13 00:54:47 $

c1 = A1 * (1 / ((2 * pi) ^ 0.5) * sig1);

k1 = 2 * (sig1 ^ 2);

c2 = A2 * (1 / ((2 * pi) ^ 0.5) * sig2);

k2 = 2 * (sig2 ^ 2);

z = linspace(0, 1, 256);

p = k + c1 * exp(-((z - m1) .^ 2) ./ k1) + ...

c2 *

exp(-((z - m2) .^ 2) ./ k2);

p = p ./ sum(p(:));

function p = manualhist

%MANUALHIST Generates a two-mode histogram interactively.

% P = MANUALHIST generates a

two-mode histogram using

% TWOMODEGAUSS(m1, sig1, m2,

sig2, A1, A2, k). m1 and m2 are the

% means of the two modes and

must be in the range [0,1]. sig1 and

% sig2 are the standard

deviations of the two modes. A1 and A2 are

% amplitude values, and k is an

offset value that raised the

% "floor" of

histogram. The number of elements in the

histogram

% vector P is 256 and sum(P) is

normalized to 1. MANUALHIST

% repeatedly prompts for the

parameters and plots the resulting

% histogram until the user

types an 'x' to quit, and then it returns

% the last histogram

computed.

%

% A good set of starting values

is: (0.15, 0.05, 0.75, 0.05, 1,

% 0.07,

0.002).

% Copyright 2002-2004 R. C.

Gonzalez, R. E. Woods, & S. L. Eddins

% Digital Image Processing

Using MATLAB, Prentice-Hall, 2004

% $Revision: 1.7

$ $Date: 2003/10/13 00:49:57 $

% Initialize.

repeats = true;

quitnow = 'x';

% Compute a default histogram in case the user quits

before

% estimating at least one histogram.

p = twomodegauss(0.15, 0.05, 0.75, 0.05, 1, 0.07, 0.002);

% Cycle until an x is input.

while repeats s = input('Enter m1, sig1, m2,

sig2, A1, A2, k OR x to quit:','s');

if s == quitnow

break

end

% Convert the input string to

a vector of numerical values and

% verify the number of

inputs.

v = str2num(s);

if numel(v) ~= 7

disp('Incorrect number of inputs')

continue

end

p = twomodegauss(v(1), v(2),

v(3), v(4), v(5), v(6), v(7));

% Start a new figure and scale

the axes. Specifying only xlim

% leaves ylim on auto.

figure, plot(p)

xlim([0 255])

end

转载地址:https://blog.csdn.net/weixin_28871097/article/details/116080269 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:龙格-库塔法(runge-kutta)matlab代码及含义,龙格-库塔法(Runge-Kutta)matlab代码及含义...
下一篇:如何访问docker内php,docker中容器之间如何访问

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月07日 12时03分06秒