在MATLAB中,通过函数imadjust()进行图像灰度的调整,该函数调用格式如下:
J=imadjust( I ) 对图像I进行灰度调整
J=imadjust( I,[low_in;high_in],[low_out;high_out]) [low_in;high_in]为原图像中要变换的灰度范围,[low_out;high_out]为变换后的灰度范围
J=imadjust( I,[low_in;high_in],[low_out;high_out],gamma) 该gamma参数为映射的方式,默认值为1,即线性映射。当gamma不等于1时为非线性映射
RGB2=imadjust(RGB1,…) 该函数对彩色图像的RGB1进行调整
- 通过函数imadjust()调整灰度图像灰度范围
close all;clear all;clc;
%通过imadjust()函数调整灰度图像的灰度范围
I=imread('/Users/yangmengxue/Desktop/flower.jpeg');
**J=imadjust(I,[0.2 0.5],[0 1]);** %调整灰度范围
figure;
subplot(121),imshow(uint8(I));
subplot(122),imshow(uint8(J));
在程序中通过函数imadjust()调整灰度图像的灰度范围。原图像灰度范围为0-255,程序将小于255×0.2的灰度值设置为0,将大于255×0.5的灰度值设置为255。程序运行后输出如下:
- 通过函数imadjust()调整灰度图像的亮度
close all;clear all;clc;
%调整灰度图像的灰度和显示亮度
I=imread('/Users/yangmengxue/Desktop/flower.jpeg');
**J=imadjust(I,[0.1 0.5],[0 1],0.4);** %调整图像灰度并调高亮度
**K=imadjust(I,[0.1 0.5],[0 1],4);** %调整图像灰度并调低亮度
figure,
subplot(131),imshow(uint8(I));
subplot(132),imshow(uint8(J));
subplot(133),imshow(uint8(K));
左侧是原图,中间是调整图像灰度并加强亮色值的输出后显示的图像,右侧是调整图像灰度并加强暗色值的输出后显示的图像。
- 通过函数imadjust()对彩色图像进行增强
close all;clear all;clc;
%调整灰度图像的灰度和显示亮度
I=imread('/Users/yangmengxue/Desktop/flower.jpeg');
J=imadjust(I,[0.2 0.3 0;0.6 0.7 1],[]); %imadjust()对RGB图像进行处理
figure,
subplot(121),imshow(uint8(I));
subplot(122),imshow(uint8(J));
左侧为原图,右侧为处理后的图像,可以看到图像亮度显著增强:
- 通过函数stretchlim()和函数imadjust()进行图像增强
可以采用stretchlim()计算灰度图像的最佳输入区间,即函数imadjust(I,[low_in;high_in],[low_out;high_out])中的第二个参数,以此来实现图像增强,具体实例如下:
close all;clear all;clc;
%通过函数stretchlim()和imadjust()进行图像增强
I=imread('/Users/yangmengxue/Desktop/flower.jpeg');
M=stretchlim(I); %获取最佳区间
J=imadjust(I,M,[]); %调整灰度范围
figure,
subplot(121),imshow(uint8(I));
subplot(122),imshow(uint8(J));
- 用函数imcomplement()进行灰度图像的反转变换
灰度图像的反转变换,将灰度值为0的像素值转换为255,将灰度值为255的像素值转换为0,将灰度值为x的像素值转换为255-x。通过灰度反转,能够增强暗色背景下的白色或灰色细节信息。
代码如下:
close all;clear all;clc;
%利用函数imcomplement()实现灰度图像灰度反转
I=imread('/Users/yangmengxue/Desktop/flow.jpeg');
J=imcomplement(I); %实现灰度反转
figure,
subplot(121),imshow(uint8(I));
subplot(122),imshow(uint8(J));