将wheel图像进行直方图均衡化处理
直方图均衡化
实验目的
实验原理
1.变换前后总像素数不变
对灰度直方图的变换,本质上是通过像素点灰度级的变换来改变直方图,不会改变图像的规模,自然也就保持
前后像素总数不变。
2.某一灰度级变换后都有对应新的灰度级
对原来灰度比较集中的图像做均衡化处理,一个很自然的想法就是将像素点的灰度级进行变换,使之比较均匀
的分布在各个灰度级上。类似,但比线性拉伸复杂。
3.累积分布函数和转移函数
1.函数histeq()直接实现
实验步骤
%直接用matlab图像处理工具箱给的直方图均衡化函数histeq()
I=imread('wheel.jpg');
H=histeq(I);
figure(1)
subplot(1,2,1);imshow(I);title('Original image');
subplot(1,2,2);imshow(H);title('New image');
1
%分别画出变换前和变换后的灰度直方图
figure(2)
subplot(1,2,1);imhist(I);title('Before');
subplot(1,2,2);imhist(H);title('After');
2
2.根据直方图均衡化原理编写函数实现
%用自己编写的函数实现Histogram Equalization
I=imread('wheel.jpg');
histeq1(I);
3
附:直方图均衡化函数histeq1()
4
function newimage=histeq1(oldimage)
%自己编写的实现Histogram Equalization的函数
[R,G,B]=size(oldimage);
figure(3);subplot(1,2,1);imhist(oldimage);title('Before');
figure(3);subplot(1,2,2);imshow(oldimage);title('Original image');
oldcounts=imhist(oldimage);
n=numel(oldimage);
%概率密度函数p
p=oldcounts/n;
%累积分布函数C
C=cumsum(p);
%映射后的灰度值
newscale=uint8(255.*C+0.5);
%计算直方图均衡化后的图片
newimage=zeros(R,G,B);
for r=1:R
for g=1:G
for b=1:B
newimage(r,g,b)=newscale(oldimage(r,g,b)+1,1);
end
end
end
figure(4);subplot(1,2,1);imhist(uint8(newimage));title('After');
figure(4);subplot(1,2,2);imshow(uint8(newimage));title('New image');
end
5