图像中不规则物体的长轴与短轴:OpenCV实现指南

1.首先,读取图像并将其转换为灰度图像。

2.进行图像预处理,包括使用高斯模糊和阈值化,以便更好地处理图像。

3.通过使用OpenCV的cv2.findContours()函数,找到图像中的所有轮廓。

4.遍历所有轮廓,如果轮廓点的数量大于等于5个,则将这个轮廓拟合为一个椭圆。

5.如果成功拟合出椭圆,则获取椭圆的中心坐标、长轴长度、短轴长度和旋转角度。

6.使用计算得到的椭圆信息,计算出长轴和短轴的端点坐标。

7.使用OpenCV的cv2.ellipse()函数在原始图像上绘制椭圆,并使用cv2.circle()函数在图像上绘制长轴和短轴的四个端点,并分别用红色和蓝色表示。

8.最后,显示带有椭圆和端点的图像,等待用户按下任意键后关闭显示窗口。

import cv2
import numpy as np

image = 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)
        break

if ellipse is not None:
    center, axes, angle = ellipse
    major_axis, minor_axis = axes
    angle_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.")

OpenCV中,要精确计算图像中的椭圆尺寸(半长轴和半长度),通常需要通过轮廓分析和数学形状拟合来完成。以下是一个简化的步骤: 1. **读取和预处理图像**:首先使用`cv2.imread()`加载图像,然后可能需要对图像进行灰度化、二值化或者滤波等操作,以便提取出目标物体的边缘。 ```python import cv2 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) ``` 2. **找到轮廓**:使用`cv2.findContours()`函数,从二值化后的图形单元格中找出轮廓。 ```python contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) ``` 3. **选择椭圆轮廓**:遍历所有轮廓,寻找疑似椭圆形的轮廓,可以利用`cv2.fitEllipse()`函数对轮廓进行拟合,得到一个椭圆的中心点和大小。 ```python best_contour = None best_axis_length = 0 for contour in contours: ellipse = cv2.fitEllipse(contour) if ellipse[1][0] * ellipse[1][1] > best_axis_length: # 通常是面积越大更可能是椭圆 best_contour = contour best_axis_length = ellipse[1][0] * ellipse[1][1] ``` 4. **获取半径**:椭圆的半长轴(a)和半(b)可以通过椭圆的宽度和高度计算出来,因为`cv2.fitEllipse()`返回的是`(center, axes)`,其中`axes`是对角线的长度,即(a,b)。 ```python if best_contour is not None: center, axes = ellipse a = axes[0] / 2 # 半长轴 b = axes[1] / 2 # 半 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VisionX Lab

你的鼓励将是我更新的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值