Digital image processing Matlab- histogram equalization and histogram (with code)

1.Objectives:

1, and concepts familiar histogram calculation method;
2, familiar with the histogram equalization process is calculated;
3, calculated and plotted image histogram, histogram equalization achieved.

2.Experiment Content:

Learn to use function imhist, histeq, bar, stem, plot, imadjust, and title, axis, set and other tools describe the image.

3.Experiment Principle:

1. Histogram: Histogram is defined a discrete function h (rk) = nkrk first k levels of gray in the interval, the number of pixels of gray levels in the image appears nk, bar stem plot can be used in three ways show,

2. The histogram equalization: Equilibrium is actually a cumulative distribution function (CDF)
Here Insert Picture Description
where rk Sk gradation value of the output image is a gray value input, nj is the number of pixels in a gradation values, n being a whole the number of pixels.

Let me illustrate with an example of how the histogram equalization

Suppose nine pixels of a picture
Here Insert Picture Description
our from the table above can be found in the original gray scale value 20 30 40 of closely spaced, is not easy to distinguish with the naked eye after equalization becomes widened 85170255 gradation value thereby increasing the contrast difference

4.Experiment Steps Result and Conlusion:

(1) Display a histogram
Here Insert Picture Description
of FIG. 1 showing a histogram
Con: seen from the figure, the whole picture is relatively dark, so focused on a gradation value of 100

(2) show the histogram bar and stem function

A histogram showing a plot function h = imhist obtained histogram; bar respectively show histograms H and stem, by adjusting the parameters, changes the display of the histogram. Shaft axis provided with maximum and minimum values (for example: axis ([0 255 0 15000 ]);), the coordinates of the display set is provided with a spacer (e.g.: set (gca, 'xtick' , 0: 50: 255))
Here Insert Picture Description
in FIG. 2 with a bar, stem, plot histogram showing
Con: where the histogram is divided into 25, and therefore not so dense FIG last, we can clearly see in particular multi-pixel value at point 100, mainly because there are concentrated and where there are concentrated to about 10 gray values of pixels are added together, the amount of data is particularly large. Set coordinates are used as a function of a number of tools in matlab.

(3) with longitudinal imadjust function adjusting image contrast, and adjusting imhist view histogram changes .
Here Insert Picture Description
3 to adjust the image contrast function with imadjust

FIG two gray image are carried out range setting processing is 0-1
for the first FIG gamma <1, the image brighter back histogram also converged in the vicinity of the high luminance value 150
for the first FIG gamma> 1, the next image becomes dark histogram is also converged in the vicinity of a high luminance value of 45
(4) histogram equalization with histeq, and view the changes before and after histogram equalization with imhist.
Here Insert Picture Description
FIG 4 is a histogram equalization histeq

Con: balancing purposes is to improve the contrast, bright brighter, darker darker, from the results, the book behind Einstein clearer, indicating contrast enhancement can help us "see" things. But treatment for people not as before, still have to explain to the appropriate treatment according to the actual situation. Also see histogram here, where the original low brightness of high brightness pixels are not equalized after almost covered the entire gray scale area, the luminance range of the original description of the image is stretched. Luminance pixels are concentrated in a high luminance region.

(5) equalizing the luminance fuzzy algorithm, the following function is obtained using the corresponding results
Here Insert Picture Description
in FIG. 5 for equalizing the luminance fuzzy algorithm

Con: Use .p addpath add the appropriate file, then ease of use fuzzysysfcn function, it can be seen very clearly the effect of blurring the image more clear and true.

5.Appendix(programs and images):

Code:

clear all;clc;
f=imread('E:\数字图像处理\程序与图像\图像库\EINSTEIN.tif');
figure(1);
subplot(1,2,1);
imshow(f);
title('EINSTEIN');
subplot(1,2,2);
imhist(f);%画出直方图
axis([0 255 0 40000]);%为直方图设置坐标轴  

