首页 > 解决方案 > OpenCV:计算椭圆长轴和短轴的方向角

问题描述

cv2.fitEllipse()用来在轮廓上拟合椭圆。此函数返回中心坐标、长轴和短轴以及旋转角度。我想知道旋转角度是否与主轴与此处给出的正水平轴的角度相同(src:Wikipedia):

在此处输入图像描述

如果没有,那么有什么方法可以在下面的等式中获得椭圆的系数:

在此处输入图像描述

然后直接计算角度。

标签: pythonopencv

解决方案


这将向您展示 Python/OpenCV 中的 fitEllipse 角度。

输入:

在此处输入图像描述

import cv2
import numpy as np
import math

# read input
img = cv2.imread('labrador.jpg')

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold
thresh = cv2.threshold(gray, 100 , 255, cv2.THRESH_BINARY)[1]

# find largest contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# fit contour to ellipse and get ellipse center, minor and major diameters and angle in degree 
ellipse = cv2.fitEllipse(big_contour)
(xc,yc),(d1,d2),angle = ellipse
print(xc,yc,d1,d1,angle)

# draw ellipse
result = img.copy()
cv2.ellipse(result, ellipse, (0, 255, 0), 3)

# draw circle at center
xc, yc = ellipse[0]
cv2.circle(result, (int(xc),int(yc)), 10, (255, 255, 255), -1)

# draw vertical line
# compute major radius
rmajor = max(d1,d2)/2
if angle > 90:
    angle = angle - 90
else:
    angle = angle + 90
print(angle)
xtop = xc + math.cos(math.radians(angle))*rmajor
ytop = yc + math.sin(math.radians(angle))*rmajor
xbot = xc + math.cos(math.radians(angle+180))*rmajor
ybot = yc + math.sin(math.radians(angle+180))*rmajor
cv2.line(result, (int(xtop),int(ytop)), (int(xbot),int(ybot)), (0, 0, 255), 3)

cv2.imwrite("labrador_ellipse.jpg", result)

cv2.imshow("labrador_thresh", thresh)
cv2.imshow("labrador_ellipse", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

在此处输入图像描述


推荐阅读