【问题标题】:Basler Pylon and OpenCV, why image window is blank? [duplicate]Basler Pylon 和 OpenCV,为什么图像窗口是空白的? [复制]
【发布时间】:2022-01-12 15:57:00
【问题描述】:

我是 C++ 的初学者,但我已经使用 Python 使用 OpenCV 进行了一些 blob 检测。但是使用 C++,我什至无法将相机流传送到窗口。

相机为Basler DAA3840-45UC,平台为Win10。

谁能帮我解释为什么“imshow("Smaller", resized_down);"显示空白窗口但“Pylon::DisplayImage(1, ptrGrabResult);”正在显示相机视图?

编辑:我没有在“imshow”之后添加“waitKey”。现在可以了。

PylonInitialize();
    
    try
    {
    CBaslerUniversalInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());

    camera.MaxNumBuffer = 5;

    camera.StartGrabbing(GrabStrategy_LatestImageOnly);

    CGrabResultPtr ptrGrabResult;

    while (camera.IsGrabbing()) {
        CImageFormatConverter fc;
        fc.OutputPixelFormat = PixelType_BGR8packed;

        camera.RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);

        CImageFormatConverter converter;

        CPylonImage image;

        int scale_percent = 25;
     
        if (ptrGrabResult->GrabSucceeded())
        {
            fc.Convert(image, ptrGrabResult);

            Mat cv_img = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t*)image.GetBuffer());
            
            cv::Size sz = cv_img.size();
            int imageWidth = sz.width;
            int imageHeight = sz.height;
            int down_width = imageWidth * scale_percent / 100;
            int down_height = imageHeight * scale_percent / 100;
            Mat resized_down;
            resize(cv_img, resized_down, Size(down_width, down_height), INTER_LINEAR);
            imshow("Smaller", resized_down);  
            
#ifdef PYLON_WIN_BUILD
            Pylon::DisplayImage(1, ptrGrabResult); 
            
#endif

【问题讨论】:

  • imshow 没有waitKey -- 好像有人没有读过documentation

标签: c++ opencv computer-vision


【解决方案1】:

以下代码可帮助您作为使用 Opencv 运行 Pylon 相机 的指南。如果您可以分享您要使用的相机类型和平台,我可能会为您提供更多帮助。请相应地编辑您的问题。

注意:即使您似乎发现了问题,您共享的代码中也应该缺少行,这就是为什么我想将这个答案分享给看到这篇与问题。

#include <iostream>
#include <opencv2/opencv.hpp>
#include <pylon/PylonIncludes.h>
#include <pylon/usb/BaslerUsbInstantCamera.h>
#include <pylon/gige/BaslerGigEInstantCamera.h>

using namespace Pylon;
using namespace GenApi;
using namespace Basler_UsbCameraParams;

int main()
{
     Pylon::PylonAutoInitTerm autoInitTerm;
    CImageFormatConverter formatConverter;
    CPylonImage pylonImage;
    CGrabResultPtr ptrGrabResult;
    CBaslerUsbInstantCamera *pylon_camera;
    //CBaslerGigEInstantCamera *pyloncamera2;
    cv::Mat frame,playingFrame,cannyFrame,contoursFrame;
    cv::namedWindow("Input",0);
    //cv::namedWindow("Output",0);


    cv::RNG rng(12345);



    pylon_camera = new CBaslerUsbInstantCamera(CTlFactory::GetInstance().CreateFirstDevice());


    pylon_camera->MaxNumBuffer = 1;
    formatConverter.OutputPixelFormat= PixelType_Mono8;
    pylon_camera->StartGrabbing();
    pylon_camera->AcquisitionFrameRate=60.0;







    while (1) {
        if(pylon_camera->IsGrabbing())
        {

            pylon_camera->RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);

            if(ptrGrabResult->GrabSucceeded() && pylon_camera->IsGrabbing())
            {

                pylon_camera->ExposureAuto.SetValue(ExposureAuto_Off);
                pylon_camera->ExposureTime.SetValue(20000);

                pylon_camera->BslBrightness.SetValue(0.0);
                pylon_camera->BslContrast.SetValue(0.0);




                const uint8_t *pImageBuffer = (uint8_t *) ptrGrabResult->GetBuffer();
                formatConverter.Convert(pylonImage, ptrGrabResult);
                frame = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC1, (uint8_t *) pylonImage.GetBuffer());




                cv::imshow("Input",frame);

                int c = cv::waitKey(1);
                if((char)c == 'c')
                    break;

            }

        }
    }


    pylon_camera->Close();
    pylon_camera->StopGrabbing();



    return 0;
}

【讨论】:

  • 感谢您的详细帮助尤努斯·铁木尔连克。我的相机是Basler DAA3840-45UC,平台是Win10。我尝试了您的代码,但现在我的相机窗口再次为空白,并且在打开小空白窗口后立即出现一些错误。它指向 IFloat.h 和第 146 行:“throw ACCESS_EXCEPTION("Feature not present (reference not valid)"); [Exception Unhandled. System.Runtime.InteropServices.SEHExceptions: "External component has throw an exception.'我知道您的代码是正确的,我正试图找出我在复制它时出错的地方,或者它与我的相机类型有关。
  • 你的相机类型也是usb类型的。如果您安装了库并正确安装它们,它应该能够运行
  • 去掉这行"pylon_camera->AutoBacklightCompensation.SetValue(0.2);"
  • 啊,谢谢你的警告,它在那里的用法也是错误的。我编辑了
猜你喜欢
  • 2014-07-28
  • 2014-09-30
  • 1970-01-01
  • 2021-12-29
  • 2022-08-07
  • 2011-12-05
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
相关资源
最近更新 更多