其他分享
首页 > 其他分享> > 图像采集(相机)

图像采集(相机)

作者:互联网

1、Halcon实现图像采集

  通过 Image Acquisition 01 助手采集图像

  

 

 

 

  得到Halcon代码:

* Image Acquisition 01: Code generated by Image Acquisition 01
open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '[0] USB2.0 PC CAMERA', 0, -1, AcqHandle)
set_framegrabber_param (AcqHandle, 'sharpness', 0)
grab_image (Image, AcqHandle)
* Image Acquisition 01: Do something
close_framegrabber (AcqHandle)
Halcon Code

 

2、根据Halcon代码,在WPF中实现图像采集(忽略了添加Halcondotnet.dll引用部分)

<Window x:Class="GrabImage.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:hc="clr-namespace:HalconDotNet;assembly=halcondotnet"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="Grab Image" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="110"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <StackPanel Margin="5">
            <Button Content="Grab Image" Margin="0 5"
                    Command="{Binding GrabImageCommand}"/>
        </StackPanel>
        <hc:HSmartWindowControlWPF Grid.Column="1" Margin="5" >
            <hc:HIconicDisplayObjectWPF IconicObject="{Binding DisplayImage}"/>            
        </hc:HSmartWindowControlWPF>
    </Grid>
</Window>
View
using GrabImage.Models;
using HalconDotNet;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Windows;

namespace GrabImage.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        #region Fields

        Camera camera;

        #endregion

        #region Properties
        
        //界面显示的图像
        private HImage _displayImage;
        public HImage DisplayImage
        {
            get { return _displayImage; }
            set { SetProperty(ref _displayImage, value); }
        }        
        #endregion

        #region Commands

        public DelegateCommand GrabImageCommand { get; set; }

        #endregion

        #region Constructor

        public MainWindowViewModel()
        {
            GrabImageCommand = new DelegateCommand(GrabImage);
            camera = Camera.GetInstance();
        }

        #endregion

        #region Methods

        /// <summary>
        /// 从相机抓取一张图片
        /// </summary>
        private void GrabImage()
        {
            DisplayImage = camera.GrabImage();
            DisplayImage.Dispose();
        }

        #endregion

    }
}
ViewModel
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrabImage.Models
{
    public class Camera
    {

        #region Fields

        private static Camera uniqueCamera;
        private HFramegrabber framegrabber;
        HImage image = new HImage();

        #endregion

        #region Ctor & Dtor

        private Camera()
        {
            framegrabber = new HFramegrabber();
            framegrabber.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default",
                8, "rgb", -1.0, "false", "default", "[0] USB2.0 PC CAMERA", 0, -1);
        }
        ~Camera()
        {
            framegrabber.CloseFramegrabber();
            framegrabber.Dispose();
            image.Dispose();
        }

        #endregion

        #region Methods

        public static Camera GetInstance()
        {
            if (uniqueCamera == null)
                uniqueCamera = new Camera();
            return uniqueCamera;
        }
        public HImage GrabImage()
        {
            if (image != null) image.Dispose();
            image = framegrabber.GrabImage();
            return image;
        }

        #endregion

    }
}
Camera.cs

  运行程序结果:

 

标签:framegrabber,endregion,region,相机,采集,Camera,图像,using,GrabImage
来源: https://www.cnblogs.com/wuyun123/p/15415021.html