Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Most efficient way to clear an image with C++ interface?

I'm currently porting my old OpenCV C code to the C++ interface of OpenCV 2/3 and I'm not quite sure about some equivalents for old functions. Pretty early I ran into an issue with cvZero. The only possibility I found was to set the matrix content via Mat::setTo. Now, having to be able to manage multi-channel scalars and different data types, setTo iterates through all elements of the matrix and sets them one after another while cvZero basically did a memset. Also, I read that using setTo is a lot slower compared to simply doing this:

myMat = cv::Mat::zeros( myMat.size(), myMat.type() );

Still, I'm not sure if this allocates a new matrix and frees the old one, which I also wouldn't want. Of course I could always write

memset( myMat.data, 0, myMat.size().width * myMat.size().height * myMat.depth() );

but isn't there a proper convenience function for this?

So, I am wondering what would be the recommended way for using the C++ interface, in case I just want to clear my image black.

Thanks!