基于Matlab的图像灰度级修正
DIP实验1:图像灰度级修正实验目的实验内容参考代码1、调用Matlab自带函数编写2、使用自己编写的线性拉伸函数、幂律变换函数、绘制直方图函数、均衡函数实验结果DIP实验1:图像灰度级修正
实验目的
掌握常用的图像灰度级修正方法(灰度变换法和直方图均衡化),加深对直方图的理解。观察图像的增强效果,对灰度级修正前后的图像加以比较。
实验内容
1)编程实现图像的灰度变换,改变图像的输入、输出映射参数范围(线性拉伸和反比);
2)修改参数γ\gammaγ值(大于、小于、等于1),观察处理结果;
3)对图像直方图作均衡化处理,显示均衡前后的图像及其直方图。
附:可供参考的Matlab函数有imread、imadjust、imhist、stretchlim、histeq、imshow、axis
输出图像排列格式如下:
参考代码
1、调用Matlab自带函数编写
% 调用系统函数org_img = imread('lena.bmp'); %original picturefigure('NumberTitle', 'off', 'Name', '实验1:图像灰度级修正'); subplot(3,3,1);imshow(org_img);title('原图像');subplot(3,3,2);linear_img = imadjust(org_img,[0.2 0.5],[0 1]); imshow(linear_img);title('线性拉伸图像');subplot(3,3,3);inv_img = imadjust(org_img,[0 1],[1 0]); imshow(inv_img);title('反比图像');subplot(3,3,4);gmbig = imadjust(org_img,[ ],[ ],2); imshow(gmbig);title('\gamma > 1');subplot(3,3,5);gmeq = imadjust(org_img,[ ],[ ],1); imshow(gmeq);title('\gamma = 1');subplot(3,3,6);gmsmall = imadjust(org_img,[ ],[ ],0.5); imshow(gmsmall);title('\gamma < 1');subplot(3,3,7);imhist(org_img);title('原图像直方图');subplot(3,3,8);adh_img = histeq(org_img); imshow(adh_img); title('均衡后的图像'); % histeq adapthisteq ?subplot(3,3,9); imhist(adh_img);title('均衡图像的直方图');
2、使用自己编写的线性拉伸函数、幂律变换函数、绘制直方图函数、均衡函数
figure('NumberTitle', 'off', 'Name', '实验1:图像灰度级修正'); OrgImg = imread('lena.bmp'); % 原始图像读取subplot(3,3,1);imshow(OrgImg);title('原图像');subplot(3,3,2);LinearImg = LinearEnhance (OrgImg, [0.5 0.8], [0 1]); imshow(LinearImg);title('线性拉伸图像');subplot(3,3,3);InverseImg = LinearEnhance (OrgImg, [0 1], [1 0]);imshow(InverseImg);title('反比图像');subplot(3,3,4);PowerLaw1 = PowerLawTrans (OrgImg, 1, 2);imshow(PowerLaw1);title('\gamma > 1');subplot(3,3,5);PowerLaw2 = PowerLawTrans (OrgImg, 1, 1);imshow(PowerLaw2);title('\gamma = 1');subplot(3,3,6);PowerLaw3 = PowerLawTrans (OrgImg, 1, 0.5);imshow(PowerLaw3);title('\gamma < 1');subplot(3,3,7);OrgHist = Histo(OrgImg);bar(0:255, OrgHist, 'b'); title('原图像直方图');subplot(3,3,8);EquImg = Equalization(OrgImg, OrgHist);imshow(EquImg); title('均衡后的图像'); subplot(3,3,9); OrgHist2 = Histo(EquImg);bar(0:255, OrgHist2, 'b'); title('均衡图像的直方图'); % 线性拉伸函数 LinearEnhancefunction TransImg = LinearEnhance (OrgImg, RangeIn, RangeOut)% OrgImg 原始图像% RangeIn 图像的输入参数范围,a、b介于0和1之间% RangeOut 图像的输出参数范围,c、d,介于0和1之间a = RangeIn(1);b = RangeIn(2);c = RangeOut(1);d = RangeOut(2); if (a < 0 || b > 1 || c < 0 || d > 1) % 防错误输入error('变换范围输入有误');endimg = im2double(OrgImg); k = (d - c) / (b - a); % 变换函数斜率%a = min(img(:));%b = max(img(:));TransImg = ones(256);for i = 1 : 256for j = 1 : 256if (img(i,j) < a)TransImg(i,j) = c;elseif (img(i,j) >= a && img(i,j) < b)TransImg(i,j) = k * img(i,j) + c;else TransImg(i,j) = d;endendendTransImg = mat2gray(TransImg);end% 幂律变换函数 PowerLawTransfunction TransImg = PowerLawTrans (OrgImg, c, gamma)% OrgImg 原始图像img = im2double(OrgImg);TransImg = c * (img .^ gamma) ;TransImg = mat2gray(TransImg);end% 绘制直方图函数 Histofunction TransImg = Histo(Img)TransImg = zeros(1,256); for i = 0: 255TransImg(i + 1) = length(find(Img == i)) / (256 * 256); endend% 均衡函数 Equalizationfunction TransImg = Equalization(Img, p)% p原始直方图s = zeros(1,256);for i = 1 : 256 for j = 1: i s(i) = p(j) + s(i);end end a = round(s * 255);% round 四舍五入TransImg = Img;for i = 0 : 255 TransImg(find(Img == i)) = a(i+1);end end