Matlab-计算直方图+直方图均衡

数字图像处理基础实验-计算直方图+直方图均衡

Problem Statement(问题描述)

1.Histograms are the basis for numerous spatial domain processing techniques, such as enhancement, compression, segmentation, and description. The focus of this exercise is on obtaining, plotting, and using histograms for image enhancement.
2.Complete 2 missions:
(1)Count the histogram of an intensity image. Ou can use the function HIST_INT to read an intensity image and display its histogram .The syntax of function HIST_INT should be : hist_int(filename).
写一个hist_int(filename)计算图像直方图。
(2)Histogram equalization. You can use function HIST_EQL to read an image, perform histogram equalization and display the results.In HIST_EQL, HIST_COUNT is called to count the histogram of the input matrix.
实现直方图均衡

Procedure(算法步骤)

(A)HIST_INT( )&HIST_COUNT( )
(1)HIST_INT( ) : HIST_INT reads an image, calls HIST_COUNT to count the histogram of a matrix, and outputs the histogram of the image.
(2)HIST_COUNT( ) : Initialize array H to get image size. Iterate over the image with the array subscript +1.
(B)HIST_EQL( )
(1)Read an image, evaluate its histogram and store it in the array H. Let Havg be its average value.
(2)Set R = 0, Hint = 0.
(3)For Z = 0 to L do:
Begin
(4)Set left(Z) = R and add H(Z) to Hint.
(5)While Hint is greater than Havg do:
Begin
(6)Subtract Havg from Hint and increment R.
End
(7)Set right(Z) = R and define the value of new(Z) according to the rule used:
For rule 1 set new(Z) to the average of left(Z) and right(Z).
For rule 2 set new(Z) to the difference right(Z)-left(Z).
For Rule 3 new(Z) is left undefined)
End
(8)For all pixels P of the image do:
Begin
(9)If left(f§) equal right(f§, then set the new value of the pixel P to left(f§).
(10)Else
If Rule1 is used, set the value to new(f§).
If Rule2 is used, choose a point at random from the interval [0,new(f§)., add its value to left(f§), and use the result for the new value of P.
If Rule3 is used, compute the average of the neighborhood of P. If it exceeds right(Z), use right(Z) as the new value. If it is below left(Z), use left(Z) as the new value. Otherwise use the average as the new value.
End
(11)End of Algorithm

Source code(Matlab) and results

(A)HIST_INT( )&HIST_COUNT( )
(1)HIST_INT( )

function [H] = HIST_INT(img_String)
img_A=imread(img_String);
H=HIST_COUNT(img_A);
bar(1:256,H);
end

(2)HIST_COUNT( )

function [H] = HIST_COUNT(img_A)
for i=1:256
       H(i)=0; %初始化这个H(g),读出来的直方图
end
[m,n]=size(img_A);
for i=1:m
       for j=1:n
              g=img_A(i,j);
              H(g+1)=H(g+1)+1;
       end
end
end

(3)Results
Test:
Figure 1:Test1.jpg
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
As can be seen from the figure, the darker the picture is, the gray order value of the histogram tends to 0. The brighter the image, the grayscale value of the histogram will be 255. The higher the image contrast, the grayscale value of the histogram.

(B)function [Newimg] = HIST_EQL(img_String)
(1)function [Newimg] = HIST_EQL(img_String)

function [Newimg] = HIST_EQL(img_String)
I=imread(img_String);
A = I;
H = HIST_COUNT(A);%直方图数组,调用了算法5.1
subplot(2,2,1),bar(1:256,H),title('Original histogram');%bar(H);
sum=0;
for i=1:256 
    sum=sum+H(i);
end
Havg=sum/256;
R=0;Hint=0;%R是随着横轴变化的中间变量,Hint是纵轴的中间变量
for z =1:256
       LeftZ=R;
       Hint=Hint+H(z);
       while Hint>Havg
             Hint_new=Hint-Havg;
             Hint=Hint_new;
             R=R+1;
             RightZ = R;
       end
       RightZ=R;
       NewZ(z)=round((LeftZ+RightZ)/2);%NewZ是现在的
end
[m,n]=size(A);
for i= 1:(m*n)
     A(i)=NewZ(A(i)-1);
end
Newimg = A;
subplot(2,2,2),imshow(I);
title('Original image');
%H2 = HIST_INT(Newimg);%直方图数组,调用了算法5.1,忘记调用直接写了
H2=HIST_COUNT(Newimg);
subplot(2,2,3),bar(1:256,H2);
title('Now the histogram')
subplot(2,2,4),imshow(Newimg);
title('Now the image');

(2)result:
The first two are the original images and the last two are histogram equalization results.
前两张是原图,后两张是直方图均衡结果。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值