如何在C#中使用鼠标绘制矩形(emgucv)

本文关键字:绘制 emgucv 鼠标 | 更新日期: 2023-09-27 18:28:47

我想用鼠标在视频帧(即图片框)上画一个矩形,就像我们选择任何文件一样。用户将单击鼠标按钮选择区域,然后释放鼠标按钮。就像剪或剪一样!

我在用emgucv!

如何在C#中使用鼠标绘制矩形(emgucv)

using System;
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.Structure;
namespace Emgucv33Apps
{
public partial class FormCropImage : Form
{
    Image<Bgr, byte> imgInput;
    Rectangle rect;
    Point StartLocation;
    Point EndLcation;
    bool IsMouseDown = false;
    public FormCropImage()
    {
        InitializeComponent();
    }
    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog()==DialogResult.OK)
        {
            imgInput = new Image<Bgr, byte>(ofd.FileName);
            pictureBox1.Image = imgInput.Bitmap;
        }
    }
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        IsMouseDown = true;
        StartLocation = e.Location;
    }
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (IsMouseDown==true)
        {
            EndLcation = e.Location;
            pictureBox1.Invalidate();
        }
    }
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        if (rect!=null)
        {
            e.Graphics.DrawRectangle(Pens.Red, GetRectangle());
        }
    }
    private Rectangle GetRectangle()
    {
        rect = new Rectangle();
        rect.X = Math.Min( StartLocation.X,EndLcation.X);
        rect.Y = Math.Min(StartLocation.Y, EndLcation.Y);
        rect.Width = Math.Abs(StartLocation.X - EndLcation.X);
        rect.Height = Math.Abs(StartLocation.Y - EndLcation.Y);
        return rect;
    }
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (IsMouseDown==true)
        {
            EndLcation = e.Location;
            IsMouseDown = false;
            if (rect!=null)
            {
                imgInput.ROI = rect;
                Image<Bgr, byte> temp = imgInput.CopyBlank();
                imgInput.CopyTo(temp);
                imgInput.ROI = Rectangle.Empty;
                pictureBox2.Image = temp.Bitmap;
            }
        }
    }
}
}

只需使用Image<B, T>.Draw方法。它显示在C#示例代码中的形状检测中。以下是链接中的一个片段:

Image<Bgr, Byte> triangleRectangleImage = img.CopyBlank();
foreach (MCvBox2D box in boxList)
   triangleRectangleImage.Draw(box, new Bgr(Color.DarkOrange), 2);