Displaying and Saving Basler Camera Images with OpenCV

Displaying and Saving Basler Camera Images with OpenCV

pylon SDK × OpenCvSharp / C#

When working with Basler cameras, converting captured images into OpenCV’s Mat format gives you access to a wide range of image processing algorithms.

In this article, we’ll take a GrabResult from the Basler pylon SDK, convert it to an OpenCV Mat, and then:

  • Display it in real time using Cv2.ImShow
  • Save it as an image file using Cv2.ImWrite

✅ Environment

Item Value
Camera Basler acA2500-14gm
SDK Basler pylon Camera Software Suite
Pixel Format Mono8 (8-bit grayscale)
OpenCV Wrapper OpenCvSharp4.Windows (via NuGet)

🎯 Goal: Convert GrabResultMat → Display & Save

Conceptual flow:


🔧 Converting GrabResult to Mat

We’ll use PixelDataConverter.Convert() to extract pixel data from IGrabResult into a byte[], and then wrap that buffer as an OpenCV Mat.

A convenient way is to implement an extension method on IGrabResult:

💡 Note: For color formats (e.g., BGR8, RGB8), you’ll need to adjust OutputPixelFormat and MatType (e.g., CV_8UC3) accordingly.


🖼 Real-Time Display with Cv2.ImShow

We now extend the BaslerCameraSample class (introduced in the previous article on event-driven capture) to display buffered images using OpenCV.

Here, we assume that _bufferedImageQueue is a queue of (DateTime, IGrabResult) that the ImageGrabbed event handler enqueues into.

Example Test: Live Display for 10 Seconds

Running this test opens an OpenCV window named “Camera” that shows the live feed from your Basler camera.


💾 Saving Mat with ImWrite

To save a Mat to disk, use ImWrite(). The following example:

  1. Captures one frame using Snap()
  2. Converts it to Mat
  3. Saves the image
  4. Applies a simple threshold operation and saves again
  5. Also saves via BitmapSource for comparison

ImShowTest

This creates three files:

  1. SnapAndSaveMatTest_Bitmap.bmp — saved from BitmapSource
  2. SnapAndSaveMatTest_Mat.bmp — saved from OpenCV Mat
  3. SnapAndSaveMatTest_Mat_Thresholded.bmp — thresholded version

Comparing (1) and (2) confirms that both pipelines produce the same image.

BitmapSource Mat Mat(Binary)
BitmapSource Mat Mat(Binary)

📌 Tips & Common Pitfalls

Point Notes
Pixel format For color (BGR8, RGB8), use MatType.CV_8UC3 and correct channel order
Nothing shows in ImShow You must call Cv2.WaitKey(); otherwise the window won’t update
OpenCvSharp packages Install both OpenCvSharp4 and OpenCvSharp4.runtime.windows via NuGet
Performance considerations Pinning buffers is fast, but for very high rates you may want to profile allocations

✅ Summary

  • You can convert Basler pylon GrabResult to an OpenCV Mat via PixelDataConverter and a byte[] buffer.
  • Cv2.ImShow enables simple live viewing; Cv2.ImWrite lets you save images directly from Mat.
  • Once in Mat form, you can apply the full power of OpenCV: filtering, edge detection, feature extraction, etc.

Author: @MilleVision

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA