图像的常用灰度变换——负片、伽马变换、对数变换、直方图均衡化、自适应灰度变换及其MATLAB实现

灰度变换是图像预处理的重要手段,常见的变换方法如下,图文并茂和大家一起学习:

%% 尝试灰度变换--伽马变换和对数变换、直方图均衡化

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)


  • 9
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Matlab伽马变换是一种图像处理技术,用于调整图像的亮度和对比度。伽马变换的目的是通过对图像的每个像素进行非线性映射,改变图像灰度级分布。 在Matlab中,可以使用不同的方法实现伽马变换。以下是三种常见的伽马变换方法: 1. 对数变换:通过将像素值取对数,并进行线性缩放,增强低灰度级,并抑制高灰度级。可以使用mat2gray和log函数实现对数变换。 2. 灰度反转:通过将图像灰度级取反,即255减去原始灰度级的值,实现图像的反转。可以使用imcomplement函数实现灰度反转。 3. Gamma变换:通过使用自定义的伽马映射函数,将输入图像灰度级映射到新的灰度级。可以使用自定义的映射函数来实现Gamma变换。 以上是几种常见的Matlab伽马变换方法,可以根据具体的需求选择合适的方法进行图像处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [数字图像处理--matlab图像反转、对数变换伽马变换、对比度拉伸详解和代码实现](https://blog.csdn.net/fcxgfdjy/article/details/130458276)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Gamma变换算法之Matlab&FPGA实现](https://blog.csdn.net/crazybingofpga/article/details/120790748)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值