Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Normally, sortIdx returns MAT of type ushort. I was trying to create a MAT of type float and display it, which was not working. Now everything works perfectly fine. My new code is :

Mat t = cvCreateMat(3,6,CV_32F);
t.setTo(1);
t.at<float>(0,0)=10;
t.at<float>(1,1)=20;
t.at<float>(2,1)=30;
t.at<float>(1,2)=5;
t.at<float>(2,4)=16;
t.at<float>(0,5)=40;
t.at<float>(0,2)=140;
cout<<t<<"\n";

Mat t1= cvCreateMat(t.rows* t.cols, 1, CV_32F);
t1 = t.reshape(1,t.rows*t.cols);


Mat ind= cvCreateMat(t.rows*t.cols,1,CV_16U);
sortIdx(t1, ind,  CV_SORT_EVERY_COLUMN | CV_SORT_DESCENDING);

for(int i=0;i<ind.rows;i++)
{
    cout<<ind.at<ushort>(i,0)<<"\t\t"<<t1.at<float>(i,0)<<"\n";
}

for(int i=0;i<ind.rows;i++)
{
    int row = ind.at<ushort>(i,0);
    int col = ind.at<ushort>(i,0);
    row = row /t.cols;
    col = col% t.cols;

    cout<<row<<"\t"<<col<<"\n";
}
getchar();
return 0;