EmguCV用鼠标绘图

在EmguCV中没有找到OpenCV中的对鼠标的调用的函数。
但是可以用WinFrom的属性来获得鼠标的按键和位置与移动。
EmguCV自带的ImageBox并不能这么用,因为无法使自带的滚轮缩放和拖拽缩放失效,所以无法正常的使用鼠标绘图在ImageBox中。
就使用WinFrom的带的PictureBox来做。
就是在放图片时要转换为Bitmap,EmguCV中已经封装了调用特别简单。
需要先添加事件,如下图:

代码:

using System;
using System.Windows;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.CV.Features2D;
using Emgu.CV.ML;
using Emgu.CV.Util;
using Emgu.Util;
using Emgu.CV.Face;
using Emgu.CV.VideoSurveillance;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using Emgu.CV.Stitching;

namespace EmguCVHist
{
    public partial class Form1 : Form
    {
        bool mousedown = false;
        int pre_x = 0, pre_y = 0;
        int x = 0, y = 0;
        Image<Bgr, byte> a;
        public Form1()
        {
            InitializeComponent();
            a = new Image<Bgr, byte>(800, 800, new Bgr(255, 255, 255));

            pictureBox1.Image = a.Bitmap;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mousedown = true;
                pre_x = e.X;
                pre_y = e.Y;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mousedown == true)
            {
                x = e.X;
                y = e.Y;
                CvInvoke.Line(a, new Point(pre_x, pre_y), new Point(x, y), new MCvScalar(0, 0, 255), 2);
                pre_x = x;
                pre_y = y;
                pictureBox1.Image = a.Bitmap;
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if(e.Button==MouseButtons.Left)
                mousedown = false;
        }
    }
}

效果图:
EmguCV用鼠标绘图_第1张图片

你可能感兴趣的:(鼠标,EmguCV)