Ask Your Question

# calculate Dynamic range of an Image

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

edit retag close merge delete

## 2 answers

Sort by » oldest newest most voted 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;
}

more

## Comments

Thanks a lot for your reply. It would be helpful and informative if you add some explanation for this. I am not sure but sometimes Histogram Calculation may help you.

more

Official site

GitHub

Wiki

Documentation

## Stats

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

Seen: 1,051 times

Last updated: Jan 13 '13