gpt4 book ai didi

android - Opencv 函数等于 matlab sortrows

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:20 25 4
gpt4 key购买 nike

我的 Android OpenCV 项目中有一个二维数组类型 Mat,我需要按照与 MATLAB sortrows 函数相同的方式对数组进行排序。我发现 Core.sort() 函数采用 Sort_Every_Row 等标志。这似乎是我想要的,但是当我使用这种方法时,我得到的输出结果与我从 MATLAB 得到的结果不同。

是否有任何其他功能或任何其他方法可以进行这种排序?我必须从头开始编写方法吗?

下面是我用来对数组 FeatureMatirx 进行排序的行:

Core.sort(FeatureMatrix, FeatureMatrix, Core.SORT_EVERY_ROW);

编辑:让我用一个简单的例子来解释:

Mat test = new Mat(4,3, CvType.CV_64FC1);
test.put(0, 0, 1);
test.put(0, 1, 2);
test.put(0, 2, 3);
test.put(1, 0, 1);
test.put(1, 1, 1);
test.put(1, 2, 1);
test.put(2, 0, 2);
test.put(2, 1, 1);
test.put(2, 2, 3);
test.put(3, 0, 1);
test.put(3, 1, 2);
test.put(3, 2, 1);

Core.sort(test, test,Core.SORT_EVERY_ROW + Core.SORT_ASCENDING);

for (int k = 0; k < test.rows(); k++) {
Log.i("Test", test.get(k, 0)[0] + " " + test.get(k, 1)[0]+ " " +test.get(k, 2)[0]);
}

上面例子的结果是这样的:

03-05 21:32:01.893: I/Test(1323): 1.0 2.0 3.0
03-05 21:32:01.893: I/Test(1323): 1.0 1.0 1.0
03-05 21:32:01.893: I/Test(1323): 1.0 2.0 3.0
03-05 21:32:01.893: I/Test(1323): 1.0 1.0 2.0

但 matlab 中的排序结果如下:

1     1     1
1 2 1
1 2 3
2 1 3

最佳答案

来自documentation :

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

因此,您还需要传递一个排序顺序标志。由于 MATLAB 的 sortrows按升序排序,传递适当的标志:

Core.sort(FeatureMatrix, FeatureMatrix, Core.SORT_EVERY_ROW + Core.SORT_ASCENDING);

关于android - Opencv 函数等于 matlab sortrows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22207679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com