简体   繁体   中英

OpenCV unable to write Images using imwrite

I am trying to write a Mat format which I have created into a jpeg file but all I'm getting is an unhandled exception. From the documentation it says that

Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function.

So I created a Mat using the codes below:

Mat watermark(5,5,CV_16U);  
imwrite("C:\\watermark.jpg",watermark);

However, I am unable to write the image into jpeg. It works fine with BMP formats, but just not JPG or any other formats. I was advised to convert it to CV_16U or 8U but it didn't work as well and I do have write permissions to the C:\\ directory.

Am I missing a step? Or is there some other way I should go about saving an image into JPG using OpenCV?

If you are using latest opencv (v3.0) or greater and windows x64 bit version some of opencv's functionality is not working correctly in debug mode... You have to run your code in release mode then everything will be run perfectly.

Issues I found with opencv3.1 and x64 bit - debug mode

  1. imwrite(filename, image) getting exception (read access violation).
  2. haarCascade.detectMultiScale(...) detects numerous amount of objects.

To fix these issues run application in release mode.

Still searching a good way to solve these problems in debug mode.

This code works correct and make/rewrite "watermark.jpg":

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

using namespace cv;

int main() {    
    Mat watermark(5,5,CV_16U);
    imwrite("C:\\watermark.jpg",watermark);
    return 0;
}

I have Win7, Wascana Eclipse(MinGW GCC compiler), Opencv 2.4.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM