Ask Your Question
0

calculate Dynamic range of an Image

asked 2013-01-10 08:21:01 -0600

UserOpenCV gravatar image

How to calculate Dynamic range of an Image using Opencv? What is the value of good dynamic range?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-01-13 23:01:22 -0600

You can compute dynamic range by below code.

void bhCalcHist(const IplImage* srcImage,CvMat* hist,const IplImage* maskImage)
{
    float* pHist = hist->data.fl;
    for (int i=0; i < 256;i++)
        pHist[i] = 0;

    if (maskImage)
    {
        for (int i=0; i < srcImage->height;i++)
        {
            byte* maskP = (byte*) maskImage->imageData + maskImage->widthStep * i;
            byte* srcP = (byte*) srcImage->imageData + srcImage->widthStep * i;
            for (int j=0; j < srcImage->width;j++)
                if (maskP[j] != 0)
            {
                pHist[srcP[j]]++;
            }

        }
    }
    else 
    {
        for (int i=0; i < srcImage->height;i++)
        {
            byte* srcP = (byte*) srcImage->imageData + srcImage->widthStep * i;
            for (int j=0; j < srcImage->width;j++)
            {
                pHist[srcP[j]]++;
            }

        }
    }
}

CvMat* bhGetHist(const IplImage* srcImage,const IplImage* maskImage)
{
    CvMat* resultHist = cvCreateMat(1,256,CV_32FC1);
    bhCalcHist(srcImage,resultHist,maskImage);
    return resultHist;
}

void  bhGetDynamicRange2(const IplImage* srcImage,const IplImage* maskImage, float lowPercent,float highPercent,unsigned char& minRange,unsigned char& maxRange )
{
    CvMat* hist = bhGetHist(srcImage,maskImage);

    float* pHist = hist->data.fl;
    int totalCount;
    if (maskImage != 0 )
        totalCount = cvCountNonZero(maskImage);
    else totalCount = srcImage->width * srcImage->height;

    int lowPercentCount = cvRound( totalCount * lowPercent /100);
    int highPercentCount = cvRound( totalCount * highPercent /100);


    int minRng = 0;
    float curCount = 0;
    int size = 256;


    while ((minRng < size) && (curCount < lowPercentCount))
    {
        curCount += (pHist[minRng]<0)?0:pHist[minRng];
        minRng++;
    }

    int maxRng = size-1;

    curCount = 0;
    while ((maxRng >= 0) && (curCount < highPercentCount))
    {
        curCount += (pHist[maxRng]<0)?0:pHist[maxRng];
        maxRng--;
    }
    if (minRng > maxRng)
    {
        minRng = maxRng;
    }


    minRange = minRng;
    maxRange = maxRng;



    cvReleaseMat(&hist);


}

int main ()
{
    unsigned char minRng ;
    unsigned char maxRng ;

    bhGetDynamicRange2(srcImage,tempMask,0.05,0.05,minRng,maxRng);

    return 0;
}
edit flag offensive delete link more

Comments

Thanks a lot for your reply. It would be helpful and informative if you add some explanation for this.

UserOpenCV gravatar imageUserOpenCV ( 2013-01-17 05:28:29 -0600 )edit

Can you post the explanation of the code in detail?

UserOpenCV gravatar imageUserOpenCV ( 2013-02-06 00:09:30 -0600 )edit
1

answered 2013-01-11 06:07:27 -0600

Haris gravatar image

updated 2013-01-11 06:50:44 -0600

I am not sure but sometimes Histogram Calculation may help you.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-01-10 08:21:01 -0600

Seen: 1,323 times

Last updated: Jan 13 '13