简体   繁体   English

OpenCV imwrite() 不保存图像

[英]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.我正在尝试在我的 mac 上保存来自 OpenCV 的图像,并且我正在使用以下代码,但到目前为止它还没有工作。

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

Can anyone see why this might not be saving?谁能看出为什么这可能不会节省?

OpenCV does have problems in saving to JPG images sometimes, try to save to BMP instead: OpenCV 有时在保存为JPG图像时确实会出现问题,请尝试保存为BMP

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

Also, before this, make sure you image cvImage is valid.另外,在此之前,请确保您的图像cvImage有效。 You can check it by showing the image first:您可以通过先显示图像来检查它:

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

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).假设你想把A.jpg复制到文件夹"C:\\\\folder1\\\\folder2\\\\" ,但实际上当folder2不存在时,复制是无法成功的(这是我的实际测试,不是官方公告) . 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.这里有一些代码可能有助于使用 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);

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 返回,没有任何异常。

I've just had a similar problem, loading in a jpg and trying to save it back as a jpg.我刚刚遇到了类似的问题,加载 jpg 并尝试将其另存为 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);

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 image and a filename for it.你只需要为它传递一个image和一个filename 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);
}

OpenCV 3.2 imwrite() seems to have a problem to write jpg file with Windows Debug mode. OpenCV 3.2 imwrite() 在 Windows Debug 模式下写入 jpg 文件似乎有问题。 I use this way instead of imwrite().我使用这种方式而不是 imwrite()。

cv::Mat cvImage;

#ifdef DEBUG

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

#else

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

#endif

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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