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;
        }
    }
}

效果图:
这里写图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cccccc1212

这是c币不是人民币,不要充值

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值