“This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

preface

Hello! Friend!!!

Thank you very much for reading haihong’s article, if there are any mistakes in the article, please point out ~

 

Self-introduction ଘ(੭, ᵕ)੭

Nickname: Haihong

Tag: programmer monkey | C++ contestant | student

Introduction: because of C language to get acquainted with programming, then transferred to the computer major, had the honor to get some national awards, provincial awards… Has been confirmed. Currently learning C++/Linux/Python

Learning experience: solid foundation + more notes + more code + more thinking + learn English well!

I. Experiment name

Image gray transformation and histogram correction

2. Purpose of the experiment

1. Familiar with MATLAB software. 2. Master the principle and mathematical operation of gray scale transformation and histogram correction. 3. Realized grayscale transformation and histogram correction of pictures by programming in MATLAB environment.

3. Experiment content

1. For a given picture, in MATLAB software programming to achieve different degrees of gray transformation of the picture. 2. For a given picture, in MATLAB software programming to achieve different degrees of histogram correction of the picture.

4. Experimental instruments and equipment

Win10 64-Bit computer MATLAB2017a

5. Experimental principle

Gray level transformationLinear transformation: When FA >1, the contrast of the output image will increase; When FA <1, the contrast of the output image will decrease. When Fa=1 and Fb is not zero, the gray values of all pixels move up or down, making the whole image darker or brighter. Fa<0, the dark area becomes brighter, and the bright area becomes darker. Logarithmic transformation: Can enhance the details of the darker parts of an image, used to expand the darker pixels in a compressed high-value image. Exponential transformation: Enhances the detail of the lighter parts of an image. Take opposite: light becomes dark, dark becomes bright. Histogram equalization step 1, calculate the probability of each gray level 2, calculate the gray accumulation probability 3, the accumulation probability multiplied by Leng-1 and then take the whole 4, update the gray level specific solution: Using the cycle, calculate the cumulative probability, gray level is 0, the cumulative probability is 0.02, gray level is 1, 0.02+0.05=0.07… Multiply by 7 and round to get the final gray levelHistogram SML, GML mapping

Step 1. Calculate the accumulation probability of the original histogram and the specified histogram respectively. 2. Get the updated data

The specific problem solving Respectively using cycle regulation and calculated the original figure cumulative probability, SML mapping rules, see the original cumulative histogram of 0.1 first, and then the histogram cumulative to find the most close to the value, is 0.3, so the grayscale change from 0 to 1, and so on GML mapping rules, see the cumulative histogram 0.3 first, Then find the nearest one in the original cumulative histogram, which is 0.3, and the corresponding gray level is 2, so the gray level before 2 becomes 1, and so on

Six, the experiment process and code

Gray scale transformation 1) Linear transformation

