Преобразование изображений из Pylon в Opencv на С++

Я хочу преобразовать стереоизображения, снятые камерами 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;
}

person ahyan    schedule 29.04.2017    source источник


Ответы (2)


Вам нужно создать окно для отображения изображения 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().

person Valentin B    schedule 27.07.2017

Я думаю, что waitKey(0) требуется после imshow для отображения изображения.

person Curiosity    schedule 11.07.2017