OpenCV: Could not find a writer error


I had this line of code in a program that uses OpenCV to write an image buffer to a PNG file:

cv::imwrite("foo.png", foo_mat);

I was using Visual Studio 2013 to compile and run this file. But, on running I got this error:

opencvmoduleshighguisrcloadsave.cpp:276: error: (-2) could not find a writer for the specified extension


I have found that there are two reasons that this can be caused.

The most logical reason is that OpenCV was compiled with the PNG write feature (or any other format) left out. In this case, you may have to get the OpenCV source code and compile it, making sure you choose to enable WITH_PNG or similar option enabled in CMake.

Surprisingly, I found that this error can also occur when the OpenCV library you are using has PNG compiled in. However, you may be linking a OpenCV release library (or DLL) with a compilation being done in Debug mode (or vice versa). Ensure that you are using Debug version of the library files in Debug mode and Release files in Release mode. I found that this resolved the error when I got it on Windows! 🙂

Tried with: OpenCV 2.4.9, Visual Studio 2013 and Windows 7 x64

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.