1. 单相机视频图像采集

  使用HalconDotNet进行单相机视频图像采集的基本步骤包括初始化相机、设置采集参数、开始采集、处理图像以及停止采集。

using HalconDotNet;

public void SingleCameraCapture()
{
    HFramegrabber framegrabber = new HFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "CameraName", 0, -1);
    HImage image = framegrabber.GrabImage();
    image.WriteImage("bmp", 0, "captured_image");
    framegrabber.CloseFramegrabber();
}

2. 多相机视频图像采集

  多相机视频图像采集需要为每个相机创建独立的帧抓取器,并分别进行图像采集和处理。

using HalconDotNet;

public void MultiCameraCapture()
{
    HFramegrabber framegrabber1 = new HFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "Camera1", 0, -1);
    HFramegrabber framegrabber2 = new HFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "Camera2", 0, -1);

    HImage image1 = framegrabber1.GrabImage();
    HImage image2 = framegrabber2.GrabImage();

    image1.WriteImage("bmp", 0, "captured_image1");
    image2.WriteImage("bmp", 0, "captured_image2");

    framegrabber1.CloseFramegrabber();
    framegrabber2.CloseFramegrabber();
}

3. 设置相机曝光时间

  通过设置相机的曝光时间,可以控制图像的亮度和对比度。

using HalconDotNet;

public void SetExposureTime()
{
    HFramegrabber framegrabber = new HFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "CameraName", 0, -1);
    framegrabber.SetFramegrabberParam("ExposureTime", 5000); // 设置曝光时间为5000微秒
    HImage image = framegrabber.GrabImage();
    image.WriteImage("bmp", 0, "captured_image");
    framegrabber.CloseFramegrabber();
}

4. 实时图像显示

  在采集图像的同时,实时显示图像可以用于监控和调试。

using HalconDotNet;

public void RealTimeDisplay()
{
    HFramegrabber framegrabber = new HFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "CameraName", 0, -1);
    HWindow window = new HWindow(0, 0, 512, 512, 0, "visible", "");

    while (true)
    {
        HImage image = framegrabber.GrabImage();
        window.DispImage(image);
        System.Threading.Thread.Sleep(100); // 控制显示频率
    }

    framegrabber.CloseFramegrabber();
}

5. 图像采集与保存

  采集图像并保存到指定路径,适用于需要长期存储图像数据的场景。

using HalconDotNet;

public void CaptureAndSave()
{
    HFramegrabber framegrabber = new HFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "CameraName", 0, -1);
    HImage image = framegrabber.GrabImage();
    string savePath = "C:\\Images\\captured_image.bmp";
    image.WriteImage("bmp", 0, savePath);
    framegrabber.CloseFramegrabber();
}

  这些示例代码展示了如何使用HalconDotNet进行单相机和多相机的视频图像采集,并涵盖了设置曝光时间、实时显示图像以及图像保存等常见操作。

09-15 15:43