我正在尝试计算多通道图像补丁的协方差(使用cv::calcCovarMatrix),因此我可以反过来计算像素与该补丁的马氏距离,我真的很难找到正确的选项来重塑矩阵转换成正确的格式。
例如,如果我的矩阵有 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;
}