Patrick.SE
Patrick.SE

Reputation: 4564

cv::Mat header information changes when using a reference

I'm declaring a

cv::Mat imageMatrix(bitmapEntry->GetHeight(), bitmapEntry->GetWidth(), ConvertToCVType(bitmapEntry), imageBytes);
cv::Mat rightImageMat;
cv::Mat leftImageMat;
SplitCVMatrix(imageMatrix, leftImageMat, rightImageMat);

I get 2160 rows and 7680 cols which is expected.

When I pass this matrix to a function by reference the matrix's structure is different:

OVOpenCVFilter::SplitCVMatrix(cv::Mat &matrix, cv::Mat &leftSplitMatrix, cv::Mat &rightSplitMatrix)

The rows are equal to 2 the step to 7680 and the cols to 2160.

I'm not receiving the same structure that I passed. Seems like I'm getting what at's the address, what should I deal to pass my matrix correctly?

Upvotes: 1

Views: 651

Answers (2)

mevatron
mevatron

Reputation: 14021

This is what I have:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

void printFromReference(Mat& src)
{
    cout << "Rows: " << src.rows << endl;
    cout << "Cols: " << src.cols << endl;
    cout << "Step: " << src.step << endl;
    cout << "Size: " << src.rows*src.cols*src.elemSize1() << endl;
}

int main(int argc, char** argv)
{
    Mat bigImage(2160, 7680, CV_8UC1);

    cout << "Before: " << endl;
    cout << "Rows: " << bigImage.rows << endl;
    cout << "Cols: " << bigImage.cols << endl;
    cout << "Step: " << bigImage.step << endl;
    cout << "Size: " << bigImage.rows*bigImage.cols*bigImage.elemSize1() << endl;

    cout << endl << "After: " << endl;
    printFromReference(bigImage);

    return 0;
}

This produces the following output:

Before: 
Rows: 2160
Cols: 7680
Step: 7680
Size: 16588800

After: 
Rows: 2160
Cols: 7680
Step: 7680
Size: 16588800

So, I'm inclined to agree with SSteve...something else you're doing isn't working correctly... If your step size seems incorrect in the SplitCVMatrix function, then you need to make sure your ConvertToCVType function telling the constructor the appropriate number of channels to create. This is just a guess though; some code would be helpful :)

Upvotes: 0

SSteve
SSteve

Reputation: 10738

I wrote this code:

void SplitCVMatrix(cv::Mat &matrix, cv::Mat &leftSplitMatrix, cv::Mat &rightSplitMatrix) {
    printf("rows: %d, columns: %d, step: %ld", matrix.rows, matrix.cols, matrix.step[0]);
}

int main(int argc, const char * argv[]) {

    cv::Mat imageMatrix(2160, 7680, CV_8UC3, cv::Scalar(128, 128, 128));
    cv::Mat rightImageMat;
    cv::Mat leftImageMat;
    SplitCVMatrix(imageMatrix, leftImageMat, rightImageMat);

    return 0;
}

and got this output:

rows: 2160, columns: 7680, step: 23040

So I don't agree with your assertion that the original imageMatrix is constructed correctly (or else there is something happening in SplitCVMatrix that you aren't showing us). You need to look more closely at the values being passed to the imageMatrix constructor.

Upvotes: 1

Related Questions