1

I need to extract a detected rectangle from an image. Rectangles are rotated in arbitrary degrees which are unknown. I have seen this post: How to straighten a rotated rectangle

But there are two problems:

  1. How to calculate 'theta', which is solvable mathematically.
  2. The code snippet in this post works only for cv not cv2.

Anybody have better solutions than this post? A solution completely developed in cv2

I have used RotateRect function. In both cases I have detected the rectangles and draw them. The problem is the angle. In both cases angels are detected negative. at least one of them should be positive. That,s why I can not rotate them correctly according to link. Please take a look at the photos I attached to the question. enter image description here

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Danial
  • 703
  • 1
  • 11
  • 26
  • 1
    You can use `RotatedRect`, which has the angel information. Or you can compute the angle from the vertices coordinates. So do you need the math for 1) ? Regarding 2), just use new syntax: `CreateImage` becomes `imread`, etc... Then, **what you've tried so far?** How are your rectangles stored? Do you have the vertices coordinates? Are they ordered? What have you tried to compute the angle? – Miki Jan 05 '16 at 15:36
  • I have used RotateRect function. In both cases I have detected the rectangles and draw them. The problem is the angle. In both cases angels are detected negative. at least one of them should be positive. That,s why I can not rotate them correctly according to link. Please take a look at the photos I attached to the question. – Danial Jan 05 '16 at 16:50
  • 2
    angle of RotatedRect may seem counter-intuitive sometime, but it's correct! Try this: if width > height, add PI/2 to the angle, and swap width and height. – Miki Jan 05 '16 at 16:54
  • Perfect Miki. As usual. !!!! – Danial Jan 05 '16 at 17:45

0 Answers0