figure(2);
subplot(2,2,1);
h=imhist(f,25);%这个25参量的意思是共有25个容器 就是把255个灰度值分成25个来进行统计
horz=linspace(0,255,25);%生成一个范围是0到255的向量,向量中有25个点,分别均匀排列
bar(horz,h);%y用bar方式显示直方图,h是直方图的信息 
axis([0 255 0 120000]);%设置xy轴标记的刻度 四个参数分别是x轴的起始刻度,终止刻度 轴的起始刻度,终止刻度
set(gca,'xtick',0:50:255) %gca是获得当前轴,然后在x轴上 每隔50个值标记刻度
set(gca,'ytick',0:20000:120000);%同上,不过是在y轴
title('bar 默认宽度');
subplot(2,2,2);
bar(horz,h,0.5);%y用bar方式显示直方图,h是直方图的信息 0.5这个参数是可以调整bar的宽度的信息 
axis([0 255 0 120000]);
set(gca,'xtick',0:50:255);
set(gca,'ytick',0:20000:120000);%同上
title('bar 0.5的宽度');
subplot(2,2,3);
stem(horz,h);%用杆状图绘制直方图
axis([0 255 0 120000]);
set(gca,'xtick',0:50:255)
set(gca,'ytick',0:20000:120000);%同上
title('stem');
subplot(2,2,4);plot(horz,h);%用plot绘制曲线图
axis([0 255 0 120000]);
set(gca,'xtick',0:50:255) 
set(gca,'ytick',0:20000:120000);%同上
title('plot'); 

figure(3);
subplot(2,2,1);
f1=imadjust(f,[0 1],[0 1],0.5);%f是输入的图像 第二、三个矩阵分别是变化前 变化后的范围 最后的值gamma是决定变化的函数是上凸还是下凹%大于1下凹,图像变暗,大于1上凸,图像变亮
imshow(f1);
title('gamma>1,图像变暗');%映射图像下凹,相应灰度级降低
subplot(2,2,2);
imhist(f1);
subplot(2,2,3);
f2=imadjust(f,[0 1],[0 1],2);
imshow(f2);
title('gamma<1,图像变亮');
subplot(2,2,4);
imhist(f2);

figure(4);
subplot(2,2,1);
imshow(f);
subplot(2,2,2);
imhist(f);
title('原图直方图');
subplot(2,2,3);
g=histeq(f,256);%求出原图直方图均衡后的图像中每个像素点的灰度数据
imshow(g);%显示原图直方图均衡后的图像
subplot(2,2,4);
imhist(g);%绘制出原图直方图均衡后的直方图
title('直方图均衡');  

addpath('E:\数字图像处理\程序与图像\dipum_toolbox_2.0.2(only run)');%添加相应的.p文件,便于之后使用fuzzysysfcn函数 

udark=@(z)1-sigmamf(z, 0.35, 0.5);%进行模糊处理的隶属度函数
ugray=@(z)triangmf(z, 0.35, 0.5, 0.65);
ubright=@(z)sigmamf(z, 0.5, 0.65);
udarker=@(z)bellmf(z, 0.0, 0.1);
umidgray=@(z)bellmf(z, 0.4, 0.5);
ubrighter=@(z)bellmf(z, 0.8, 0.9); 
outmf={udark; ugray; ubright};%表示输出模糊函数
rules={udarker; umidgray; ubrighter}; %表示输入
F=fuzzysysfcn(outmf, rules, [0,1]); %使用
fuzzysysfcn函数及逆行模糊处理
z=linspace(0, 1, 256);%产生0-1范围内的指定数量点数,等分为256份,并返回一个行向量。
T=F(z);
I=f;
g=intrans(I, 'specified', T);
figure(5), imshow(g);%模糊处理的结果
figure(6), imshow(I);%原图
figure(7),plot(T);%灰度分布

result:
Here Insert Picture Description

Here Insert Picture Description

Published 17 original articles · won praise 12 · views 1665

Guess you like

Origin blog.csdn.net/weixin_42784535/article/details/105121416