Ask Your Question
0

histogram specification/fitting/matching

asked 2014-11-12 04:37:29 -0600

Acanus gravatar image

Hi everyone, I'm trying to make a histogram specification/fitting/matching to make the illumination of an image look like a model image ( the model image is the average of several image).So i want to know if OpenCV already have the function to do this? If not, can someone give me some advice on how to make the histogram specification/fitting/matching ( i want to normalize the illumination, but Histogram equalization not work well). Here is the wiki page of histogram specification/fitting/matching: http://en.wikipedia.org/wiki/Histogram_matching

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2015-08-14 12:59:04 -0600

Mat GrayHistMatching(Mat I, Mat R) {

/* Histogram Matching of a gray image with a reference*/
// accept two images I (input image)  and R (reference image)

Mat Result;     // The Result image

int L = 256;    // Establish the number of bins
if(I.channels()!=1)
{
    cout<<"Please use Gray image"<<endl;
    return Mat::zeros(I.size(),CV_8UC1);
}
Mat G,S,F; //G is the reference CDF, S the CDF of the equlized given image, F is the map from S->G
if(R.cols>1)
{
    if(R.channels()!=1)
    {
        cout<<"Please use Gray reference"<<endl;
        return Mat::zeros(I.size(),CV_8UC1);
    }


    Mat R_hist, Rp_hist; //R_hist the counts of pixels for each level, Rp_hist is the PDF of each level
    /// Set the ranges ( for B,G,R) )
    float range[] = { 0, 256 } ;
    const float* histRange = { range };
    bool uniform = true; bool accumulate = false;
    //calcHist(const Mat* images, int nimages, const int* channels, InputArray mask, OutputArray hist, int dims, const int* histSize, const float**         ranges, bool uniform=true, bool accumulate=false )
    calcHist( &R, 1, 0, Mat(), R_hist, 1, &L, &histRange, uniform, accumulate );
    //Calc PDF of the image
    Rp_hist=R_hist/(I.rows*I.cols);
    //calc G=(L-1)*CDF(p)
    Mat CDF=cumsum(Rp_hist);
    G=(L-1)*CDF;
    for(int i=0;i<G.rows;i++)
        G.at<Point2f>(i,0).x=(float)cvRound(G.at<Point2f>(i,0).x);//round G
}
else
{
    //else, the given R is the reference PDF
    Mat CDF=cumsum(R);
    G=(L-1)*CDF;
    for(int i=0;i<G.rows;i++)
        G.at<Point2f>(i,0).x=(float)cvRound(G.at<Point2f>(i,0).x);//round G
}
/// Establish the number of bins
Mat S_hist, Sp_hist; //S_hist the counts of pixels for each level, Sp_hist is the PDF of each level
/// Set the ranges ( for B,G,R) )
float range[] = { 0, 256 } ;
const float* histRange = { range };

bool uniform = true; bool accumulate = false;

//calcHist(const Mat* images, int nimages, const int* channels, InputArray mask, OutputArray hist, int dims, const int* histSize, const float**         ranges, bool uniform=true, bool accumulate=false )
calcHist( &I, 1, 0, Mat(), S_hist, 1, &L, &histRange, uniform, accumulate );

//Calc PDF of the image
Sp_hist=S_hist/(I.rows*I.cols);
//calc s=(L-1)*CDF(p)
Mat CDF=cumsum(Sp_hist);
S=(L-1)*CDF;
for(int i=0;i<S.rows;i++)
    S.at<Point2f>(i,0).x=(float)cvRound(S.at<Point2f>(i,0).x);//round S

F=Mat::zeros(S.size(),CV_32F);
int minIndex=-1;
double T,min=100000;
for(int i=0;i<S.rows;i++)
{
    for(int j=0;j<G.rows;j++)
    {
        T=abs(double(S.at<Point2f>(i,0).x)-double(G.at<Point2f>(j,0).x));
        if (T==0)
        {
            minIndex=j;
            break;
        }
        else
            if(T<min)
            {
                minIndex=j;
                min=T;
            }
    }
    F.at<Point2f>(i,0).x=(float)minIndex;
    minIndex=-1;
    min=1000000;
}
uchar table[256]; 
for(int i=0;i<256;i++)
{
    table[i]=(int)F.at<Point2f>(i,0).x;
}
Result= ScanImageAndReduceC(I, table); 

return Result;

}

edit flag offensive delete link more
0

answered 2015-01-28 09:26:48 -0600

Hi I'm trying to do the histogram specification/matching as you. I'm trying to work with the HSV space color for the color images but with any result. Have you do it? Or have you any suggestions? Thank you.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-11-12 04:37:29 -0600

Seen: 2,891 times

Last updated: Nov 12 '14