Haris
Haris

Reputation: 14053

Copy Mat data to another Mat

I need to copy my image data from one Mat another Mat. My code look like below

Mat src; // Source image
Mat res(1024,768,CV_8UC3); //Same width and height as source
uchar *dest=src.data;
res.data=dest;

But I am getting distorted destination image. Is it my coding problem?

Thanks in advance!

Upvotes: 2

Views: 1798

Answers (1)

ArtemStorozhuk
ArtemStorozhuk

Reputation: 8725

But I am getting distorted destination image. Is it my coding problem?

If width and height is the same than it seems that problem is with number of channels (number of bytes per pixel). Try to change CV_8UC3 to CV_8UC1.

And also your code doesn't copy data, it copies pointers. Read documentation about memcpy.

Actually you should use method clone of cv::Mat:

// returns deep copy of the matrix, i.e. the data is copied
    Mat clone() const;

Upvotes: 6

Related Questions