实验二:直接灰度变换和直方图均衡
一、 代码
线性变换:LinearTransformFunc.m 分段线性变换:StretchFunc.m 对数变化:LogFunc.m 指数变换:ExponentFunc.m
LinearTransformFunc.m
function [new]= LinearTransformFunc( original, k, d ) new=original; m=size(original,1); n=size(original,2); for i=1:m for j=1:n
new(i,j)=original(i,j)*k+d; end end
new=original*k+d; end
StretchFunc.m
function [ new ] = StretchFunc(original, x1,y1,x2,y2) new = original; w = size(new, 1); h = size(new, 2); k1 = y1 / x1;
dk1 = (y2 - y1) / (x2 - x1); dk2 = (255 - y2) / (255 - x2); for i = 1 : w for j = 1 : h x = new(i, j); if x < x1
new(i, j) = k1 * x; elseif x < x2
new(i, j) = dk1 * (x - x1) + y1;
else
new(i, j) = dk2 * (x - x2) + y2; end end end
LogFunc.m
function [new] = LogFunc(original,c) new=original; m=size(original,1); n=size(original,2); for i=1:m for j=1:n
new(i,j)=c*log(double(original(i,j))+1); end end
new=c*log(double(original)+1); end
ExponentFunc.m
function [new] = ExponentFunc(original,c,d) new=original; m=size(original,1); n=size(original,2); for i=1:m for j=1:n
new(i,j)=c*original(i,j).^d; end end
new=c*original.^d; end
命令行窗口调用上述.m文件:
i=imread('C:\\Users\\Administrator\\Desktop\\shiyanyi.png'); i_gray=rgb2gray(i); I=double(i_gray)/255;
I_LinearTransform=LinearTransformFunc(I, 1, 0.01);
imshow(I_LinearTransform);
I_Stretch=StretchFunc(I,0.01,0.03,0.4,0.5); imshow(I_Stretch); I_Log=LogFunc(I,1.5); imshow(I_Log);
I_Exponent=ExponentFunc(I,1.1,2); imshow(I_Exponent); I1=imadjust(I,[],[0 1]); imhist(I); imhist(I1);
I_junhenghua=histeq(I); imshow(I)
imshow(I_junhenghua)
输出图像以及直方图如下: