灰度变换是图像预处理的重要手段,常见的变换方法如下,图文并茂和大家一起学习:
%% 尝试灰度变换--伽马变换和对数变换、直方图均衡化
clc,clear,close all
f = imread('C:\Users\HS\Desktop\duck.jpg');
figure;imshow(f);title('原图');
f = rgb2gray(f); % 彩色图像的灰度化
figure;imshow(f);title('原图灰度化');
灰度化之后开始正式的操作,
1.负片
就是把灰度级的黑与白对调,有时候会从视觉上方便观察和寻找目标
g0 = imadjust(f,[0,1],[1,0],1);
figure,imshow(g0);title('负片图像');
g = imcomplement(f); % 效果同上
figure,imshow(g);title('等效的负片图像');
2.扩展区间与伽马灰度变换
g1 = imadjust(f,[0 1],[0,1],1.5);
figure,imshow(g1);title('灰度高低限不变,gamma=1.5');%变换效果不佳。。。
%开始选择灰度较高的区域做变换
%g2 = imadjust(f,[0.5,0.85],[0,1],1);%mamma=1时,线性变换,等效于下一句
g2 = imadjust(f,[0.1,0.85],[]);
figure,imshow(g2);title('扩展区间0.1~0.85,gamma变换,默认值');
imhist(g2);ylim('auto');title('gamma变换,默认1');
g3 = imadjust(f,[0.1,0.85],[],0.5);%mamma<1时,变换凸显高灰度区域
figure,imshow(g3);title('扩展区间0.1~0.85,gamma<1');
imhist(g3);ylim('auto');title('gamma变换,0.5');
g4 = imadjust(f,[0.1,0.85],[0,1],2);%mamma>1时,变换凸显低灰度区域
figure,imshow(g4);title('扩展区间0.1~0.85,gamma>1');
imhist(g4);ylim('auto');title('gamma变换,2.0');
%figure,histogram(g4);title('gamma变换,2');
光看图没感觉?是滴,人眼的直观感受其实并不可信,建议对比灰度直方图看效果
可见灰度伽马变换的效果是根据gamma改变图像灰度构成,具体效果需要修改参数具体分析了
3.自动选取灰度级的灰度变换
%% 灰度变换,stretchlim函数自动选取灰度级,...
% 自适应找到一个分割阈值向量来改变一幅图像的对比度
g5 = imadjust(f,stretchlim(f),[0 1],1.5);
figure,imshow(g5);title('自动选取灰度级,默认[0.01,0.99]');
%自动选择了低限与高限,充满底部与顶部1%的灰度值,即[0.01,0.99]
%效果不佳,选择的灰度范围太大,区分性不好,不如不用
说实话还是自己设置的情况好看一点,过程可控
4.对数变换
比伽马函数gamma>1的效果更剧烈的一种灰度变换
%% 对数变换
g7 = im2uint8(mat2gray(log(1+double(f))));figure;imshow(g7);title('对数变换');
%利用对数变换:减小动态范围,提高图像对比度
5.直方图均衡化
%% 直方图均衡化
% 利用adapthisteq函数做直方图均衡化
g8 = adapthisteq(f);
figure;imshow(g8);title('直方图均衡化');
figure,imhist(g8);ylim('auto');title('直方图均衡化');
6.主要的灰度变换结果对比与完整代码
灰度直方图的对比更明显
可见对数变换将整个灰度图像的像素灰度拔高了,伽马变换的效果根据gamma的值可分为提升或者降低图像整体灰度。
完整的matlab代码如下,可以自己调试一下,很简单很好上手
%% 尝试灰度变换--伽马变换和对数变换、直方图均衡化
clc,clear,close all
f = imread('C:\Users\HS\Desktop\duck.jpg');
figure;imshow(f);title('原图');
f = rgb2gray(f); % 彩色图像的灰度化
figure;imshow(f);title('原图灰度化');
%% 灰度变换,imadjust函数
g0 = imadjust(f,[0,1],[1,0],1);
figure,imshow(g0);title('负片图像');
g = imcomplement(f); % 效果同上
figure,imshow(g);title('等效的负片图像');
g1 = imadjust(f,[0 1],[0,1],1.5);
figure,imshow(g1);title('灰度高低限不变,gamma=1.5');%变换效果不佳。。。
%开始选择灰度较高的区域做变换
%g2 = imadjust(f,[0.5,0.85],[0,1],1);%mamma=1时,线性变换,等效于下一句
g2 = imadjust(f,[0.1,0.85],[]);
figure,imshow(g2);title('扩展区间0.1~0.85,gamma变换,默认值');
imhist(g2);ylim('auto');title('gamma变换,默认1');
g3 = imadjust(f,[0.1,0.85],[],0.5);%mamma<1时,变换凸显高灰度区域
figure,imshow(g3);title('扩展区间0.1~0.85,gamma<1');
imhist(g3);ylim('auto');title('gamma变换,0.5');
g4 = imadjust(f,[0.1,0.85],[0,1],2);%mamma>1时,变换凸显低灰度区域
figure,imshow(g4);title('扩展区间0.1~0.85,gamma>1');
imhist(g4);ylim('auto');title('gamma变换,2.0');
%figure,histogram(g4);title('gamma变换,2');
%放在一起看看
figure;subplot(221);imshow(f);title('原灰度图');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(222);imshow(g2);title('gamma=1');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(223);imshow(g3);title('gamma=0.5');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(224);imshow(g4);title('gamma=2');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
%% 灰度变换,stretchlim函数自动选取灰度级,...
% 自适应找到一个分割阈值向量来改变一幅图像的对比度
g5 = imadjust(f,stretchlim(f),[0 1],1.5);
figure,imshow(g5);title('自动选取灰度级,默认[0.01,0.99]');
%自动选择了低限与高限,充满底部与顶部1%的灰度值,即[0.01,0.99]
%效果不佳,选择的灰度范围太大,区分性不好,不如不用
% g6 = imadjust(f,stretchlim(f,0.5),[0,1],1.5);
% figure,imshow(g6);title('自动选取灰度级');
%% 对数变换
g7 = im2uint8(mat2gray(log(1+double(f))));figure;imshow(g7);title('对数变换');
%利用对数变换:减小动态范围,提高图像对比度
figure,imhist(g7);ylim('auto');title('对数变换的灰度直方图');
%% 直方图均衡化
% 利用adapthisteq函数做直方图均衡化
g8 = adapthisteq(f);
figure;imshow(g8);title('直方图均衡化');
figure,imhist(g8);ylim('auto');title('直方图均衡化');
%% 放一起看看灰度变换效果
close all;
figure;
subplot(231);imshow(f);title('原图的灰度图');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
%subplot(232);imshow(g1);title('灰度高低限不变,gamma=1.5');
subplot(232);imshow(g8);title('直方图均衡化');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(233);imshow(g7);title('对数变换');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(234);imshow(g2);title('扩展区间0.1~0.85,gamma=1');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(235);imshow(g3);title('扩展区间0.1~0.85,gamma=0.5');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(236);imshow(g4);title('扩展区间0.1~0.85,gamma=2');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
%% 比较灰度直方图变化情况
% subplot(221);histogram(f);ylim('auto');title('原图');set(gcf,'color','w');set(gca,'linewidth',1,'fontsize',16)
% subplot(232);histogram(g8);ylim('auto');title('直方图均衡化');set(gcf,'color','w');set(gca,'linewidth',1,'fontsize',16)
% subplot(233);histogram(g4);ylim('auto');title('gamma变换');set(gcf,'color','w');set(gca,'linewidth',1,'fontsize',16)
% subplot(234);histogram(g7);ylim('auto');title('对数变换');set(gcf,'color','w');set(gca,'linewidth',1,'fontsize',16)
figure;
subplot(231);histogram(f);title('原图的灰度图');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(232);histogram(g8);title('直方图均衡化');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(233);histogram(g7);title('对数变换');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(234);histogram(g2);title('扩展区间0.1~0.85,gamma=1');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(235);histogram(g3);title('扩展区间0.1~0.85,gamma=0.5');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)
subplot(236);histogram(g4);title('扩展区间0.1~0.85,gamma=2');
set(gcf,'color','w')
set(gca,'linewidth',1,'fontsize',16)