Basler Pylon 4 SDK and OPENCV 2.4.9, CPylonImage to Mat
Solution 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.
Solution 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
Solution 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);
}
Eric
Updated on June 05, 2022Comments
-
Eric almost 2 years
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; } }
-
Eric almost 10 yearsThat worked as i am able now to transform the image as i need it, but i have an additional issue. The image I am getting is a repeated fragment of what it is supposed to be, what is going wrong?
-
Dani van der Meer almost 10 yearsI am not sure I understand what you mean. Maybe you can post an image?
-
Eric almost 10 yearsThis is the image I am obtaining from the program dropbox.com/s/dyeeyv0cnj4ovq1/CapturaError.JPG And this is the image I want to capture (obtained with Pylon viewer) dropbox.com/s/vnb9wcpee73ko8z/testImage.tiff
-
Dani van der Meer almost 10 yearsI don't think this is caused by the conversion to OpenCV data. It looks like some wrong bayer filter conversion. I would suggest trying other settings for the convertor and/or camera settings, or otherwise to ask Basler for support.
-
Yulia V over 9 yearsCould you explain why it is better?
-
sma over 9 yearsmay be I used wrong words. It was bit easier to do that with pylon sdk