0
votes

Here is the matlab code i'm trying to reproduce in openCV 2.4.11

A = [1 1; 1 1]; 
B = [2 2; 2 2];
C = [3 3; 3 3];
D = cat(3, A, B,C)
D= imresize(D, [100 50], 'nearest');

This is what I have tried so far. but as far as I understand this isn't the same 3 D matrix, openCV just appends the matrices one next to the other.

cv::Mat a(2,2,CV_64F);
cv::Mat b(2,2,CV_64F);
cv::Mat c(2,2,CV_64F);
for (int i = 0; i < 4; i++)
{
    a.at<double>(i) = 1;
    b.at<double>(i) = 2;
    c.at<double>(i) = 3;
}

cv::Mat d;
std::vector<cv::Mat>tempVec;
tempVec.push_back(a);
tempVec.push_back(b);
tempVec.push_back(c);

cv::hconcat(tempVec,d);

I have also tried

cv::Mat d = cv::Mat::zeros(2,2,CV_64FC3);
for (int w = 0; w < 2; w++)
{
    for (int h = 0; h < 2; h++)
    {
        d.at<cv::Vec3d>(w,h)[0] = a.at<double>(w,h);
        d.at<cv::Vec3d>(w,h)[1] = b.at<double>(w,h);
        d.at<cv::Vec3d>(w,h)[2] = c.at<double>(w,h);
    }
}    

can you help please? please see that I need to use imresize in the next line of matlab, so the solution should be cv::Mat

will cv::Mat with cv::Vec3f do the trick? or is there a better way to concat 3 matrices into a 3D matrix?

update: I changed cv::hconcat(tempVec,d); into cv::merge(tempVec, d);

and add this print to file

ofstream myfile;
myfile.open("C:\\Users\\gdarmon\\Desktop\\OpenCV_TNR.txt");
myfile << d;
myfile.close();

here is my output

[1, 2, 3, 1, 2, 3;
  1, 2, 3, 1, 2, 3]
1

1 Answers

1
votes

You're pretty close. I would use cv::merge. In fact, its very purpose is to merge single channel matrices (2D) into one multi-channel matrix (3D).

As such, instead of cv::hconcat, try just:

cv::merge(tempVec, d);