我正在尝试计算多通道图像补丁的协方差(使用cv::calcCovarMatrix),因此我可以依次从该补丁中计算像素的Mahalonobis distance,而我真的在努力寻找正确的选项来将矩阵重塑为正确的形状格式。

例如,如果我的矩阵有3行,4列和2个通道:

// Channel 1:
1 2 3 4
5 6 7 8
9 0 1 2

// Channel 2:
99 98 97 96
95 94 93 92
91 90 89 88

我认为我需要将图像重塑为3x4 = 12行2列(或其转置)的形状:
// Desired result:
1  2  3  4  5  6  7  8  9  0  1  2
99 98 97 96 95 94 93 92 91 90 89 88
  • 这是cv::calcCovarMatrix的正确格式吗?
  • .reshape()我需要什么参数才能实现?

  • 代码示例:
    #include <opencv2/opencv.hpp>
    int main(int argc, char* argv[])
    {
        // Construct channel 1
        cv::Mat_<float> channel1 = (cv::Mat_<float>(3, 4) << 1.0, 2.0, 3.0, 4.0,
                                                             5.0, 6.0, 7.0, 8.0,
                                                             9.0, 0.0, 1.0, 2.0);
        std::cout << "Channel 1: " << std::endl;
        std::cout << channel1 << std::endl;
    
        // Construct channel 2
        cv::Mat_<float> channel2 = (cv::Mat_<float>(3, 4) << 99.0, 98.0, 97.0, 96.0,
                                                             95.0, 94.0, 93.0, 92.0,
                                                             91.0, 90.0, 89.0, 88.0);
        std::cout << "Channel 2: " << std::endl;
        std::cout << channel2 << std::endl;
    
        // Merge together
        std::vector<cv::Mat> stack;
        cv::Mat merged;
        stack.push_back(channel1);
        stack.push_back(channel2);
        cv::merge(stack, merged);
        std::cout << "Merged:" <<std::endl;
        std::cout << merged << std::endl;
    
        // Reshape
        cv::Mat reshaped = merged.reshape(0,1).reshape(1); // <----Need help with this line
        std::cout << "Reshaped:" <<std::endl;
        std::cout << reshaped << std::endl;
    
        return 0;
    }
    

    最佳答案

    未经测试,但查看docs和calcCovarMatrix()的实现,您应该执行类似的操作

    cv::Mat reshaped = merged.reshape(1,1);
    

    要么
    cv::Mat reshaped = merged.reshape(1,3*4);
    

    似乎calcCovarMatrix()可以处理列矩阵和行矩阵。

    您可以看一下opencv/modules/core/src/matmul.cpp 2097行中的代码

    10-07 16:52