OpenCV's RotatedRect angle does not provide enough information

后端 未结 4 2004
囚心锁ツ
囚心锁ツ 2020-12-29 12:41

From my experiments, the angle returned by RotatedRect\'s angle variable goes from -90 to 0 degrees, which is not sufficient to determine if the object is leaned to the left

4条回答
  •  -上瘾入骨i
    2020-12-29 12:52

    This is what I use (c is my contour). Basically, I get the longest line's du and dv, and then use atan2()

    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    origin = box[0]
    rect_width, rect_height = rect[1]
    if rect_width > rect_height:
         target = box[3]
    else:
         target = box[1]
    dv = target[1] - origin[1]
    du = target[0] - origin[0]
    angle_rads = math.atan2(dv, du)
    

提交回复
热议问题