How to convert IDS peak::ipl::Image or peak::core::Buffer to an opencv cv::Mat?

317 Views Asked by At

I have tried to convert peak::ipl::Image to cv::Mat but dont find a method.

This proyect is implemented with C++.

while(true){ 
   // Get buffer from device's DataStream. Wait 5000 ms. The buffer is automatically locked until it is queued again. 
   const auto buffer = dataStream->WaitForFinishedBuffer(5000); 
   const auto image = peak::ipl::Image(peak::BufferTo<peak::ipl::Image>(buffer)); 
   const auto imageProcessed = image.ConvertTo(peak::ipl::PixelFormatName::BGRa8,peak::ipl::ConversionMode::Fast); 
   //Convert 
   //cv::Mat imageMat = imageProcessed???  
   // Queue buffer again 
   dataStream->QueueBuffer(buffer); 
} 

IDS peak provie a example for QImage but it's unuse to me.

I'v tried pass image.Data() to constructor method, but ilp::Image work with uint8_t* and opencv with uchar*.

I know that QImage work well but I dont want include it only to catch the images and then convert it to Mat.

Also, I'v search for a method to convert ids buffer to cv::Mat directly, but nothing appears.

1

There are 1 best solutions below

0
Claudio On

To specify the total size of the Mat matrix, one should multiply newImage.total() by newImage.elemSize(). If the image is to be transformed into peak::ipl::PixelFormatName::BGRa8, the created image (cv::Mat) should have sizes four times larger than the original buffer.

cv::Mat newImage(height, width, CV_8UC4);
m_imageConverter->Convert(peak::BufferTo<peak::ipl::Image>(buffer), peak::ipl::PixelFormatName::BGRa8,newImage.data, newImage.total() * newImage.elemSize());
m_dataStream->QueueBuffer(buffer);
cv::imshow("", newImage);
cv::waitKey(1);

Furthermore, one can first create the ipl::Image and then pass the data to the Mat constructor:

const auto image = peak::BufferTo<peak::ipl::Image>(buffer).ConvertTo(peak::ipl::PixelFormatName::BGRa8, peak::ipl::ConversionMode::Fast);
cv::Mat newImage(height, width, CV_8UC4, image.Data(),cv::Mat::AUTO_STEP);