OpenCV imwrite() not saving image

后端 未结 7 1259
梦毁少年i
梦毁少年i 2020-12-31 13:00

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/Desk         


        
相关标签:
7条回答
  • 2020-12-31 13:31

    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);
    
    0 讨论(0)
  • 2020-12-31 13:33

    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);
    
    0 讨论(0)
  • 2020-12-31 13:40

    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
    
    0 讨论(0)
  • 2020-12-31 13:44

    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);
    
    0 讨论(0)
  • 2020-12-31 13:52

    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);
    }
    
    0 讨论(0)
  • 2020-12-31 13:54

    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

    0 讨论(0)
提交回复
热议问题