C++ OpenCV imwrite() 不保存图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22369168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:03:17  来源:igfitidea点击:

OpenCV imwrite() not saving image

c++opencv

提问by herohuyongtao

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.

我正在尝试在我的 mac 上保存来自 OpenCV 的图像,并且我正在使用以下代码,但到目前为止它还没有工作。

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

Can anyone see why this might not be saving?

谁能看出为什么这可能不会节省?

回答by herohuyongtao

OpenCV does have problems in saving to JPGimages sometimes, try to save to BMPinstead:

OpenCVJPG有时在保存到图像时确实有问题,请尝试保存到BMP

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

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

另外,在此之前,请确保您的图像cvImage有效。您可以通过先显示图像来检查它:

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

回答by Pengju Zhao

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 folder2doesn'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.

我遇到了同样的问题,一个可能的原因是放置图像的目标文件夹。假设你想复制A.jpg到文件夹"C:\\folder1\\folder2\\",但实际上folder2不存在时,复制不成功(这是我的实际测试,不是官方公告)。我通过检查文件夹是否存在并在不存在的情况下创建一个文件夹来解决此问题。这里有一些代码可能有助于使用 c++ 和 boost::filesystem。可能会有所帮助。

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

回答by ton4eg

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

我还建议检查文件夹权限。即使输出文件夹没有写入权限,Opencv 也会悄悄地从 imwrite 返回,没有任何异常。

回答by hybridrules

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.

我刚刚遇到了类似的问题,加载 jpg 并尝试将其另存为 jpg。添加了这段代码,现在似乎没问题了。

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

回答by CodeBender

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

可以将以下函数放入您的代码中,以支持写出 jpg 图像以进行调试。

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

你只需要传入 animage和 a 即可filename。在该函数中,指定您希望写入并有权这样做的路径。

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

回答by Shingo H.

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

OpenCV 3.2 imwrite() 在 Windows Debug 模式下写入 jpg 文件似乎有问题。我使用这种方式而不是 imwrite()。

cv::Mat cvImage;

#ifdef DEBUG

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

#else

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

#endif

回答by akurmustafa

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.

虽然对于您的情况并非如此。如果作为参数提供给 cv::imwrite 函数的图像路径超过系统允许的最大路径长度(或可能允许的文件名长度),则可能会出现此问题。

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

对于 linux,请参阅: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

对于 Windows,请参见:https: //www.quora.com/What-is-the-maximum-character-limit-for-file-names-in-windows-10