OpenCV求最小外接圆、最小外接矩形、椭圆拟合、直线拟合

本文介绍了一种使用Python OpenCV库进行图像处理的方法,包括读取图像、转换灰度、阈值处理、轮廓查找及绘制等步骤,并进一步展示了如何为这些轮廓拟合多种几何形状如矩形、圆形和椭圆等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import cv2
import numpy as np

if __name__ == "__main__":
    orig = cv2.imread('mask.jpg', flags=cv2.IMREAD_COLOR)
    mask = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY)
    _, mask = cv2.threshold(mask, 200, 255, 0)
    image = orig.copy()
    contours, hierarchy = cv2.findContours(mask, mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_SIMPLE)
    for c in contours:
        # 绘制轮廓
        image = cv2.drawContours(image, [c], 0, (255, 0, 0), 2)
        # 外接矩形框,没有方向角
        x, y, w, h = cv2.boundingRect(c)
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
        # 最小外接矩形框,有方向角
        rect = cv2.minAreaRect(c)
        print(rect)
        box = np.int0(cv2.boxPoints(rect))
        cv2.drawContours(image, [box], 0, (0, 0, 255), 2)
        # 绘制最小外接圆
        (x, y), radius = cv2.minEnclosingCircle(c)
        center = (int(x), int(y))
        radius = int(radius)
        cv2.circle(image, center, radius, (0, 255, 255), 2)
        # 用轮廓数据来拟合椭圆
        ellipse = cv2.fitEllipse(c)
        cv2.ellipse(image, ellipse, (255, 0, 255), 2)
        # 直线拟合
        rows, cols = image.shape[:2]
        [vx, vy, x, y] = cv2.fitLine(c, cv2.DIST_L2, 0, 0.01, 0.01)
        lefty = int((-x * vy / vx) + y)
        righty = int(((cols - x) * vy / vx) + y)
        image = cv2.line(image, (cols - 1, righty), (0, lefty), (0, 0, 255), 2)
        cv2.imshow("image", image)
        cv2.waitKey(0)

原图

### 如何使用 OpenCV 计算椭圆的外接矩形 在 OpenCV 中,可以通过 `cv2.fitEllipse` 函数拟合一个椭圆到一组点上,并返回该椭圆的相关参数。为了获取这个椭圆的外接矩形,可以利用这些参数进一步计算其边界框。 以下是具体实现方法: #### 1. 拟合椭圆并提取参数 函数 `cv2.fitEllipse(points)` 可以用于拟合椭圆,其中输入是一个二维点集数组。它会返回一个元组 `(center, axes, angle)`,分别表示椭圆中心坐标、轴长度以及旋转角度[^1]。 ```python import numpy as np import cv2 # 假设 points 是一组轮廓点 points = np.array([[x1, y1], [x2, y2], ...]) # 替换为实际数据 ellipse = cv2.fitEllipse(points) (center, (width, height), angle) = ellipse ``` #### 2. 构造外接矩形 一旦得到了椭圆的信息,就可以通过构造一个围绕它的最小面积矩形来获得外接矩形。这一步可以直接调用 `cv2.boundingRect()` 或者更精确地使用 `cv2.minAreaRect()` 来得到旋转后的矩形。 对于非旋转版本(直角矩形),可采用以下代码: ```python rect = cv2.boundingRect(np.int32([points])) (x, y, w, h) = rect print(f"Bounding Rectangle: Top-left corner ({x}, {y}), Width={w}, Height={h}") ``` 如果需要考虑旋转情况,则应改用 `cv2.minAreaRect()` 方法: ```python rotated_rect = cv2.minAreaRect(points) box_points = cv2.boxPoints(rotated_rect) box_points = np.int0(box_points) print("Rotated Bounding Box Points:", box_points) ``` 上述两部分结合起来能够满足不同场景下对外接矩形的需。 #### 完整示例代码 下面提供了一个完整的例子展示如何从给定的一系列点出发找到它们所形成形状的最佳匹配椭圆及其对应的外接矩形。 ```python import cv2 import numpy as np def draw_ellipse_and_bounding_box(image_path): img = cv2.imread(image_path, 0) _, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:] if not contours: print("No contours detected.") return None cnt = max(contours, key=cv2.contourArea) ellipse = cv2.fitEllipse(cnt) center, axes, angle = ellipse result_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) cv2.ellipse(result_img, ellipse, (0, 255, 0), 2) # Draw fitted ellipse rotated_rect = cv2.minAreaRect(cnt) box_points = cv2.boxPoints(rotated_rect) box_points = np.int0(box_points) cv2.drawContours(result_img,[box_points],0,(255,0,0),2) # Draw min area rectangle straight_rect = cv2.boundingRect(cnt) x,y,w,h = straight_rect cv2.rectangle(result_img,(x,y),(x+w,y+h),(0,0,255),2) # Draw bounding rectangle return result_img result = draw_ellipse_and_bounding_box('your_image.png') if result is not None: cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() ``` 此脚本读入一幅灰度图,在二值化之后查找最大连通域作为目标对象,接着绘制出相应的椭圆与两种类型的包围盒。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI吃大瓜

尊重原创,感谢支持

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

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

打赏作者

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

抵扣说明:

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

余额充值