“This is the 14th 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!
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 examples: The left figure is the gray histogram of the original image, and the right figure is the figure we need (the need here refers to the need for gray scale from the original 0~7 into the specified 1, 3, 6, the ordinate of the specified image will change, here only need abscissa match on the line!)The specified image (the x-coordinate is the same as the specified, the y-coordinate will change) : Implementation steps:
- The cumulative histograms of the original image and the specified image are calculated respectively
- Use SML or GML to map gray values
- 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 title is shown in the picture below:Steps to solve the problem:
- 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), then when calculating the accumulative value
- According to the given specified histogram, the same method is used to calculate the cumulative histogram, as shown in the following figure
- The results were obtained using SML mapping rules
- 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 0 4 0 0 6 The target array of GML mapping change rules is: 12 2 2 3 3 4 April 4 4 6 66: The number before 1 becomes 1, and the number between 1 and 2 becomes 2, and the number between 2 and 3 becomes 3, and the number between 3 and 4 becomes 4, and the number between 4 and 6 becomes 6. (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