Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Basler image to OpenCV

Tags:

opencv

I'm trying to convert frames captured from a Basler camera to OpenCV's Mat format. There isn't a lot of information from the Basler API documentation, but these are the two lines in the Basler example that should be useful in determining what the format of the output is:

// Get the pointer to the image buffer
const uint8_t *pImageBuffer = (uint8_t *) Result.Buffer();
cout << "Gray value of first pixel: " << (uint32_t) pImageBuffer[0] << endl << endl;

I know what the image format is (currently set to mono 8-bit), and have tried doing:

img = cv::Mat(964, 1294, CV_8UC1, &pImageBuffer);
img = cv::Mat(964, 1294, CV_8UC1, Result.Buffer());

Neither of which works. Any suggestions/advices would be much appreciated, thanks!

EDIT: I can access the pixels in the Basler image by:

for (int i=0; i<1294*964; i++)
  (uint8_t) pImageBuffer[i];

If that helps with converting it to OpenCV's Mat format.

like image 406
chocobo_ff Avatar asked Oct 25 '25 14:10

chocobo_ff


1 Answers

You are creating the cv images to use the camera's memory - rather than the images owning their own memory. The problem may be that the camera is locking that pointer - or perhaps expects to reallocate and move it on each new image

Try creating the images without the last parameter and then copy the pixel data from the camera to the image using memcpy().

// Danger! Result.Buffer() may be changed by the Basler driver without your knowing          
const uint8_t *pImageBuffer = (uint8_t *) Result.Buffer();  

// This is using memory that you have no control over - inside the Result object
img = cv::Mat(964, 1294, CV_8UC1, &pImageBuffer);

// Instead do this
img = cv::Mat(964, 1294, CV_8UC1); // manages it's own memory

// copies from Result.Buffer into img 
memcpy(img.ptr(),Result.Buffer(),1294*964); 

// edit: cvImage stores it's rows aligned on a 4byte boundary
// so if the source data isn't aligned you will have to do
for (int irow=0;irow<964;irow++) {
     memcpy(img.ptr(irow),Result.Buffer()+(irow*1294),1294);
 }
like image 109
Martin Beckett Avatar answered Oct 27 '25 07:10

Martin Beckett