OpenCV-ED绘制的使用(附源码)

注意:需安装opencv-contrib-python库,直接用pip install 即可

1.创建 EdgeDrawing 对象

python 复制代码
 ed = cv.ximgproc.createEdgeDrawing()

2.参数设置

python 复制代码
EDParams = cv.ximgproc.EdgeDrawing_Params()
# scharr算子,还有sobel等等,可选可不选,一般不选
# EDParams.EdgeDetectionOperator = cv.ximgproc.EDGE_DRAWING_SCHARR 
# 最小路径长度:设置检测到的边缘片段的最小长度。如果边缘片段的长度小于这个值,则会被忽略。
EDParams.MinPathLength = 100
# 概率森林模式 (PFmode):当设置为 True 时,启用概率森林模式,这可以提高边缘检测的准确性,但可能会增加计算时间。
EDParams.PFmode = False  # 默认值,尝试将其更改为 True
# 最小直线长度:设置检测到的直线的最小长度。如果直线长度小于这个值,则会被忽略
EDParams.MinLineLength = 100  # 尝试将此值更改为 5 到 100 之间
# NFA 验证可以帮助过滤掉一些误检的边缘或线条
EDParams.NFAValidation = True  # 默认值,尝试将其更改为 False
# 设置梯度阈值。只有当边缘点的梯度值大于这个阈值时,才会被认为是有效的边缘点
EDParams.GradientThresholdValue = 20  # 默认值,尝试将其更改为 False
ed.setParams(EDParams)

3.获取检测结果

python 复制代码
    # 获取检测到的边缘片段、线条和椭圆
    segments = ed.getSegments()  # 边缘
    lines = ed.detectLines()  # 线条
    ellipses = ed.detectEllipses()  # 椭圆

完整代码以及图像绘制

python 复制代码
from __future__ import print_function
import numpy as np
import cv2 as cv
import random as rng
import sys

rng.seed(12345)


def main():
    try:
        fn = sys.argv[1]
    except IndexError:
        fn = r"your_image_path"
    src = cv.imread(cv.samples.findFile(fn))
    src = cv.resize(src, (0, 0), fx=0.2, fy=0.2)
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    cv.imshow("source", src)
    ssrc = src.copy() * 0
    lsrc = src.copy()
    esrc = src.copy()
    ed = cv.ximgproc.createEdgeDrawing()
    # you can change parameters (refer the documentation to see all parameters)
    EDParams = cv.ximgproc.EdgeDrawing.Params()
    EDParams.MinPathLength = 100
    EDParams.PFmode = False
    EDParams.MinLineLength = 100
    EDParams.NFAValidation = True
    ed.setParams(EDParams)
    # Detect edges
    # you should call this before detectLines() and detectEllipses()
    ed.detectEdges(gray)
    segments = ed.getSegments()
    lines = ed.detectLines()
    ellipses = ed.detectEllipses()
    # Draw detected edge segments
    for i in range(len(segments)):
        color = (rng.randint(0, 256), rng.randint(0, 256), rng.randint(0, 256))
        cv.polylines(ssrc, [segments[i]], False, color, 1, cv.LINE_8)
    cv.imshow("detected edge segments", ssrc)
    # Draw detected lines
    if lines is not None:
        lines = np.uint16(np.around(lines))
        for i in range(len(lines)):
            cv.line(lsrc, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 1,
                    cv.LINE_AA)
    cv.imshow("detected lines", lsrc)
    # Draw detected circles and ellipses
    if ellipses is not None:
        for i in range(len(ellipses)):
            center = (int(ellipses[i][0][0]), int(ellipses[i][0][1]))
            axes = (int(ellipses[i][0][2]) + int(ellipses[i][0][3]), int(ellipses[i][0][2]) + int(ellipses[i][0][4]))
            angle = ellipses[i][0][5]
            color = (0, 0, 255)
            if ellipses[i][0][2] == 0:
                color = (0, 255, 0)
            cv.ellipse(esrc, center, axes, angle, 0, 360, color, 2, cv.LINE_AA)
            cv.imshow("detected circles and ellipses", esrc)
            print('Done')


if __name__ == '__main__':
    print(__doc__)
    main()
    cv.waitKey(0)
    cv.destroyAllWindows()

效果展示:

相关推荐
蹦蹦跳跳真可爱5891 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
蚂蚁20142 小时前
卷积神经网络(二)
人工智能·计算机视觉
z_mazin4 小时前
反爬虫机制中的验证码识别:类型、技术难点与应对策略
人工智能·计算机视觉·目标跟踪
lixy5795 小时前
深度学习3.7 softmax回归的简洁实现
人工智能·深度学习·回归
youhebuke2255 小时前
利用deepseek快速生成甘特图
人工智能·甘特图·deepseek
訾博ZiBo5 小时前
AI日报 - 2025年04月26日
人工智能
郭不耐5 小时前
DeepSeek智能时空数据分析(三):专业级地理数据可视化赏析-《杭州市国土空间总体规划(2021-2035年)》
人工智能·信息可视化·数据分析·毕业设计·数据可视化·城市规划
AI军哥6 小时前
MySQL8的安装方法
人工智能·mysql·yolo·机器学习·deepseek
余弦的倒数6 小时前
知识蒸馏和迁移学习的区别
人工智能·机器学习·迁移学习
Allen Bright6 小时前
【机器学习-线性回归-2】理解线性回归中的连续值与离散值
人工智能·机器学习·线性回归