2012-07-25 35 views
3

我试图计算多通道图像补丁的协方差(使用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 

相信我需要的是将图像重塑与3×4 = 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()来实现这个目标?

在代码中的一个例子:

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

我想回答上面这两个问题你都可以在这里找到:docs.opencv.org你尝试了吗? – Sam 2012-07-25 14:10:27

+0

这是我点击率最高的链接之一,也是我将功能链接到的地方。我知道calcCovarMatrix的输入应该是'Samples存储为单独的矩阵或单个矩阵的行/列',我目前正试图理解http://docs.opencv.org/上的建议modules/core/doc/basic_structures.html#mat-reshape没有运气来试图满足这个要求。在文档中是否有更具体的位置,您认为我错过了? – Chris 2012-07-25 14:39:50

回答

0

没有测试,但是看文档和calcCovarMatrix()实现,你应该这样做

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

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

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

你可以看看在opencv/modules/core/src/matmul.cpp代码,行2097

+0

'.reshape(1,1)'产生我遇到过的同样的问题:1行,24列。然而,'.reshape(1,12)'确实有效,所以谢谢你。 – Chris 2012-07-25 15:06:17