t=imread('t1.jpg'Figure) t1 = im2double (t) (1) the subplot (2, 2, 1), imshow (t1), title ('original'Figure (2), subplot (2, 2, 1), imhist (t1), title ('original'Figure (1) subplot(2,2,2),imshow(t2),title('fa = 1.25 fb = 0'Figure (2), subplot (2,2,2), imhist (t2), title ('fa = 1.25 fb = 0'Figure (1) subplot(2,2,3),imshow(t2),title('fa = 0.5 fb = 0'Figure (2), subplot (2, 2, 3), imhist (t2), title ('fa = 0.5 fb = 0'Figure (1) subplot(2,2,4),imshow(t2),title('fa=-1 fb=255'Figure (2), subplot (2, 4-trichlorobenzene), imhist (t2), title ('fa=-1,fb=255') 2) t=imread('t1.jpg')
t1=double(rgb2gray(t))
t2=log(1 + t1)/t3 = 0.033log(1 + t1)/t2 = 0.092 uint8 (t2) t3 = uint8 (t3) subplot,3,1 (2), imshow (t), title ('original'Subplot (2 and 4), imhist (t), title ('original') subplot 31 (2), imshow (t2), title ('r = 0.033'Subplot (2,3,5), imhist (t2), title ('r = 0.032'Subplot (2 filling), imshow (t3), title ('r = 0.092'Subplot (2,3,6), imhist (t3), title ('r = 0.092') 3) t=imread('t1.jpg') t1 = double (rgb2gray (t)) t2 = 1.4. ^ t3 (t1 * 0.09) - 1 = 1.4. ^ (t1 * 0.03) - 1 t2 = uint8 (t2) t3 = uint8 (t3) Subplot (2,3,1), imshow (t), title ('original'Subplot (2 and 4), imhist (t), title ('original'31) subplot (2), imshow (t2), subplot (2,3,5), imhist (t2), subplot filling (2), imshow (t3), Subplot (2,3,6),imhist(t3)'a1.jpg'T1 =im2double(rgb2gray(t)) fa=-1,fb=255 t2=fa.*t1+fb/255 subplot(2,2,1),imshow(t1),title(rgb2gray(t))'original'Subplot (2,2,2), imhist (t1), title ('original'Subplot (2, 2, 3), imshow (t2), title ('invert'Subplot (2, 4-trichlorobenzene), imhist (t2), title ('invert')
Copy the code

Histogram equalization

H= imread('a1.jpg'); % Judge whether it is a three-channel color picture, if so, gray itiflength(size(H))>2 H=rgb2gray(H); M *n [m,n]=size(H); % generate a 256-row matrix p=zeros(1,256); %find(H== I) is to find the coordinates of the point with gray scale of I in the image matrix % because the matrix starts from 1, so p(I +1)fori=0:255 p(i+1)=length(find(H==i))/(m*n); End subplot (2, 2, 1); imshow(H); title('original'); Subplot (2,2,2); % display histogram of original image bar(0:255,p,'b');  
title('Original histogram'); % Use cyclic accumulation probability value s= Zeros (1,256);for i=1:256  
     forj=1:i s(i)=p(j)+s(i); A =round(s*255); b=H; Update the grayscale of the original imagefori=0:255 b(find(H==i))=a(i+1); End subplot (2, 2, 3); imshow(b) title('Equalized image'); % Statistics updated probabilityfori=0:255 GPeq(i+1)=sum(p(find(a==i))); End subplot (2, 4-trichlorobenzene); bar(0:255,GPeq,'b'); title('Equalized histogram'); GML, SML mapping t=imread('a1.jpg'M *n [m,n]=size(t); %n_1 here is the number of pixels in each gray scale, which will be transformed into probability N_1 = Zeros (1,256); % Count the pixel number of each gray scalefor i=1:m
forj=1:n n_1(t(i,j)+1)=n_1(t(i,j)+1)+1; N_1 =n_1/m/n; % the probability of calculating each gray level %p_1 The cumulative probability of recording the original image p_1= Zeros (1,256); %p_1 The probability of calculating the original cumulative histogramfor i=1:256
forj=1:i p_1(i)=p_1(i)+n_1(j); End end %n_2 specifies the grayscale probability distribution of the histogram N_2 = [zeros (1, 50), 0.1, zeros (1, 50), 0.2, zeros (1, 50), 0.3, zeros (1, 50), 0.2, zeros (1, 20), 0.1, zeros (1), 30, 0.1]. %p_2 records specify the cumulative probability of the histogram p_2= Zeros (1,256); % calculate the cumulative probability of the specified histogramfor i=1:256
forj=1:i p_2(i)=p_2(i)+n_2(j); Data_1 =zeros(1,256); % datA_1 SML mapping tablefor i=1:256
min=abs(p_1(i)-p_2(1));
for j=2:256
ifabs(p_1(i)-p_2(j))<min min=abs(p_1(i)-p_2(j)); data_1(i)=j-1; Data_2 =zeros(1,256); end end data_2=zeros(1,256); %data_2 Records the GML mapping tablefor i=1:256
if n_2(i)~=0
tem=1;
min=abs(p_2(i)-p_1(1));
for j=2:256
ifabs(p_2(i)-p_1(j))<min min=abs(p_2(i)-p_1(j)); tem=j; end end data_2(tem)=i-1; End end % convert the data_2 obtained above to the target arrayfor i=1:256
if data_2(i)==0
for j=i:256
if data_2(j)~=0
tem=data_2(j);
break; end end data_2(i)=tem; End End % Using SML, GML mapping table, convert the original image t2=t; t3=t; % conversion algorithmfor i=1:m
for j=1:n
t2(i,j)=data_1(t(i,j)+1);
end
end
for i=1:m
forj=1:n t3(i,j)=data_2(t(i,j)+1); Subplot (3,2,1),imshow(t),title('original') the subplot (3,2,2), imhist (t), title ('original') the subplot (3, 2, 3), imshow (t2), title ('SML') the subplot (3, 4-trichlorobenzene), imhist (t2), title ('SML') the subplot (3,2,5), imshow (t3), title ('GML') the subplot (3,2,6), imhist (t3), title ('GML')
Copy the code

7. Experimental results and analysis

Figure 1 original

Gray level transformation

FIG. 2 Linear transformation

FIG. 3 Histogram of linear transformationFIG. 4 Logarithmic transformationFIG. 5 Exponential transformationTake the figure 6

Gray scale transformation analysis linear transformation: when FA >1, the contrast of the output image will increase; When FA <1, the contrast of the output image will decrease. When Fa=1 and Fb is not zero, the gray values of all pixels move up or down, making the whole image darker or brighter. Fa<0, the dark area becomes brighter, and the bright area becomes darker. Logarithmic transformation: Can enhance the details of the darker parts of an image, used to expand the darker pixels in a compressed high-value image. Exponential transform: Enhances the detail of the brighter part of an image, used to expand the brighter pixels in a compressed high-value image. Inversely: in sharp contrast to the original image, the dark area becomes light area, and the light area becomes dark area.

Histogram equalization

GML, SML mapping

8. Experiment summary and experience

In this experiment, I learned to use some simple mathematical functions for digital image processing, such as simple linear transformation, logarithm, exponential transformation, etc., using these simple operations, you can carry out some processing of the image, to achieve a certain effect. At the beginning, the concept of image processing was not very strong. Through this study, I learned that image processing is nothing more than processing some pixels in the picture, and the value represented by pixels is the gray value of the image. In histogram equalization and GML, SML mapping, we found that we can use the knowledge of C++ to solve the problem. Of the two mapping methods, GML is the most difficult, and some effort has been made here.

Attached: SML-GML algorithm verification

Histogram specification

Principle: histogram specification is to transform the original gray histogram into the desired histogram through a gray image function. Generally speaking, the grayscale of the original image ranges from 0 to 255, and its distribution is random. In some cases, we may need some specific grayscale values. For example, we only need grayscale values of 0, 3, 40, 240, 255. So going from the original image to the image we need can be understood as a specification of the image.

Specific example: the left figure is the gray histogram of the original image, the right figure is the figure we need (the need here refers to the need for gray scale from the original 0~7 into 1, 3, 6 provisions, the ordinate of the image will change, here only need the abscissa match on the line!) The specified image (the abscissa is the same as the specified, and the ordinate will change) :

  1. The cumulative histograms of the original image and the specified image are calculated respectively
  2. Use SML or GML to map gray values
  3. The gray value of the original image is converted by the updated mapping table

After reading, should still not understand? So direct actual combat one! The problem is shown in the picture below:

  1. Here is directly given by the original image gray level of the pixel number, in order to get the straight side we need accumulative figure (actually are cumulative probability values, of course, also can need not probability, with the pixel points directly, is look larger Numbers), need to work out the probability of the original image gray level (the proportion of the total number of pixels), and then calculate the accumulative value 2. According to the specified histogram given, the same method is used to calculate the cumulative histogram, as shown in Figure 3 below. The results were obtained using SML mapping rules
  2. Using the mapping change table, update the original image

Tip:

Here may not understand the SML mapping rules, I talk about your understanding of simple (oneself can baidu) : SML is single mapping, here we focus on the original accumulation histogram and accumulative histogram, so-called SML, is from the beginning of the original accumulation histogram, within the prescribed accumulative histogram seeking and his closest to the value of the and the grey value of it into their own. In particular, the original first cumulative probability is 0.19, the provisions of accumulative histogram, the most close to its is 0.15, so the original image gray level 0 to 3, the second accumulative probability is 0.44, the most close to accumulate histogram of 0.35, so the change from 1 to 4, by the same token, the graph traversal in turn out the probability of accumulative histogram, Find the nearest value in the specified cumulative histogram, and finally change the gray level.

With SML rules, GML rules are easier to understand (GML rules are harder to program) :

In SML, we traverse the original cumulative histogram in turn and find the one closest to us in the specified cumulative histogram. In GML(group mapping), it becomes successively from the specified accumulated histogram, compared with the original accumulated histogram, also find the nearest value, gray transformation, but here the transformation rules have changed. For example, from the figure above, we can look from 0.15 of the specified accumulated histogram, and the value in front of 0 is actually not needed to be looked at. Then we can find the value closest to 0.15 from the original accumulated histogram, which is 0.19. Then 0.19 corresponds to the gray value in front of the corresponding gray value of 0.15 in the specified accumulated histogram: 3; The second view is that 0.35 of the specified accumulated histogram is close to 0.44 of the original image, so 1 of the original image becomes 4 (assuming that the gray value of the original image corresponding to 0.44 is 4, then it becomes 4 between 0 and 4 of the previous one). 1 0 0 2 0 3 0 0 4 0 0 6 Target array of GML mapping change rule is: 1 2 2 2 3 3 3 4 4 4 4 6 6 The number in front of the 1 becomes 1 and the number in front of the 1 becomes 2 and the number in front of the 2 becomes 3 and the number in front of the 2 becomes 4 and the number in front of the 4 becomes 6, which is simply 0 and becomes the nearest number that is not 0. (here the algorithm itself needs to master, that is, how to change from the original array to the target array, here first give a C++ verification algorithm)

#include<iostream> using namespace std; Int main() {int t[8]={2,0,1,0,3,0,0,6}; int i,j; int tem; int flag=0; for (i=0; i<8; i++) { if(t[i]==0) { for(j=i; j<8; j++) { if(t[j]! =0) { tem=t[j]; break; } } t[i]=tem; } } for(i=0; i<8; i++) cout<<t[i]<<" "; return 0; }Copy the code

If you still don’t understand SML and GML rules? That oneself according to the following two kinds of picture understanding again understand. MATLAB actual combat principle understand, must be to start the actual combat ah! Here I’ll start with the entire code:

T =imread('a1.jpg') % obtain the length and width of the image, used to calculate the total pixel, i.e. M *n [m,n]=size(t); %n_1 here is the number of pixels in each gray scale, which will be transformed into probability N_1 = Zeros (1,256); % statistics each gray scale of pixels for I = 1: m for j = 1: n n_1 (t (I, j) + 1) = n_1 (t (I, j) + 1) + 1; N_1 =n_1/m/n; % the probability of calculating each gray level %p_1 The cumulative probability of recording the original image p_1= Zeros (1,256); %p_1 Probability of calculating the original cumulative histogram for I =1:256 for j=1: I P_1 (I)= P_1 (I)+ N_1 (j); End end %n_2 specifies the grayscale probability distribution of the histogram N_2 = [zeros (1, 50), 0.1, zeros (1, 50), 0.2, zeros (1, 50), 0.3, zeros (1, 50), 0.2, zeros (1, 20), 0.1, zeros (1), 30, 0.1]. %p_2 records specify the cumulative probability of the histogram p_2= Zeros (1,256); For I =1:256 for j=1: I P_2 (I)= P_2 (I)+ N_2 (j); Data_1 =zeros(1,256); %data_1 SML mapping table for I = 1:256min = ABS (p_1(I)- P_2 (1)); for j=2:256 if abs(p_1(i)-p_2(j))<min min=abs(p_1(i)-p_2(j)); data_1(i)=j-1; Data_2 =zeros(1,256); end end data_2=zeros(1,256); Data_2 GML mapping table for I =1:256 if N_2 (I)~=0 TEM =1; min=abs(p_2(i)-p_1(1)); for j=2:256 if abs(p_2(i)-p_1(j))<min min=abs(p_2(i)-p_1(j)); tem=j; end end data_2(tem)=i-1; For I =1:256 if data_2(I)==0 for j= I :256 if data_2(j)~=0 TEM =data_2(j); break; end end data_2(i)=tem; End End % Using SML, GML mapping table, convert the original image t2=t; t3=t; % conversion algorithm for I = 1: m for j = 1: n t2 (I, j) = data_1 (t (I, j) + 1); end end for i=1:m for j=1:n t3(i,j)=data_2(t(i,j)+1); Subplot (3,2,2),imhist(t),title(' original ') subplot(3,2,2) Subplot (3, 2, 3), imshow (t2), title (' SML) subplot (3, 4-trichlorobenzene), imhist (t2), title (' SML) subplot,2,5 (3), imshow (t3), title (' GML) Subplot (3,2,6), imhist (t3), title (' GML)Copy the code

Effect:

conclusion

SML, GML two algorithms used for a day to figure out the principle, the formula on the book is really very difficult to read ah, can not go in, the method on the Internet, really is, there is no own want. In fact, the algorithm was done in a day, because the MATLAB grammar is not very familiar, took a lot of detours. Here is a summary of their own trample pits:

  • For I =1:256 c++:for(I =1; i<=256; i++)

  • “For”, “if”, and so on are terminated by “end”. The number of ends must be paired with “if” and “for”. For example, if there are three “for” and two “ifs, there must be five” ends”

  • Whether the program statement ends or not; The difference between: written; The local code area does not display the specific data, otherwise do not write, will display, recommended to write, and c++ syntax similar.

  • Print more data, step by step debugging, easy to find bugs