I am trying to obtain a ROI of a Picture using Emgu, But am getting the wrong region in the second PictureBox
I am getting this https://ibb.co/sHN4kGQ
I got the code from https://www.youtube.com/watch?v=B3q5kta34pM&list=PLUSwCY_ybvyLcNxZ1Q3vCkaCH9rjrRxA6&index=48
Thanks InAdvanced
`` namespace dwkROI2 { public partial class Form1 : Form { Rectangle rect; Point StartROI; bool Selecting; bool bMouseDown;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Selecting = false;
bMouseDown = false;
rect = new Rectangle();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
var img = new Image<Bgr, byte>(ofd.FileName);
pictureBox1.Image = img.ToBitmap();
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
if (Selecting)
{
Point tempEndPoint = e.Location;
rect.Location = new Point(
Math.Min(StartROI.X, tempEndPoint.X),
Math.Min(StartROI.Y, tempEndPoint.Y));
rect.Size = new Size(
Math.Abs(StartROI.X - tempEndPoint.X),
Math.Abs(StartROI.Y - tempEndPoint.Y));
Refresh();
/*
int w = Math.Max(StartROI.X, e.X) - Math.Min(StartROI.X, e.X);
int h = Math.Max(StartROI.Y, e.Y) - Math.Min(StartROI.Y, e.Y);
rect = new Rectangle(
Math.Min(StartROI.X, e.X),
Math.Min(StartROI.Y, e.Y),
w,
h);
*/
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
// if (Selecting)
// {
Selecting = false;
bMouseDown = false;
// }
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Selecting = true;
if (Selecting)
{
bMouseDown = true;
StartROI = e.Location;
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (bMouseDown)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
var img = new Bitmap(pictureBox1.Image).ToImage<Bgr, byte>();
img.ROI = rect;
var imgROI = img.Copy();
img.ROI = Rectangle.Empty;
pictureBox2.Image = imgROI.AsBitmap();
}
}
} ``