简体   繁体   中英

Basler camera image OpenCvSharp3

I want to put a PictureBox on WinForm in C # using a Basler camera. But I want to convert IGrabImage to Mat. because I want to insert it into the PictureBox using Mat.

Please let me know your hint or solution.

PixelDataConverter converter = new PixelDataConverter();

    public Form1() {
        InitializeComponent();
        using (Camera camera = new Camera())
        {
            camera.CameraOpened += Configuration.AcquireContinuous;

            camera.Open();
            camera.Parameters[PLCameraInstance.MaxNumBuffer].SetValue(5);
            camera.StreamGrabber.Start();

            IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);

            using (grabResult)
            {
                if (grabResult.GrabSucceeded) {
                    Mat rtnMat = convertToMat(grabResult);                       
                    Cv2.ImShow("test", rtnMat);
                    pictureBox1.Image = BitmapConverter.ToBitmap(frame);
                }
            }

            camera.StreamGrabber.Stop();
            camera.Close();
        }
    }

    private Mat convertToMat(IGrabResult rtnGrabResult) {
        IImage image = rtnGrabResult;
        converter.OutputPixelFormat = PixelType.BGR8packed;
        byte[] buffer = image.PixelData as byte[];
        return new Mat(rtnGrabResult.Width, rtnGrabResult.Height, MatType.CV_8UC1, buffer);
    }

Basler Image:

巴斯勒影像

OpenCvSharp Image:

OpenCvSharp 图像

Here is the correct way to convert an IGrabResult into an OpenCvSharp.Mat. I didn't try it without the converter but your main problem was the sequence of the new Mat(..) arguments. In OpenCV, you declare the rows first and then the columns. That means first height and then width. And also the MatType for an colored image was wrong like @Nyerguds said. It has to be CV_8UC3.

Corrected code:

private Mat convertToMat(IGrabResult rtnGrabResult) {
        converter.OutputPixelFormat = PixelType.BGR8packed;
        byte[] buffer = new byte[conv.GetBufferSizeForConversion(rtnGrabResult];
        converter.Convert(buffer, rtnGrabResult);
        return new Mat(rtnGrabResult.Height, rtnGrabResult.Width, MatType.CV_8UC3, buffer);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM