Ask Your Question

How to convert Mat <-> UMat back-and-forth

asked 2018-12-10 12:28:35 -0500

peter_cz gravatar image

updated 2018-12-10 15:18:26 -0500

I'd like to know how to convert UMat image onto Mat and back-and-forth. I do calculation on input image which I read/write

UMat img,gray; 
imread("lena.jpg", 1).copyTo(img); 
cvtColor(img, gray, COLOR_BGR2GRAY);  
imwrite("lena_gray.jpg", gray);

I have OpenCV 3.4.1. and after imwrite() call I got

Segmentation fault (core dumped)

There is a bug #11176 of segmentation fault if UMat saved by imwrite() call So I'm thinking to convert to Mat and after then to save. How to convert UMat to Mat back-and-forth? Or is there any other solution to write UMat image instead?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2018-12-10 23:29:41 -0500

berak gravatar image

you probably should update your opencv to some later version, like 3.4.4 (which has the mentioned fix)

in the meantime, you have to copy back to a cv::Mat to save it:

UMat gray = ...
Mat m = gray.getMat(ACCESS_READ);
imwrite("lena_gray.jpg", m);
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-12-10 12:28:35 -0500

Seen: 1,970 times

Last updated: Dec 10 '18