Chris - 12 days ago 5
C++ Question

# Image to vector of points

I'm trying to calculate the covariance of a multichannel image patch (using cv::calcCovarMatrix), so I can in turn calculate the Mahalonobis distance of a pixel from that patch and I'm really struggling to find the right options to reshape the matrix into the right format.

For example if my matrix has 3 rows, 4 columns, and 2 channels:

``````// 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
``````

What I believe I need is to reshape the image into a shape with 3x4=12 rows and 2 columns (or its transpose):

``````// 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. Is this the correct format for cv::calcCovarMatrix?

2. What parameters do I need for .reshape() to achieve this?

An example in code:

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

Not tested, but looking at the docs and the calcCovarMatrix() implementation, you should do something like

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

or

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

It seems that calcCovarMatrix() can process both column matrices and row-matrices.

You can take a look at the code in `opencv/modules/core/src/matmul.cpp`, line 2097

Source (Stackoverflow)