图像矢量点

时间:2012-07-25 13:47:08

标签: c++ opencv

我正在尝试计算多通道图像补丁的协方差(使用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
  1. 这是cv :: calcCovarMatrix的正确格式吗?
  2. .reshape()需要哪些参数才能实现此目的?
  3. 代码中的一个例子:

    #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;
    }
    

1 个答案:

答案 0 :(得分:0)

未经测试,但查看文档和calcCovarMatrix()实现,您应该执行类似

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

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

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

您可以查看opencv/modules/core/src/matmul.cpp第2097行

中的代码
相关问题