Original address: http://www.cnblogs.com/tiandsp/archive/2012/12/19/2825418.html
Histogram matching or histogram regulation can be used to map the histogram of the original image according to the given histogram, so that the histogram distribution of the new image is similar to the given function.
There are a total of the following steps:
1. The cumulative histogram of the given function is asked for S.
2. Calculate the cumulative histogram of the original image G.
3. For each value in S, the position of the smallest distance in G is index.
4. Find the value of the new pixel that each pixel of the original image is mapped to by index.
The code is as follows:
Clear All;close all;clc;r=127; X=-r:r+1;sigma=20;y1=exp (-((x-80). ^2)/(2*sigma^2)); Y2=exp (-((x+80). ^2)/(2*sigma^2)); y=y1+y2; % bimodal Gaussian function, any function can be%im=imread (' bg.bmp '); % matches the histogram of an image%y=imhist (IM); y=y/sum (y) ; Percent normalization, so that the function conforms to the probability distribution of sum (y) ==1 such a regular plot (y); % to be matched histogram g=[]; The cumulative histogram of the% function for i=1:256 g=[g sum (Y (1:i))]; Endimg=imread (' lena.jpg '); [ M N]=size (IMG); Hist=imhist (IMG); % pending Image histogram p=hist/(m*n); Figure;plot (p) % original histogram s=[]; % cumulative histogram of the image to be processed for i=1:256 s=[s sum (P (1:i))];endfor i=1:256 tmp{i}=g-s (i); Tmp{i}=abs (Tmp{i}); % since the nearest point is to be found, the absolute value [a index (i)]=min (Tmp{i}) is taken; % found two cumulative histogram distance nearest point Endimgn=zeros (M,n); for i=1:m for j=1:n IMGN (i,j) =index (IMG (i,j) +1)-1; % is mapped to the new grayscale endendimgn=uint8 (IMGN) by the grayscale of the original image, Figure;imshow (IMGN) Figure;plot (Imhist (IMGN)) % histogram of the new graph
The effect is as follows:
The given histogram
Original
Original histogram
Post-Transform histogram
The final result
"ZZ" matlab histogram matching