Matlab color image histogram equalization processing line() function implementation

OK, let's not say so much, but go straight to the theme. This time, we have done two implementations respectively: histogram equalization of color images to grayscale images and histogram equalization of color images. You can watch them on demand.

1, Program source code
(1) Histogram equalization of color image to grayscale image:

%Read picture
I=imread('test.jpg');
%Convert image to grayscale image
I=rgb2gray(I);
%Display original image
figure(4),imshow(I);
%Get the length and width of the grayscale image
[M,N]=size(I);

%Do histogram equalization and set initial parameters
average=0;

%Do histogram equalization and create mapping area
A=zeros(1,256);

%Get the gray value of each point and judge its value
for k=1:256
    count=0; 
   for i=1:M   
     for j=1:N
        value=I(i,j);
        %Statistical gray value
        if value==k
            count=count+1;
        end
     end  
   end
   %Draw the image gray histogram
   figure(1),line([k,k],[0,count]);
   %Histogram equalization
   count=count/(M*N*1.0);
   average=average+count;
   %Get gray map
   A(k)=average;
   %Draw the histogram after equalization
   figure(2),line([k,k],[0,average]);
end


%Cumulative distribution rounding,+0.5 It is equal to round, that is to round
A=uint8(255.*A+0.5);
%The original image is obtained by histogram reflection
for i=1:M
    for j=1:N
        I(i,j)=A(I(i,j));
    end
end

%Display processed image
figure(3),imshow(I);

Screenshot of experimental results:

(2) Histogram equalization of color image
The histogram equalization processing of color image is the same as that of gray image. The difference is that the R, G and B layers should be processed once, that is, three times. The gray image only needs to process the gray value, which is the only difference between the two. Therefore, the principle of understanding, it is not difficult to achieve

%Read picture
I=imread('test.jpg');
figure(7),imshow(I);
%Get the length, width, and number of layers of the image (actually three, R,G,B Third floor)
[M,N,G]=size(I);
result=zeros(M,N,3);
%Get the RGB Value, and determine its value is equal to
for g=1:3
    A=zeros(1,256);
    %After each layer of processing, the parameter should be reinitialized to 0
    average=0;
    for k=1:256
        count=0;
        for i=1:M
            for j=1:N
                value=I(i,j,g);
                if value==k
                    count=count+1;
                end
            end
        end
        figure(g),line([k,k],[0,count]);
        count=count/(M*N*1.0);
        average=average+count;
        A(k)=average;
        figure(g+3),line([k,k],[0,average]);
    end
    A=uint8(255.*A+0.5);
    for i=1:M
        for j=1:N
            I(i,j,g)=A(I(i,j,g)+0.5);
        end
    end  
end
%Show treatment effect
figure(8),imshow(I);

Screenshot of experimental results:

The above is how to use the line() function to draw histogram and how to process the image through histogram processing. What I think is not enough is that there are too many for loops. The program seems to be a bit bloated. I hope that some great God can give me some advice.

Posted on Wed, 01 Jul 2020 11:19:35 -0400 by thom2002