Angle of rectangle in image

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Angle of rectangle in image

Post by collectordave »

I have four points x,y that describe a rectangle on an image.

The rectangle can be at any angle from -45 to +45 degrees and at any position on the image. The angle is not known before hand.

I can calculate the angle to the X axis when I have the correct two points.

The question given the four points is how do I get hold of the two that describe the line to use.

Mostly this is the first line closest to the horizontal.

After a few hours of trying does anybody have any idea how to get the two points needed?

Regards

collectordave
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Angle of rectangle in image

Post by Olli »

Depending of the language, the question seems to be difficult, but I think it is really simple and interesting : could you post an image (no need to be realistic, just a draw on the fly, some arrows and '?' characters !) illustrating your question ?
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Angle of rectangle in image

Post by eck49 »

A rectangle has two pairs of parallel lines.

Firstly calculate the six pairs of differences between the points, ie
x1-x2, y1-y2
x1-x3, y1-y3
x1-x4, y1-y4
x2-x3, y2-y3
x2-x4, y2-y4
x3-x4, y3-y4
but ignore the signs of the 12 numbers.

Two of these will represent the diagonals of the rectangle, the others will be two pairs of equal values which will be the four sides.
Discard the diagonals. The line you want to choose will be one of the others.

Now the line you want will depend on what is meant by the phrase "closest to the horizontal".

If you mean that the difference between the y-coordinates at the ends of the chosen line is less that the others, one pair will be less than the other but there will be two equally acceptable parallel lines.

If, on the other hand, you mean the chosen line is to be the one closest to the line y = 0 it may be one of these two or it could be that one or both of the other two cross that line and for you this will take the prize.

There are special cases such as a rectangle composed of the points (x,y): (1,5), (1, -5), (10, 5), (10, -5), which sits half above and half below y=0 like a ship at sea and where any of the four lines could be the one you want depending on the interpretation of the phrase.

I hope I've understood the question!
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Angle of rectangle in image

Post by kenmo »

My thoughts:

Take the 4 points

Pick the "bottom" point (by max y, or min y, depending on your coordinates layout)

Calculate the angles to the other 3 points (see atan() or atan2())

The minimum of those 3 angles (use abs() because they could be negative) should give you the angle of the rectangle

I have no example code for you :)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Angle of rectangle in image

Post by collectordave »

Thanks for all the replies


I should have described it better.

This is for an automatic image crop and rotate application, I have the image edge detection sorted and the get the minimum bounding rectangle for all the points detected which leaves me with a rectangle which is not always regular just four sides.

I can crop the image no bother it was just straightening the image.

After a bit of sleep I tried a different approach.

Realised I had the four points to draw the rectangle then looked at the Vector drawing commands.

As I draw the lines on a hidden image just get the path angle of each line and compare.

Code: Select all


      MovePathCursor(X1,Y1)
      AddPathLine(X2,Y2)
      
      Debug "Angle Of Line = "
      Debug PathPointAngle(10)  ;Angle of line in degrees
      StrokePath(1)
      
      ;Repeat For Each Line
      
   
This gives me four angles the smallest ABS value appears to be the one I need which I can then reverse and use to rotate the coordinates to straighten up the image.

Now to spend a few hours testing this.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Angle of rectangle in image

Post by eck49 »

Just to straighten up my previous post, that approach (posted late at night, should have slept on it!) will not quite do. Pythagoras guarantees the longest line from each of the 4 points must be a diagonal, and the extremes of the other two lines form the other diagonal, so these would be the lines to discard.

Each of the posts, mine, kenmo's and your own demonstrate that taking any one of the points and looking at its relationship to all of the other three will yield the result you want, one way or another. Speed or convenience should decide the approach in the end.

If you choose to calculate angles, two will be at right angles to one another and these will be for the sides.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Post Reply