做网站怎么收费多少,静态网站建设的技术运用,域名交易网站,wordpress怎么修改数据库密码1.首先#xff0c;读取图像并将其转换为灰度图像。
2.进行图像预处理#xff0c;包括使用高斯模糊和阈值化#xff0c;以便更好地处理图像。
3.通过使用OpenCV的cv2.findContours()函数#xff0c;找到图像中的所有轮廓。
4.遍历所有轮廓#xff0c;如果轮廓点的数量大…1.首先读取图像并将其转换为灰度图像。
2.进行图像预处理包括使用高斯模糊和阈值化以便更好地处理图像。
3.通过使用OpenCV的cv2.findContours()函数找到图像中的所有轮廓。
4.遍历所有轮廓如果轮廓点的数量大于等于5个则将这个轮廓拟合为一个椭圆。
5.如果成功拟合出椭圆则获取椭圆的中心坐标、长轴长度、短轴长度和旋转角度。
6.使用计算得到的椭圆信息计算出长轴和短轴的端点坐标。
7.使用OpenCV的cv2.ellipse()函数在原始图像上绘制椭圆并使用cv2.circle()函数在图像上绘制长轴和短轴的四个端点并分别用红色和蓝色表示。
8.最后显示带有椭圆和端点的图像等待用户按下任意键后关闭显示窗口。
import cv2
import numpy as npimage cv2.imread(XXX.png, cv2.IMREAD_GRAYSCALE)
blur cv2.GaussianBlur(image, (5, 5), 0)
_, thresh cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)contours, _ cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)ellipse None
for contour in contours:if len(contour) 5:ellipse cv2.fitEllipse(contour)breakif ellipse is not None:center, axes, angle ellipsemajor_axis, minor_axis axesangle_rad np.deg2rad(angle)cos_angle np.cos(angle_rad)sin_angle np.sin(angle_rad)# 长轴端点坐标x1 int(center[0] major_axis / 2 * cos_angle)y1 int(center[1] - major_axis / 2 * sin_angle)x2 int(center[0] - major_axis / 2 * cos_angle)y2 int(center[1] major_axis / 2 * sin_angle)# 短轴端点坐标x3 int(center[0] minor_axis / 2 * sin_angle)y3 int(center[1] minor_axis / 2 * cos_angle)x4 int(center[0] - minor_axis / 2 * sin_angle)y4 int(center[1] - minor_axis / 2 * cos_angle)# 在图像上绘制椭圆及长轴和短轴的端点image_with_ellipse cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)cv2.ellipse(image_with_ellipse, ellipse, (0, 255, 0), 2)cv2.circle(image_with_ellipse, (x1, y1), 5, (0, 0, 255), -1) # 长轴端点用红色标记cv2.circle(image_with_ellipse, (x2, y2), 5, (0, 0, 255), -1) # 长轴端点用红色标记cv2.circle(image_with_ellipse, (x3, y3), 5, (255, 0, 0), -1) # 短轴端点用蓝色标记cv2.circle(image_with_ellipse, (x4, y4), 5, (255, 0, 0), -1) # 短轴端点用蓝色标记# 显示图像cv2.imshow(Image with Ellipse and Axes, image_with_ellipse)cv2.waitKey(0)cv2.destroyAllWindows()
else:print(No ellipse found.)