“This is the 15th 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!

Image histogram equalization

First, we need to understand what image histogram equalization is:

The gray histogram of the original image is changed from a concentrated gray range to a uniform distribution in the whole gray range. Histogram equalization is to perform nonlinear stretching on an image and redistribute image pixel values so that the number of pixels in a certain gray range is roughly the same. Histogram equalization is to change the histogram distribution of a given image into a “uniform” histogram distribution, as shown in the following figure (to be simple, it is to evenly distribute the gray level of the original image, so that 0-255 has a certain value, so that the contrast is relatively large and visually better) :Here, we can directly use histeq() and adapthisteq () functions to equalize the image

H= imread('a1.jpg'); if length(size(H))>2 H=rgb2gray(H); End subplot (3, 2, 1); imshow(H); The title (' artwork '); Subplot (3,2,2); imhist(H); Title (' original histogram '); Subplot (3, 2, 3); H1=adapthisteq(H); imshow(H1); Title ('adapthisteq '); Subplot (3, 4-trichlorobenzene); imhist(H1); Title ('adapthisteq balanced histogram '); Subplot (3,2,5); H2=histeq(H); imshow(H2); Title ('histeq balanced graph '); Subplot (3,2,6); imhist(H1); Title ('histeq balanced histogram ');Copy the code

Effect:

Of course, we can also write our own equalization function, first of all to understand the algorithm of equalization steps (I will not say more here) on the code:

H= imread('a1.jpg'); If length(size(H))>2 H=rgb2gray(H); if length(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 in the image matrix to find the gray level of I point coordinates % because the matrix is from 1 so p(I +1) for I =0:255 P (I +1)=length(find(H== I))/(m*n); End subplot (2, 2, 1); imshow(H); The title (' artwork '); Subplot (2,2,2); % Displays the histogram of the original image bar(0:255,p,'b'); Title (' original histogram '); % Use cyclic accumulation probability value s= Zeros (1,256); for i=1:256 for j=1:i s(i)=p(j)+s(i); A =round(s*255); b=H; For I = 0:255b (find(H== I))=a(I +1); End subplot (2, 2, 3); Imshow (b) title(' equalized image '); % statistics updated probability for I = 5 GPeq 0:25 (I + 1) = sum (p (find (a = = I))); End subplot (2, 4-trichlorobenzene); bar(0:255,GPeq,'b'); Title (' equalized histogram ');Copy the code

Effect:

Method 2 (copy from the big guy)

Img= imread('a1.jpg'); if length(size(Img))>2 Img=rgb2gray(Img); End % Draw the histogram of the original image [height,width]=size(Img); [counts1, x] = imhist(Img,256); counts2 = counts1/height/width; Figure (1), subplot (1, 2, 1), imshow (Img); Title (' Original image '); Subplot (1,2,2), stem (x, counts2); Title (' Original image histogram '); % Count the cumulative number of pixel values of each gray scale NumPixel = Zeros (1,256); For I = 1:height for j = 1: Width % corresponds to grayscale pixel number +1 %NumPixel subscript starts from 1, and image pixel value range is 0~255, Img(I,j) + 1) NumPixel(I,j) + 1) = Img(I,j) + 1); End end % = ProbPixel = zeros(1,256); For I = 1:256 ProbPixel(I) = NumPixel(I)/(height * width * 1.0); for I = 1:256 ProbPixel(I) = NumPixel(I)/(height * width * 1.0); Cumsum is used to calculate the CDF and map the frequency (ranging from 0.0 to 1.0) to the unsigned integer CumuPixel = cumsum(ProbPixel) of 0 to 255. * Uint8 (255.* CumuPixel + 0.5); % histogram equalization. For I = 1:height for j = 1: width Img(I,j) = CumuPixel(Img(I,j)+1); Img(I,j) = CumuPixel(Img(I,j)+1); Figure (2), subplot(1,2,1), imshow(Img); Title (' histogram equalization image '); [counts1, x] = imhist(Img,256); counts2 = counts1/height/width; Subplot (1,2,2), stem (x, counts2); Title (' Histogram of image after histogram equalization ');Copy the code

Above are the grayscale picture equalization, so how to color picture equalization? There must be a way. As we know, color pictures are nothing more than RGB three channels, as long as we equalize the three channels respectively, and then combine, the picture is color, after equalization. The code:

Img= imread('a1.jpg'); OutImg=Img; % Extract three channel information R = Img(:,:,1); G = Img(:,:,2); B = Img(:,:,3); % Equalize the pictures of the three channels respectively R = histeq(R, 256); G = histeq(G, 256); B = histeq(B, 256); OutImg(:,:,1) = R; OutImg(:,:,1) = R; OutImg(:,:,2) = G; OutImg(:,:,3) = B; Figure, subplot (1, 2, 1), imshow (Img); Title (' Original image '); ,2,2 subplot (1), imshow (OutImg); Title (' equalized result ');Copy the code

Renderings (feel more colorful, haha) :In fact, there is another way, is to convert RGB to HSV, and then equalize the code:

mg= imread('a1.jpg'); hsvImg = rgb2hsv(Img); V=hsvImg(:,:,3); [height,width]=size(V); V = uint8(V*255); NumPixel = zeros (1256); for i = 1:height for j = 1: width NumPixel(V(i,j) + 1) = NumPixel(V(i,j) + 1) + 1; End end ProbPixel = zeros(1,256); For I = 1:256 ProbPixel(I) = NumPixel(I)/(height * width * 1.0); for I = 1:256 ProbPixel(I) = NumPixel(I)/(height * width * 1.0); end CumuPixel = cumsum(ProbPixel); * Uint8 (255.* CumuPixel + 0.5); for i = 1:height for j = 1: width V(i,j) = CumuPixel(V(i,j)+1); End end V = im2double(V); end end V = im2double(V); hsvImg(:,:,3) = V; outputImg = hsv2rgb(hsvImg); Figure, subplot (1, 2, 1), imshow (Img); Title (' Original image '); ,2,2 subplot (1), imshow (outputImg); Title (' result after HSV space equalization ');Copy the code

Effect drawing (feel not as good as the previous method, this is actually to divide the picture) :