0

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

} ``

dwk
  • 25
  • 5
  • "I am getting this" Okay, and *what is wrong with* that result? What *should the result be instead*? Why? How is your code intended to make that happen - what is the intended algorithm of the code? What happens when you try to [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) the code - wher does the behaviour differ from your expectations? Please read [ask]. – Karl Knechtel Oct 22 '21 at 09:58
  • Also, why is this tagged as `emgucv`? Where do you use that in the code? How is it relevant to the *problem you are asking about*? – Karl Knechtel Oct 22 '21 at 09:59
  • I am getting this https://ibb.co/sHN4kGQ it should display what inside the red square only – dwk Oct 22 '21 at 10:09
  • I don't understand. When I look at the image in that link, I see a red square. How is it not being displayed? – Karl Knechtel Oct 22 '21 at 10:10
  • only what inside the red square should be displayed in picturebox 2 – dwk Oct 22 '21 at 10:14
  • Okay, so what happens when you attempt to debug the program? For example, in `button2_Click`, does `rect` have the value you expect? When you do `var imgROI = img.Copy();`, is that intended to copy only the ROI? Does the documentation say that it works that way? – Karl Knechtel Oct 22 '21 at 10:17
  • var imgROI = img.Copy(); should copy the ROI only, but the images in picturebox 2 is not that of the region selected in picturbox 1 by the red square, it works in the video, linked above, the rect values appear to be wrong – dwk Oct 22 '21 at 10:28
  • Well, do you know what the rect values should be? Do you see a pattern in how they go wrong? – Karl Knechtel Oct 22 '21 at 10:29
  • I have playing around with it and the values of the rect are wrong, I think its the setting and releasing of bool Selecting variable , which controls the values of the rec, is at fault – dwk Oct 22 '21 at 10:58
  • OOOPs .. the code works fine, I had the Pictureboxes1 Properties set up wrong, it was on StretchImage, which distorted the location **thank you Karl** – dwk Oct 22 '21 at 11:18

0 Answers0