0

Original question: Original question

I have an image I1 of size height x width = (1500,500) resized to (500,300).

I1 contains bounding boxes characterized by their coordinates (Top, Left, Bottom, Right).


How can rescale the coordinates of the bounding boxes when The size of I1 is changed? are these formulas correct?

   double newTop = Math.Ceiling((top) * (double)pictureBox1.Height / (double)image1.Height);
   double newLeft = Math.Ceiling((left) * (double)pictureBox1.Width / (double)image1.Width);
   double newBottom = Math.Ceiling((bottom + 1) * (double)pictureBox1.Height / (double)image1.Height) - 1;
   double newRight = Math.Ceiling((right + 1) * (double)pictureBox1.Width / (double)image1.Width) - 1;

Community
  • 1
  • 1
Hani Goc
  • 2,371
  • 5
  • 45
  • 89
  • 1
    Seems ok from a brief look without actually trying it. – Codor Sep 15 '14 at 13:49
  • Are you sure you should be doing integer divisons? Seems to me you are going to run into some massive errors unless I'm completely misunderstanding your code. – InBetween Sep 15 '14 at 14:17
  • @InBetween you are right. Maybe because of the integers i am not being able to rescale the bounding box correctly. I'll try it now. Shoud I take the uppder or lower bounds? for the division – Hani Goc Sep 15 '14 at 14:18
  • The code looks like C#, so why have you added the C++ tag? – crashmstr Sep 15 '14 at 14:52

1 Answers1

1

In general:

Sizes scale by the factor (new-size)/(old-size).

Coordinates are a bit more complex:

x2 = left2 + (x1 - left1) * width2 / width1
y2 = top2 + (y1 - top1) * height2 / height1

where left and width, etc describe the location of the entire image. x and y describe the feature being transformed. In your case, corners of the bounding boxes are features.

If left1, left2, top1, top2 are all zero, you will get expressions similar to yours.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720