Skip to content

Instantly share code, notes, and snippets.

@liangfu
Last active April 27, 2016 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liangfu/6f6f6e6b61f52f69834c to your computer and use it in GitHub Desktop.
Save liangfu/6f6f6e6b61f52f69834c to your computer and use it in GitHub Desktop.
Compute quantile of input matrix for given value range from 0.0 to 1.0
/**
* compute quantile of input matrix for given value range from 0.0 to 1.0
*/
float cvQuantile(CvMat * src, float p)
{
p=MIN(1,MAX(0,p)); // eliminate overflow
int nr = src->rows, nc = src->cols;
CvMat * converted = cvCreateMat(nr,nc,CV_32F);
CvMat * reshaped = cvCreateMat(1,nr*nc,CV_32F);
CvMat * sorted = cvCreateMat(1,nr*nc,CV_32F);
cvConvert(src,converted);
memcpy(reshaped->data.ptr,converted->data.ptr,sizeof(float)*nr*nc);
cvSort(reshaped,sorted,0,CV_SORT_ASCENDING);
float retval = sorted->data.fl[cvRound(p*(nr*nc-1))];
cvReleaseMat(&converted);
cvReleaseMat(&reshaped);
cvReleaseMat(&sorted);
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment