• Python Opencv实践 - 轮廓特征(最小外接圆,椭圆拟合)


    1. import cv2 as cv
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. img = cv.imread("../SampleImages/stars.PNG")
    5. plt.imshow(img[:,:,::-1])
    6. #轮廓检测
    7. img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    8. ret,thresh = cv.threshold(img_gray, 127, 255, 0)
    9. contours,hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
    10. #显示轮廓
    11. img_contours = img.copy()
    12. img_contours = cv.drawContours(img_contours, contours, -1, (0,255,0), 2)
    13. plt.imshow(img_contours, cmap=plt.cm.gray)
    14. for contour in contours:
    15. #轮廓最小外接圆
    16. #(x,y),radius = cv.minEnclosingCircle(cnt)
    17. #cnt: 轮廓信息
    18. #(x,y):最小外接圆的圆心
    19. #radius: 最小外接圆的半径
    20. #参考资料:https://geek-docs.com/opencv/python-opencv/t_how-to-find-the-minimum-enclosing-circle-of-an-object-in-opencv-python.html
    21. (x,y),radius = cv.minEnclosingCircle(contour)
    22. center = (int(x), int(y))
    23. radius = int(radius)
    24. cv.circle(img, center, radius, (0,255,0), 2)
    25. #椭圆拟合
    26. #ellips = cv.fitEllipse(cnt)
    27. #ellipse: 椭圆信息((x,y),(a,b),angle) (x,y)椭圆中心点;(a,b) 椭圆长短轴的直径(注意:非半径);angle中心旋转角度
    28. #参考资料:https://blog.csdn.net/Other_stone/article/details/111186254?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-5-111186254-blog-111409635.235%5Ev38%5Epc_relevant_default_base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-5-111186254-blog-111409635.235%5Ev38%5Epc_relevant_default_base&utm_relevant_index=10
    29. ellipse = cv.fitEllipse(contour)
    30. cv.ellipse(img, ellipse, (0,0,255), 2)
    31. plt.imshow(img[:,:,::-1])

     

     

  • 相关阅读:
    在 Ubuntu 22.04安装配置 Ansible
    猿创征文|阿里云MaxCompute存取性能测试报告
    倍福PLC通过MC_ReadParameter读取NC轴的配置参数
    ros-noetic采集单目USB相机数据
    Cadence orcad 原理图导出带书签目录的办法
    MDT服务器的安装
    攻防世界WEB练习区(view_source、get_post、robots)
    微信小程序项目源代码SSM英语学习平台
    【无标题】
    C#【必备技能篇】使用GDI绘制进度条的代码实现
  • 原文地址:https://blog.csdn.net/vivo01/article/details/132700714