Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用C+;在OpenCV中按深度维度排序+;_C++_Opencv_Computer Vision_Mat - Fatal编程技术网

C++ 使用C+;在OpenCV中按深度维度排序+;

C++ 使用C+;在OpenCV中按深度维度排序+;,c++,opencv,computer-vision,mat,C++,Opencv,Computer Vision,Mat,我有一个多维矩阵,如何按第三维对立方体排序?使用opencv的适当功能 std::vector<int> sz = { 3,3,4 }; cv::Mat M(3, sz.data(), CV_32FC1, cv::Scalar(0)); 各国的文件: 对矩阵的每一行或每一列进行排序 因此,您不能使用它来排序三维。。。直接的 但是,您可以利用这些数据,并将其转换为二维垫。然后可以按行对其进行排序,并将结果重塑为原始形状。在这种特殊情况下,您需要将其重塑为一个包含9行4列的矩阵 voi

我有一个多维矩阵,如何按第三维对立方体排序?使用opencv的适当功能

std::vector<int> sz = { 3,3,4 };
cv::Mat M(3, sz.data(), CV_32FC1, cv::Scalar(0));
各国的文件:

对矩阵的每一行或每一列进行排序

因此,您不能使用它来排序三维。。。直接的

但是,您可以利用这些数据,并将其转换为二维垫。然后可以按行对其进行排序,并将结果重塑为原始形状。在这种特殊情况下,您需要将其重塑为一个包含9行4列的矩阵

void sort3rd(cv::Mat1f const& src, cv::Mat1f& dest, int direction)
{
    assert(src.size.dims() == 3);
    std::vector<int> original_size(src.size.p, src.size.p + 3);
    std::vector<int> new_size{ original_size[0] * original_size[1], original_size[2] };
    cv::Mat1f temp(src.reshape(1, new_size));
    cv::sort(temp, temp, cv::SORT_EVERY_ROW | direction);
    dest = temp.reshape(1, original_size);
}
各国的文件:

对矩阵的每一行或每一列进行排序

因此,您不能使用它来排序三维。。。直接的

但是,您可以利用这些数据,并将其转换为二维垫。然后可以按行对其进行排序,并将结果重塑为原始形状。在这种特殊情况下,您需要将其重塑为一个包含9行4列的矩阵

void sort3rd(cv::Mat1f const& src, cv::Mat1f& dest, int direction)
{
    assert(src.size.dims() == 3);
    std::vector<int> original_size(src.size.p, src.size.p + 3);
    std::vector<int> new_size{ original_size[0] * original_size[1], original_size[2] };
    cv::Mat1f temp(src.reshape(1, new_size));
    cv::sort(temp, temp, cv::SORT_EVERY_ROW | direction);
    dest = temp.reshape(1, original_size);
}

根据的文档,您只能对行或列进行排序,仅此而已。尽管。。。暂时将其重新塑造为一个包含9行的单通道矩阵,按行排序,然后重新塑造。那么,这(以及我在回答中描述的内容)对您有效吗?我明确地使用了
cv::Mat1f
,因为您将
cv_32FC1
作为数据类型,这让我更简洁。如果您需要的话,修改它以使其更通用应该不会太难。根据的文档,您只能对行或列进行排序,仅此而已。尽管。。。暂时将其重新塑造为一个包含9行的单通道矩阵,按行排序,然后重新塑造。那么,这(以及我在回答中描述的内容)对您有效吗?我明确地使用了
cv::Mat1f
,因为您将
cv_32FC1
作为数据类型,这让我更简洁。如果您需要的话,修改它以使其更通用应该不会太难。
#include <opencv2/opencv.hpp>

#include <numeric>

void dump(cv::Mat1f const& m)
{
    assert(m.size.dims() == 3);

    std::cout << "[ ";
    for (int r(0); r < m.size[0]; ++r) {
        for (int c(0); c < m.size[1]; ++c) {
            for (int d(0); d < m.size[2]; ++d) {
                std::cout << m.at<float>(r,c,d) << " ";
            }
            std::cout << "; ";
        }
        std::cout << "\n";
    }
    std::cout << "  ]\n";
}

void sort3rd(cv::Mat1f const& src, cv::Mat1f& dest, int direction)
{
    assert(src.size.dims() == 3);
    std::vector<int> original_size(src.size.p, src.size.p + 3);
    std::vector<int> new_size{ original_size[0] * original_size[1], original_size[2] };
    cv::Mat1f temp(src.reshape(1, new_size));
    std::cout << "Reshaped before sort\n" << temp << "\n";
    cv::sort(temp, temp, cv::SORT_EVERY_ROW | direction);
    std::cout << "Reshaped after sort\n" << temp << "\n";
    dest = temp.reshape(1, original_size);
}


int main()
{
    std::vector<int> sz{3, 3, 4};
    cv::Mat1f M(static_cast<int>(sz.size()), sz.data());

    std::iota(M.begin(), M.end(), 0.0f);

    std::cout << "Input\n";
    dump(M);

    sort3rd(M, M, cv::SORT_DESCENDING);

    std::cout << "Output\n";
    dump(M);

    return 0;
}
Input
[ 0 1 2 3 ; 4 5 6 7 ; 8 9 10 11 ;
12 13 14 15 ; 16 17 18 19 ; 20 21 22 23 ;
24 25 26 27 ; 28 29 30 31 ; 32 33 34 35 ;
  ]
Reshaped before sort
[0, 1, 2, 3;
 4, 5, 6, 7;
 8, 9, 10, 11;
 12, 13, 14, 15;
 16, 17, 18, 19;
 20, 21, 22, 23;
 24, 25, 26, 27;
 28, 29, 30, 31;
 32, 33, 34, 35]
Reshaped after sort
[3, 2, 1, 0;
 7, 6, 5, 4;
 11, 10, 9, 8;
 15, 14, 13, 12;
 19, 18, 17, 16;
 23, 22, 21, 20;
 27, 26, 25, 24;
 31, 30, 29, 28;
 35, 34, 33, 32]
Output
[ 3 2 1 0 ; 7 6 5 4 ; 11 10 9 8 ;
15 14 13 12 ; 19 18 17 16 ; 23 22 21 20 ;
27 26 25 24 ; 31 30 29 28 ; 35 34 33 32 ;
  ]