Last active
April 27, 2016 09:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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