OpenCV imwrite()不保存图像

时间:2014-03-13 04:36:12

标签: c++ opencv

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

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

任何人都可以看到为什么这可能无法保存?

7 个答案:

答案 0 :(得分:11)

OpenCV在保存到JPG图片时遇到问题,请尝试保存到BMP

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

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

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

答案 1 :(得分:4)

我遇到了同样的问题,一个可能的原因是放置图像的目标文件夹。假设你想要将A.jpg复制到文件夹"C:\\folder1\\folder2\\",但事实上当folder2不存在时,副本不能成功(这是我的实际测试,而不是官方公告)。我通过检查文件夹是否存在来解决此问题,并创建一个文件夹(如果它不存在)。这里有一些代码可能有助于使用c ++&提高::文件系统。愿它有所帮助。

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

答案 2 :(得分:1)

我只是遇到了类似的问题,加载了jpg并尝试将其保存为jpg。添加了此代码,现在看起来很好。

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

你需要在你的writefile中包含param。

SEVERE: Exception sending context initialized event to listener instance of class com.myapp.spring.config.MyAppContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/<NONE>]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/<NONE>]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:609)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:510)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at com.myapp.spring.config.MyAppContextLoaderListener.contextInitialized(MyAppContextLoaderListener.java:52)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4810)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/<NONE>]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
    ... 22 more

Apr 18, 2016 5:11:44 PM org.apache.catalina.core.StandardContext startInternal

答案 3 :(得分:1)

我还建议检查文件夹权限。即使输出文件夹没有写入权限,Opencv也会毫无例外地从imwrite中安静地返回。

答案 4 :(得分:0)

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

您只需传入imagefilename即可。在函数中,指定要写入的路径&amp;有权这样做。

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

答案 5 :(得分:0)

OpenCV 3.2 imwrite()似乎在使用Windows调试模式编写jpg文件时遇到问题。我用这种方式代替imwrite()。

cv::Mat cvImage;

#ifdef DEBUG

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

#else

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

#endif

答案 6 :(得分:0)

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

对于Linux,请参见:https://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs

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