sortIdx不适用于C ++

时间:2021-07-22 07:44:04

I am trying to sort elements of 1D openCV Mat in descending order. My python code seems to work fine using sortIdx function of openCV. But code is not working in C++, where my index list contains zero values.

我试图按降序排序1D openCV Mat的元素。我的python代码似乎使用openCV的sortIdx函数工作正常。但是代码在C ++中不起作用,我的索引列表包含零值。

C++ code

cv::Mat temp = cv::Mat(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);

Python 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 with C++ code

我的输出索引数组结果是零,C ++代码很奇怪......我尝试了很多,但不知道C ++代码是怎么回事

2 个解决方案

#1


0  

For C++ version, according to the documentation,

对于C ++版本,根据文档,

The function sortIdx sorts each matrix row or each matrix column in the ascending or descending order. So you should pass two operation flags to get desired behaviour.

函数sortIdx按升序或降序对每个矩阵行或每个矩阵列进行排序。所以你应该传递两个操作标志来获得所需的行为。

So the correct way is

所以正确的方法是

cv::sortIdx(temp, ind, CV_SORT_EVERY_COLUMN + CV_SORT_DESCENDING);

#2


0  

It's working now

它现在正在运作

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

通常,sortIdx返回类型为ushort的MAT。我试图创建一个类型为float的MAT并显示它,这是无效的。现在一切都很好

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;

#1


0  

For C++ version, according to the documentation,

对于C ++版本,根据文档,

The function sortIdx sorts each matrix row or each matrix column in the ascending or descending order. So you should pass two operation flags to get desired behaviour.

函数sortIdx按升序或降序对每个矩阵行或每个矩阵列进行排序。所以你应该传递两个操作标志来获得所需的行为。

So the correct way is

所以正确的方法是

cv::sortIdx(temp, ind, CV_SORT_EVERY_COLUMN + CV_SORT_DESCENDING);

#2


0  

It's working now

它现在正在运作

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

通常,sortIdx返回类型为ushort的MAT。我试图创建一个类型为float的MAT并显示它,这是无效的。现在一切都很好

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;