opencv 专栏收录该内容
6 篇文章 1 订阅

# cv2.fitEllipse函数详解：ellipse参数并根据参数计算出椭圆焦点坐标

ellipse = cv2.fitEllipse(cnt)

#(x, y), (a, b), angle = cv2.fitEllipse(cnt)
#ellipse =  [ (x, y) , (a, b), angle ]


ellipse 为元组类型，其里面的参数为：

• （x, y）代表椭圆中心点的位置
• （a, b）代表长短轴长度，应注意a、b为长短轴的直径，而非半径
angle 代表了中心旋转的角度

## 椭圆焦点坐标的计算

#画长宽
res_ellipse = ((150,140),(150,100),60)
ell_center_x = int(res_ellipse[0][0])
ell_center_y = int(res_ellipse[0][1])

ell_h_point1_x = int(ell_center_x - 0.5 * res_ellipse[1][0] * math.cos(res_ellipse[2]/ 180 * math.pi))
ell_h_point1_y = int(ell_center_y - 0.5 * res_ellipse[1][0]* math.sin(res_ellipse[2]/ 180 * math.pi))
ell_h_point2_x = int(ell_center_x + 0.5 * res_ellipse[1][0] * math.cos(res_ellipse[2]/ 180 * math.pi))
ell_h_point2_y = int(ell_center_y + 0.5 * res_ellipse[1][0]* math.sin(res_ellipse[2]/ 180 * math.pi))

ell_w_point1_x = int(ell_center_x - 0.5 * res_ellipse[1][1] * math.sin(res_ellipse[2]/ 180 * math.pi))
ell_w_point1_y = int(ell_center_y + 0.5 * res_ellipse[1][1]* math.cos(res_ellipse[2]/ 180 * math.pi))
ell_w_point2_x = int(ell_center_x + 0.5 * res_ellipse[1][1] * math.sin(res_ellipse[2]/ 180 * math.pi))
ell_w_point2_y = int(ell_center_y - 0.5 * res_ellipse[1][1]* math.cos(res_ellipse[2]/ 180 * math.pi))

cv2.line(img,(ell_h_point1_x,ell_h_point1_y),(ell_h_point2_x,ell_h_point2_y),(0,255,255),thickness=2)
cv2.line(img,(ell_w_point1_x,ell_w_point1_y),(ell_w_point2_x,ell_w_point2_y),(0,255,255),thickness=2)



## 完整代码如下：

import os
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image
import cv2
from PIL import ImageEnhance
import math
def cv_show(name,file):
cv2.imshow(name, file)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 椭圆参数的计算

#cv_show("res",img)
cv2.ellipse(img, ((150,140),(150,100),60), (0, 255, 0), 2)

cv_show("res",img)
#画长宽

res_ellipse = ((150,140),(150,100),60)
ell_center_x = int(res_ellipse[0][0])
ell_center_y = int(res_ellipse[0][1])

ell_h_point1_x = int(ell_center_x - 0.5 * res_ellipse[1][0] * math.cos(res_ellipse[2]/ 180 * math.pi))
ell_h_point1_y = int(ell_center_y - 0.5 * res_ellipse[1][0]* math.sin(res_ellipse[2]/ 180 * math.pi))
ell_h_point2_x = int(ell_center_x + 0.5 * res_ellipse[1][0] * math.cos(res_ellipse[2]/ 180 * math.pi))
ell_h_point2_y = int(ell_center_y + 0.5 * res_ellipse[1][0]* math.sin(res_ellipse[2]/ 180 * math.pi))

ell_w_point1_x = int(ell_center_x - 0.5 * res_ellipse[1][1] * math.sin(res_ellipse[2]/ 180 * math.pi))
ell_w_point1_y = int(ell_center_y + 0.5 * res_ellipse[1][1]* math.cos(res_ellipse[2]/ 180 * math.pi))
ell_w_point2_x = int(ell_center_x + 0.5 * res_ellipse[1][1] * math.sin(res_ellipse[2]/ 180 * math.pi))
ell_w_point2_y = int(ell_center_y - 0.5 * res_ellipse[1][1]* math.cos(res_ellipse[2]/ 180 * math.pi))

cv2.line(img,(ell_h_point1_x,ell_h_point1_y),(ell_h_point2_x,ell_h_point2_y),(0,255,255),thickness=2)
cv2.line(img,(ell_w_point1_x,ell_w_point1_y),(ell_w_point2_x,ell_w_point2_y),(0,255,255),thickness=2)

cv_show("res_img",img)

• 1
点赞
• 3
评论
• 16
收藏
• 一键三连
• 扫一扫，分享海报

02-07 1万+
08-26 1万+
10-26 4万+
01-03 2103
04-17 1677
06-22 3571