Ask Your Question
0

calculate Dynamic range of an Image

asked Jan 10 '13

UserOpenCV gravatar image

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

Preview: (hide)

2 answers

Sort by » oldest newest most voted
1

answered Jan 14 '13

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;
}
Preview: (hide)

Comments

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

UserOpenCV gravatar imageUserOpenCV (Jan 17 '13)edit

Can you post the explanation of the code in detail?

UserOpenCV gravatar imageUserOpenCV (Feb 6 '13)edit
1

answered Jan 11 '13

Haris gravatar image

updated Jan 11 '13

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

Preview: (hide)

Question Tools

Stats

Asked: Jan 10 '13

Seen: 1,358 times

Last updated: Jan 13 '13