Ask Your Question
1

sortIdx seems not to work

asked 2015-07-02 22:48:23 -0600

NabeelKhan gravatar image

updated 2015-07-02 22:51:12 -0600

I am trying to sort elements of 1D mat in descending order. Code seems to work fine in python (openCV) but does not work in C++. In C++, I get zero values as output

My code (C++):

> cv::Mat temp = cvCreateMat(5,
> 1,CV_32F); temp.setTo(10);
> temp.at<float>(2, 0) = 20;
> temp.at<float>(4, 0) = 40; cv::Mat
> ind; cv::sortIdx(temp, ind, CV_SORT_DESCENDING);

Pyhton Code:

     kernel = np.ones((7,1), np.int32)  
    kernel[5]=10    
    p =cv2.sortIdx(kernel, cv2.SORT_DESCENDING+cv2.SORT_EVERY_COLUMN)

My output index array turns out to be zero with C++ code which is strange ... I tried a lot but don't know what's going on

edit retag flag offensive close merge delete

Comments

btw, please do not mix old c-api types with c++ ones, cvCreateMat() returns a CvMat*, and should be avoided.

use Mat temp(5,1,CV_32F); instead (a constructor)

berak gravatar imageberak ( 2015-07-02 23:08:03 -0600 )edit
1

your c++ version is lacking the CV_SORT_EVERY_COLUMN flag:

sortIdx(temp, ind,  CV_SORT_EVERY_COLUMN | CV_SORT_DESCENDING);
[4, 2, 3, 1, 0]
berak gravatar imageberak ( 2015-07-02 23:23:07 -0600 )edit

I am using openCV in big project , where we want the openCV MAT object to be deleted automatically.

I read in documentation that if we declare openCV MAT with cvCreateMat() function then MAT is deleted automatically once u are outside the scope ...

Will it also happen with Mat temp(5,1,CV_32F) ??

NabeelKhan gravatar imageNabeelKhan ( 2015-07-03 21:59:05 -0600 )edit

yes. cv::Mat has an automatic refcount. please use that, not CvMat* or IplImage*

berak gravatar imageberak ( 2015-07-03 22:54:03 -0600 )edit

thanks for that ... this means that MAT object (created by any constructor as discussed above) is deleted automatically .... plz correct me If I am wrong

NabeelKhan gravatar imageNabeelKhan ( 2015-07-04 06:36:06 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-07-08 15:16:27 -0600

NabeelKhan gravatar image

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;
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-07-02 22:48:23 -0600

Seen: 1,161 times

Last updated: Jul 08 '15