18

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?

5
  • 1
    What does 'not been working' mean? building error? runtime error? or you could not find the stored image? you could either: check the return value of this function, or display the cvImage to see if the image is correct. Commented Mar 13, 2014 at 4:48
  • When I say not working, the code is compiling fine but I see no image saved to my destination. Commented Mar 13, 2014 at 4:49
  • what's the return value of this call? could the cvImage be displayed by imshow? Commented Mar 13, 2014 at 4:51
  • I tried imshow and get a lengthy exception. Check out my question over here: answers.opencv.org/question/29895/imshow-throwing-exception-osx Commented Mar 13, 2014 at 5:30
  • it seems that your library has some problem. sorry that i don't know how to solve it in MAC. what about cvShowImage? you could write a simple c program to test load and cvShowImage. Commented Mar 13, 2014 at 5:46

7 Answers 7

25

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);
Sign up to request clarification or add additional context in comments.

6 Comments

I've been trying to save a bloody jpg image for the longest time.... Tried everything I can think of, changed to .bmp and suddenly it works. Thank you dude, bloody MVP!
I was doing the exact same, Sipty! How frustrating haha. That's a bummer jpg's are not working
I wish the solution fixed writing to a jpg instead of this band aid.
I ran into the same problem. Changed file extension to .bmp and it magically started working. OpenCV 3.4 facepalm
Very strange. Saving to png worked some time back. It suddenly stopped working. Saving to bmp works ;)
|
11

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);

Comments

3

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

Comments

1

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);

Comments

1

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

1 Comment

I have jpeg writing problem on linux with opencv imwrite() on 3.2.0 version.
0

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);
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.