Ask Your Question

How to convert cv::Mat* to cv::Mat?

asked 2019-07-16 01:16:05 -0600

updated 2019-07-16 02:43:50 -0600

Hi all,
How to convert cv::Mat* to cv::Mat by C++ in opencv-4.1.0?
I'm using some third party SDK that return the result as cv::Mat*.
How do can I convert it to cv::Mat to manipulate by opencv.

edit retag flag offensive close merge delete


WHY is there a cv::Mat* in your code (it shouldn't be !) ?

please show us, so we get a better idea, what to do.

also please look up "how to dereference c++ pointers", it's a very basic c++ topic, and you should have it covered before venturing into more complicated things, like computer-vision.

berak gravatar imageberak ( 2019-07-16 02:50:34 -0600 )edit

some third party SDK that return the result as cv::Mat*

RED FLAG here. unless it's open src you wouldn't know, whether to delete it in the end or not.

again, please show us some code (function signatures, etc)

berak gravatar imageberak ( 2019-07-16 03:01:16 -0600 )edit

Thanks @berak,
Actually it is not in my code, Third party sdk is response their result as cv::Mat* .
According by their document, OpenCV can be used in case if OpenCV library is available.

cv::Mat resultImage = result.image.mat(); 
 //IDE warning: no suitable constructor exists to convert from "cv::Mat *" to "cv::Mat"

I will check with third party vendor again. thanks again.

MinChanSike gravatar imageMinChanSike ( 2019-07-16 03:08:50 -0600 )edit

5 posts already, and we still don't know, which sdk you're talking about (link ?) what result.image.mat(); might be, exactly, constraints of it's usage (null pointers ? pointer ownage ?), etc.

berak gravatar imageberak ( 2019-07-16 03:18:11 -0600 )edit

Well that's remarkably un-informative. I think it's just returning a pointer to the internal cv::Mat, so don't delete it. But it copies a cv::Mat you use to construct it, with an optional bool.

Oh dear, it actually stores a cv::Mat* as it's member variable. Eww.

Tetragramm gravatar imageTetragramm ( 2019-07-17 23:14:13 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2020-01-27 11:00:14 -0600

h22 gravatar image

cv::Mat* is pointer to the cv::Mat. Believing that much, the trivial assignments should work:

  cv::Mat x;
  cv::Mat *xp = &x;
  cv::Mat y = *xp;

Beware simple assignment does not copy the buffer, so you must be sure that Mat object referenced by that pointer lives at least as long as you y does. If you are not sure, create a complete copy of it:

cv::Mat y = xp->clone();

It is really strange to see pointers to cv::Mat, normally should not be required. I would not recommend, but when working with the third party code it may be no choices.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-07-16 01:16:05 -0600

Seen: 1,618 times

Last updated: Jan 27 '20