简体   繁体   English

OpenCV imwrite 不保存图像

[英]OpenCV imwrite doesn't save the image

Im using a Basler camera, and I'm trying to save the grabbed image with OpenCV.我使用的是 Basler 相机,我正在尝试使用 OpenCV 保存抓取的图像。 However, when i try to use imwrite(), I get this error:但是,当我尝试使用 imwrite() 时,出现此错误:

imwrite_('C:/Users/Uporabnik/Desktop/slika.png'): can't write data: unknown exception imwrite_('C:/Users/Uporabnik/Desktop/slika.png'):无法写入数据:未知异常

My conversion of the grabbed image:我对抓取的图像的转换:

openCvImage = Mat(image.GetHeight(), image.GetWidth(), CV_16U, (uint8_t *)image.GetBuffer());

Trying to save the image:尝试保存图像:

cv::imwrite("C:/Users/Uporabnik/Desktop/slika.png", openCvImage);

I am also using basler camera. 我也在使用basler相机。 You also need to share codes which includes basler configurations.Here is how I used basler camera to get frame in the format of opencv: 您还需要共享包含basler配置的代码。以下是我使用basler相机获取opencv格式的帧的方法:

#include <pylon/usb/BaslerUsbInstantCamera.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <pylon/PylonIncludes.h>    

using namespace std;
using namespace cv;
using namespace Pylon;
using namespace GenApi;
using namespace Basler_UsbCameraParams;

int main()
{
Mat openCvImage;
Pylon::PylonAutoInitTerm autoInitTerm;
CBaslerUsbInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
CImageFormatConverter formatConverter;
CPylonImage pylonImage;

    camera.MaxNumBuffer = 1;
    formatConverter.OutputPixelFormat= PixelType_BGR8packed;
    camera.StartGrabbing( c_countOfImagesToGrab);
    const uint8_t *pImageBuffer = (uint8_t *) ptrGrabResult->GetBuffer();
    formatConverter.Convert(pylonImage, ptrGrabResult);
    openCvImage= cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult>GetWidth(), CV_8UC3, (uint8_t *) pylonImage.GetBuffer());

      imshow("Basler Frame",openCvImage);
      waitKey(0);
      return 0;
}

But first of all you need to use "imshow" function to see image. 但是首先,您需要使用“ imshow”功能来查看图像。 If you can, the problem is in your directory part. 如果可以,则问题出在您的目录部分。 Otherwise you need to share code about initiating basler camera. 否则,您需要共享有关启动basler摄像机的代码。

Please just pay attention to the dimension of your image, you can simply print and see if the shape is correct.请注意图像的尺寸,您可以简单地打印并查看形状是否正确。 In my case, I include the batchsize dimension to the image shape by mistake, and I solve it by image = image[0].就我而言,我错误地将批量尺寸包含到图像形状中,并通过 image = image[0] 解决了它。

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

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