Ask Your Question
0

From cv::Mat to saving image in a given format(png,jpeg...)

asked 2017-03-16 20:29:10 -0600

fb20 gravatar image

Hi,

I am trying to work out how to save an image in a given format (PGM, PNG, JPEG,... ) once I have constructed a cv::Mat

in Header file:

uchar *m_imageData;
uchar* getImageData() const { return m_imageData; }

in class

//c_mrcI being the object that populates m_imageData variable

in main with nxi=4096 and nyi=4096:

cv::Mat *cvmat = new cv::Mat(nxi,nyi,CV_8UC4,c_mrcI->getImageData());

Once I have cvmat I would like to display the content to screen using cvImageShow for example and then save this into a given format (PGM, PNG, JPEG, ...).

Normally I would do:

IplImage *img_jpg = cvLoadImage("some.jpg");
cvNamedWindow("some.jpg", CV_WINDOW_AUTOSIZE);
cvShowImage("some.jpg", img_jpg);
cvWaitKey();

Bu this requires to already have the image some.ext already saved on disk.

So in OpenCV how do we go from cvmat to cvShowImage with and without a disk access?

FB.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-03-16 20:50:48 -0600

Tetragramm gravatar image

Please do not use the C-API.

The first tutorial explains how to do this. I suggest you take a look at it, and the other tutorials to find out how to do things the proper way. It's much easier when you don't manage your own memory.

cv::Mat cvmat(nxi, nyi, CV_8UC4, c_mrcI->getImageData());
cv::imwrite("some.jpg", cvmat);
cv::imshow("some", cvmat);
cv::waitKey();
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-16 20:29:10 -0600

Seen: 35,295 times

Last updated: Mar 16 '17