用C ++将图像从Pylon转换为Opencv

时间:2017-04-29 12:37:53

标签: c++ opencv stereoscopy

我想将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;
}

2 个答案:

答案 0 :(得分:1)

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

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

你的代码中也有一些错误,我想&#34; right_img.png&#34;应更改为&#34; left_img.png&#34;,否则您只会保存一张图片。

这是冗余代码

PylonInitialize();
Pylon::PylonAutoInitTerm autoInitTerm;

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

答案 1 :(得分:0)

我认为var $rows = $('#list .row'); var order = ['grain', 'fruit', 'dairy']; // this is the order I'd like the divs to be in tinysort($rows, {sortFunction:function(a, b) { var rowA = $(a.elm).data('type'); var rowB = $(b.elm).data('type'); return rowA == rowB ? 0 : (rowA > rowB ? 1 : -1); }}); 后需要waitKey(0)才能显示图片。