[英]How to draw a rectangle using a mouse in C# (emgucv)?
我想在視頻幀(即圖片框)上使用鼠標繪制一個矩形,就像選擇任何文件一樣。 用戶將單擊鼠標按鈕選擇區域,然后釋放鼠標按鈕。 就像剪斷或裁剪一樣!
我正在使用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;
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.