Basler Pylon 4 SDK and OPENCV 2.4.9, CPylonImage t

2020-06-06 04:30发布

问题:

I am currently developing a machine vision application using Basler camera acA1300-30gc. For this I am working with Basler Pylon 4 and OPENCV version 2.4.9 and some problems have shown up. I am trying to capture an image using the Pylon SDK and convert it to Mat format for further analysis. Due to processing time restrictions my goal is to avoid saving the image to the hard drive but to analyze it on-the-fly.

In the following code I try to capture the image, convert it to Mat format and display it on a new window, but the window I get is blank. I will be very grateful if some one could help me please identifying my mistake(s), or explaining how could I reach my goal in a different way. (I probably should add that the camera is properly working and that I have been able to save images to the hard drive).

Thanks.

here is my code:

    PylonAutoInitTerm autoInitTerm;

    try
    {
        CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());
        cout << "Dispositivo usado:"<<camera.GetDeviceInfo().GetModelName()<<endl;

        CGrabResultPtr ptrGrabResult;

        camera.GrabOne(500,ptrGrabResult,TimeoutHandling_ThrowException);

        if(ptrGrabResult->GrabSucceeded())
        {
            CPylonImage target;

            CImageFormatConverter converter;
            converter.OutputPixelFormat=PixelType_RGB8packed;
            converter.OutputBitAlignment=OutputBitAlignment_MsbAligned;

            converter.Convert(target,ptrGrabResult);

            Mat image(target.GetWidth(),target.GetHeight(),CV_8UC1,target.GetBuffer(),Mat::AUTO_STEP);

            if(image.empty())
            {
                cout << "No se pudo cargar la imagen Mat" << endl;
                return -1;
            }
            cout << "La imagen se presenta en la ventana *Captura*" << endl;

            namedWindow("Captura",WINDOW_AUTOSIZE);
            imshow( "Captura", image );

        }
        else
        {
            cout<<"Error: "<<ptrGrabResult->GetErrorCode()<<" "<<ptrGrabResult->GetErrorDescription()<<endl;
        }
    }

回答1:

You have to make sure that the pixel types match. In your code example you use PixelType_RGB8packed for the camera image, and CV_8UC1 as the Mat pixel type. you should use CV_8UC3 instead. Also I would use PixelType_BGR8packed instead of PixelType_RGB8packed, because BGR is compatible with Windows bitmaps. I am assuming you use Windows.



回答2:

I can't seem to be able to post a comment, but to complete the answer above:

It worked for me after replacing:

Mat image(target.GetWidth(),target.GetHeight(),CV_16UC3,target.GetBuffer(),Mat::AUTO_STEP);

by

Mat image(target.GetHeight(),target.GetWidth(),CV_16UC3,target.GetBuffer(),Mat::AUTO_STEP);

Note width for Height



回答3:

here is a better solution:

CImageFormatConverter fc;

fc.OutputPixelFormat = PixelType_BGR8packed;

CPylonImage image;

if (ptrGrabResult->GrabSucceeded())
{
fc.Convert(image, ptrGrabResult);
Mat cv_img = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3,(uint8_t*)image.GetBuffer());
imshow(src_window,cv_img);  // display the image in OpenCV image window
waitKey(1);
}


回答4:

In order to fix the conversion to OpenCV data (your image) you can copy the buffer in this way

Mat image(target.GetHeight(), target.GetWidth(), CV_8UC3);
memcpy(image.ptr(),target.GetBuffer(),3*target.GetWidth()*target.GetHeight());

It worked for me.