Ask Your Question

How to save image data in shared pointer

asked 2019-11-21 10:43:59 -0600

saurabh162 gravatar image

updated 2019-11-21 10:44:42 -0600

Dear Developers,

Whether there is any way I can save image data from Mat object to "std::shared_ptr<char> newFramePointer".

For example I read image as follows : Mat newFrame = cv::imread("NewFrame.png");

and now how do i save data in newFrame into a shared pointer "newFramePointer"

Many Thanks for Help :)

edit retag flag offensive close merge delete


what do you need it for ?

who will own the data in which step of your program ?

will it need copies, and when ?

berak gravatar imageberak ( 2019-11-21 10:58:45 -0600 )edit

@berak thank you very much for your fast reply.

Actually, one module of our OpenCV software will be developed by another company and they want me to pass newFrame image to their software module in following format:

Void PartnerComanyModule (std::shared_ptr<char>& newFrame_dat, int typeOfnewFrame, cv::Size src_size.widthOfNewFrame, cv::Size src_size.heigthOfNewFrame);

So that later in their module they can reconstruct image using following instruction.

cv::Mat src(src_size, type, (void*)src_data.get());

So can you please inform me how can I convert image in Mat object to std::shared_ptr<char>& newFrame_dat ?

Please inform me if you need any other information from me.

Many thanks :)

saurabh162 gravatar imagesaurabh162 ( 2019-11-22 03:44:05 -0600 )edit
sturkmen gravatar imagesturkmen ( 2019-11-22 10:51:00 -0600 )edit

i just deleted the prev. answer, because it was wrong.

never thought about shared_ptr deleting objects.

can you take a look at ?

std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object. The object is destroyed and its memory deallocated when either of the following happens:

    the last remaining shared_ptr owning the object is destroyed;
    the last remaining shared_ptr owning the object is assigned another pointer via operator= or reset(). 

The object is destroyed using delete-expression or a custom deleter that is supplied to shared_ptr during construction.
berak gravatar imageberak ( 2019-11-22 12:45:42 -0600 )edit

you have 2 concurring refcounted pixel containers, and that's a terrible idea

berak gravatar imageberak ( 2019-11-22 13:30:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-11-23 03:01:43 -0600

berak gravatar image

updated 2019-11-23 09:48:27 -0600

one module of our OpenCV software will be developed by another company

the short answer:

please try to convince them to use cv::Mat, not a shared_ptr.

the long one:

you might try something like this:

Mat src = ...
shared_ptr<char> p( static_cast<char>( );
PartnerCompanyModule( p, src.type(), src.cols, src.rows );

but now youre in trouble, since both Mat and shared_ptr want to delete the data !

once you use a shared_ptr, it tries to take ownership of the data.

you could again hack it:

src.addref(); // so the Mat won't  delete it
shared_ptr<char> p( static_cast<char>( );

but seriously, that's terrible.

edit flag offensive delete link more



Many many thanks @berak for your answer ..I will try to convine them ...and will again update you with final solution we have taken ..

saurabh162 gravatar imagesaurabh162 ( 2019-11-25 03:49:58 -0600 )edit

Question Tools

1 follower


Asked: 2019-11-21 10:43:59 -0600

Seen: 1,942 times

Last updated: Nov 23 '19