# Computer image processing experiment 2 image histogram and gray transformation

## 1, Purpose and requirements of the experiment

1. Master the concept and calculation method of image gray histogram, and write gray histogram statistical program.
2. Through the analysis of image histogram, learn to apply histogram method to solve specific problems such as image binarization.
3. Be familiar with the calculation process and application of histogram equalization.
4. Master the image gray conversion technology, and improve the visual effect by adjusting the parameters such as contrast and brightness of the image.

## 2, Experiment related knowledge

Histogram is the most basic statistical feature of image. It is the probability density function of image brightness distribution and reflects the distribution of image gray value. Histogram is the basis of many spatial domain processing technologies. Histogram operation can be effectively used for image enhancement. For example, through histogram equalization, the distribution of the image in the whole gray level range can be homogenized, that is, there are the same number of pixels on each gray level, so as to obtain better visual effect. In addition, the inherent information of histogram can also be used in other image processing applications such as image segmentation.

The gray level transformation technology can be expressed in the form of g(x,y)=T[f(x,y)], where f(x,y) is the input image, g(x,y) is the output image, and T is the operation of processing the image F. Since the value of G at (x,y) is only determined by the brightness of F at this point, T is also called a brightness or gray level transformation function, which is independent of the position coordinates (x,y), it is usually written in the following simplified form s=T ®， Where r and s represent the brightness of images F and g at corresponding points (x,y), respectively. The brightness and contrast of an image can be adjusted by gray transformation.

## 3, Experimental content

1. Write an image gray histogram statistical function my_imhist, select an image and use my_imhist displays its histogram, compares the results with the processing results of the gray histogram function imhist provided in the MATLAB image processing toolbox, and displays them in the same window.

(1) Source code

```function my_imhist( input)
y=zeros(1,256);
[row,col]=size(input);
n=row*col;
for i=1:row
for j=1:col
num=double(input(i,j))+1;
y(num)=y(num)+1;
end
end
bar(y);%Draw histogram
xlim([0,256]);
end

subplot(2,1,1);
imhist(i);
title('imnhist handle');
subplot(2,1,2);
my_imhist(i);
title('my imhist handle');
```

(2) Experimental results 2. Use the function written above_ Imhist or imhist, to estimate the radius of the pupil in the image iris.tif (in pixels).

(1) Source code

```I=imread('C:\Users\Administrator\Pictures\iris.tif');
subplot(3,1,1);
imshow(I);
title('Original image');
subplot(3,1,2);
imhist(I);
title('0 Histogram in the range of to 255');
grid;
subplot(3,1,3);
imhist(I);
title('0 Histogram in the range of to 20');
grid;
xlim([0,20]);
[count,x] = imhist(I,256);
```

(2) Experimental results  It can be seen that the gray range of the pupil is roughly distributed in 0-20. Enter count on the command line and add the number of the first 20 gray values. The result is 4471 pixels, indicating that the pupil area is 4471 pixels. According to the circular area formula, the pupil radius is about 38 pixels.

3. According to the formula on page 68 of the textbook (4.1.6) The piecewise linear gray transformation of image is realized by programming

(1) Source code

```I=imread('C:\Users\Administrator\Pictures\bottle.tif');
[m,n]=size(I);
G=zeros(m,n);
a=255/3;
b=a*2;
M=255;
c=a+20;
d=M-c;
for i=1:m
for j=1:n
if(I(i,j)>=0&&I(i,j)<a)
G(i,j)=(c/a)*I(i,j);
elseif(I(i,j)>=a&&I(i,j)<b)
G(i,j)=((d-c)/(b-a))*(I(i,j)-a)+c;
else
G(i,j)=((M-d)/(M-b))*(I(i,j)-b)+d;
end
end
end

subplot(2,2,1);
imshow(I);
title('Original image');
subplot(2,2,2);
imhist(I);
subplot(2,2,3);
imshow(uint8(G));
title('New image');
subplot(2,2,4);
imhist(uint8(G));
```

(2) Experimental results 4. Write a histogram equalization function of gray image (library function cannot be used). (the number of gray levels that can be used remains unchanged), test the example in the figure below and give the test results. (1) Source code

```I=[5 5 4 4 2 0 7;5 5 4 2 0 0 7;4 4 4 2 0 7 7;1 3 3 0 7 7 6;1 0 0 0 7 7 6;7 7 7 7 7 7 6;7 7 7 7 7 7 6];
%Get input image size M,N(size Function)
[m,n]=size(I);
%The gray level is 0-7
num=zeros(1,8);
%Calculate the number of each gray level
for i=1:m
for j=1:n
for k=0:7
if(I(i,j)==k)
num(k+1)=num(k+1)+1;
end
end
end
end
%Calculate the frequency of each gray level
for k=1:8
num(k)=num(k)/(m*n);
end
%Calculate the cumulative distribution of gray level of the original image (using cumsum Function) calculates the cumulative value of an array
sumnum=cumsum(num);
%Rounding function, take num rounding,The mapping results of gray values of old and new images are obtained
new=round(sumnum*7);
%Form a new matrix
Inew=zeros(m,n);
for i=1:m
for j=1:n
Inew(i,j)=new(I(i,j)+1);
end
end
```

(2) Experimental results Tags: MATLAB

Posted on Mon, 13 Sep 2021 18:20:52 -0400 by KevinCB