user2780240
user2780240

Reputation:

OpenCV imwrite() not saving image

I am trying to save an image from OpenCV on my mac and I am using the following code and so far it has not been working.

cv::imwrite("/Users/nickporter/Desktop/Gray_Image.jpg", cvImage);

Can anyone see why this might not be saving?

Upvotes: 18

Views: 90983

Answers (7)

akurmustafa
akurmustafa

Reputation: 122

Although it is not true for your case. This problem may arise if the image path given as argument to the cv::imwrite function exceeds the allowed maximum path length (or possibly allowed file name length) for your system.

for linux see: https://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs

for windows see: https://www.quora.com/What-is-the-maximum-character-limit-for-file-names-in-windows-10

Upvotes: 0

Shingo H.
Shingo H.

Reputation: 11

OpenCV 3.2 imwrite() seems to have a problem to write jpg file with Windows Debug mode. I use this way instead of imwrite().

cv::Mat cvImage;

#ifdef DEBUG

IplImage image = IplImage(cvImage);
cvSaveImage("filename.jpg", &image);

#else

cv::imwrite("filename.jpg", cvImage);

#endif

Upvotes: 1

CodeBender
CodeBender

Reputation: 36620

The following function can be dropped into your code to support writing out jpg images for debugging purposes.

You just need to pass in an image and a filename for it. In the function, specify a path you wish to write to & have permission to do so with.

void imageWrite(const cv::Mat &image, const std::string filename)
{
    // Support for writing JPG
    vector<int> compression_params;
    compression_params.push_back( CV_IMWRITE_JPEG_QUALITY );
    compression_params.push_back( 100 );

    // This writes to the specified path
    std::string path = "/path/you/provide/" + filename + ".jpg";

    cv::imwrite(path, image, compression_params);
}

Upvotes: 0

ton4eg
ton4eg

Reputation: 1912

I also suggest to check folder permissions. Opencv quietly returns from imwrite without any exception even if output folder doesn't have write permissions.

Upvotes: 3

Pengju Zhao
Pengju Zhao

Reputation: 1509

I met the same problem and one possible reason is that the target folder to place your image. Suppose you want copy A.jpg to folder "C:\\folder1\\folder2\\", but in fact when folder2 doesn't exist, the copy cannot be successful(It is from my actual test, not from official announcement). And I solved this issue by checking whether the folder exists and create one folder if it doesn't exist. Here is some code may it help using c++ & boost::filesystem. May it help.

#include <boost/filesystem.hpp>  
#include <iostream>
std::string str_target="C:\\folder1\\folder2\\img.jpg";

boost::filesystem::path path_target(str_target);
boost::filesystem::path path_folder=path_target.parent_path();//extract   folder
if(!boost::filesystem::exists(path_folder)) //create folder if it doesn't exist
{
  boost::filesystem::create_directory(path_folder);
}  
cv::imwrite(str_target,input_img);

Upvotes: 11

hybridrules
hybridrules

Reputation: 11

I've just had a similar problem, loading in a jpg and trying to save it back as a jpg. Added this code and it seem to be fine now.

vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(100);

And you need to include the param in your writefile.

cv::imwrite("/Users/nickporter/Desktop/Gray_Image.jpg", cvImage, compression_params);

Upvotes: 1

herohuyongtao
herohuyongtao

Reputation: 50667

OpenCV does have problems in saving to JPG images sometimes, try to save to BMP instead:

cv::imwrite("/Users/nickporter/Desktop/Gray_Image.bmp", cvImage);

Also, before this, make sure you image cvImage is valid. You can check it by showing the image first:

namedWindow("image", WINDOW_AUTOSIZE);
imshow("image", cvImage);
waitKey(30);

Upvotes: 25

Related Questions