Convert images from Pylon to Opencv in c++

好久不见. 提交于 2020-01-17 06:29:14

问题


I want to convert stereo images captured by Basler cameras to opencv (Mat) format. In the below code i have converted images to opencv format, but in show stages, i can not show the images. please guide me. Thanks

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

回答1:


You need to create a window to display an opencv image into, use :

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

There is also a few mistakes in your code, i guess "right_img.png" should be change in "left_img.png", otherwise you will save only one image.

And this is redundant code

PylonInitialize();
Pylon::PylonAutoInitTerm autoInitTerm;

autoInitTerm is automatically calling PylonInitialize() and PylonTerminate(). So you should remove it or remove PylonInitialize() and PylonTerminate()




回答2:


I think a waitKey(0) is required after the imshow to display the image.



来源:https://stackoverflow.com/questions/43695811/convert-images-from-pylon-to-opencv-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!