【问题标题】:Convert images from Pylon to Opencv in c++在 C++ 中将图像从 Pylon 转换为 Opencv
【发布时间】:2022-04-14 11:23:48
【问题描述】:

我想将 Basler 相机拍摄的立体图像转换为 opencv (Mat) 格式。在下面的代码中,我已将图像转换为 opencv 格式,但在显示阶段,我无法显示图像。请指导我。 谢谢

int main(int argc, char* argv[])
{
    // The exit code of the sample application.
    int exitCode = 0;


    PylonInitialize();
    Pylon::PylonAutoInitTerm autoInitTerm;//me

    try
    {
        // Get the transport layer factory.
        CTlFactory& tlFactory = CTlFactory::GetInstance();

        // Get all attached devices and exit application if no device is found.
        DeviceInfoList_t devices;
        if (tlFactory.EnumerateDevices(devices) == 0)
        {
            throw RUNTIME_EXCEPTION("No camera present.");
        }

        CInstantCameraArray cameras(min(devices.size(), c_maxCamerasToUse));

        // Create and attach all Pylon Devices.
        for (size_t i = 0; i < cameras.GetSize(); ++i)
        {
            cameras[i].Attach(tlFactory.CreateDevice(devices[i]));

            // Print the model name of the camera.
            cout << "Using device " << cameras[i].GetDeviceInfo().GetModelName() << endl;
        }


        CGrabResultPtr ptrGrabResult;
        CImageFormatConverter formatConverter;//me
        formatConverter.OutputPixelFormat = PixelType_BGR8packed;//me
        CPylonImage pylonImage;//me

        // Create an OpenCV image
        Mat openCvImage;//me
    for (int i = 0; i < c_countOfImagesToGrab && cameras.IsGrabbing(); ++i)
        {
            cameras.RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);

            intptr_t cameraContextValue = ptrGrabResult->GetCameraContext();


#ifdef PYLON_WIN_BUILD

#endif

            // Print the index and the model name of the camera.
            cout << "Camera " << cameraContextValue << ": " << cameras[cameraContextValue].GetDeviceInfo().GetModelName() << endl;

            // Now, the image data can be processed.
            cout << "GrabSucceeded: " << ptrGrabResult->GrabSucceeded() << endl;
            cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
            cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
            const uint8_t *pImageBuffer = (uint8_t *)ptrGrabResult->GetBuffer();
            cout << "Gray value of first pixel: " << (uint32_t)pImageBuffer[0] << endl << endl;


            formatConverter.Convert(pylonImage, ptrGrabResult);//me
            // Create an OpenCV image out of pylon image
            openCvImage = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *)pylonImage.GetBuffer());//me
            if (cameraContextValue == 0)
            {

                imshow("left camera", openCvImage);
                imwrite("right_img.png", openCvImage);
            }
            else if (cameraContextValue == 1)
            {
                imshow("right camera", openCvImage);
                imwrite("right_img.png", openCvImage);

            }

            Sleep(3000);

        }
    }
    catch (const GenericException &e)
    {
        // Error handling
        cerr << "An exception occurred." << endl
            << e.GetDescription() << endl;
        exitCode = 1;
    }

    // Comment the following two lines to disable waiting on exit.
    cerr << endl << "Press Enter to exit." << endl;
    while (cin.get() != '\n');

    // Releases all pylon resources. 
    PylonTerminate();

    return exitCode;
}

【问题讨论】:

    标签: c++ opencv stereoscopy


    【解决方案1】:

    你需要创建一个窗口来显示一个opencv图像,使用:

    namedWindow("left camera", CV_WINDOW_NORMAL); 
    imshow("left camera", openCvImage);
    

    你的代码也有一些错误,我猜“right_img.png”应该在“left_img.png”中改变,否则你只会保存一张图片。

    这是多余的代码

    PylonInitialize();
    Pylon::PylonAutoInitTerm autoInitTerm;
    

    autoInitTerm 会自动调用 PylonInitialize() 和 PylonTerminate()。所以你应该删除它或删除 PylonInitialize() 和 PylonTerminate()

    【讨论】:

      【解决方案2】:

      我认为imshow 之后需要waitKey(0) 才能显示图像。

      【讨论】:

        【解决方案3】:

        在下面添加一段代码。完成后 for (size_t i = 0; i

                cameras.StartGrabbing(GrabStrategy_LatestImageOnly, GrabLoop_ProvidedByUser);
        
        

        将此添加到您的代码中。和上面的 cmets 一样删除不必要的代码。

        【讨论】:

          猜你喜欢
          • 2013-02-26
          • 1970-01-01
          • 1970-01-01
          • 2018-12-06
          • 2013-01-12
          • 2017-02-10
          • 2012-08-20
          • 1970-01-01
          • 2012-12-17
          相关资源
          最近更新 更多