# UMAT refcount error with setUseOpenCL(false)

Currently I'm rewritting some code from Mat to UMat that will hopefully run both with or without OpenCL depending on the OpenCL flag. Now I'm getting a refcount error I don't quite understand. It only shows up if setUseOpenCL is set to false.

(Since the images I work with are around 196Megapixel-RGB-images I do prefer avoiding deep copies as much as possible and I would like to understand why in the code below I'm not allowed to write into the orginal UMat.)

By the way: is there an easy way to simply convert any image (gray, bgr, bgra) to gray or even directly to CV_32FC1 or do I always have to manually check if the image is bgra or bgr or gray and then do a conversion?


main()
{
ocl::setUseOpenCL(false); //myFoo() crashes only if this is set to false.
myFoo(myimg, outImg);
} //in this function the line convertTo() crashes if the input UMat is the same as the output UMat and setUseOpenCL is set to false
void myFoo(InputArray img_ext, OutputArray img_Out)
{
UMat gray; //if I use Mat instead of UMat it doesnt crash.
img_ext.copyTo(gray); //I also tried using getUMat(), but in both cases convertTo() below crashes. cvtColor(gray, gray, COLOR_BGR2GRAY); //doesn't crash UMat gray2;
gray.convertTo(gray2, CV_32FC1); //this doesnt crash gray.convertTo(gray, CV_32FC1); //crashes with error message: OpenCV Error: Assertion failed (u->refcount == 0) in cv::StdMatAllocator::deallocate  
edit retag close merge delete

img_ext.copyTo(gray); --> You are using a CPU function to copy data from CPU memory into GPU memory. Afaik, this is impossible...

( 2017-04-26 07:21:47 -0500 )edit

Thank you for your response! The error occurs when I set OpenCL to false which - as far as I understand - will make OpenCV call the Mat functions internally so that CopyTo() should be a pure CPU to CPU copy with no GPU involved. If I set OpenCL to true and use the GPU, the whole code works fine. The copying and all lines below work fine except for the convertTo() line.

( 2017-04-26 11:00:09 -0500 )edit