简体   繁体   中英

How to draw a rectangle using a mouse in C# (emgucv)?

I want to draw a rectangle using a mouse on video frame (ie picture box), just like when we select any files. The user will click mouse button select the area and will release the mouse button. Just like snipping or crop!

I m using 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;
            }
        }
    }
}
}

Just use the Image<B, T>.Draw method. It's shown in the Shape detection in C# example code. Here is a snippet from the link:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM