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()

效果展示:

相关推荐
Alocus_19 小时前
小龙虾Openclaw安装与解决cmake等报错 (ubuntu20.04)
人工智能·ubuntu·openclaw·小龙虾
硅基流动19 小时前
硅基流动私有化 MaaS 加速能源央企“数字供应链”智能化升级
人工智能·能源
码农三叔19 小时前
(6-4-02)IMU融合与机体状态估计:综合实战:腿式机器人的IMU关节融合与状态估计(2)
人工智能·嵌入式硬件·机器人·人机交互·人形机器人
祥哥的说19 小时前
万字深度解析 OpenClaw 架构:为什么它能成为全球最火的开源 AI Agent?
人工智能·架构·开源·openclaw
跨境Tool哥19 小时前
2026网赚升级版:利用 OpenClaw 搭建属于你的 AI Agent 赚钱机器
人工智能·openclaw
AI科技19 小时前
清唱歌词的音频直接用,原创音乐人用AI编曲软件直接生成完整歌曲的编曲伴奏
人工智能·音视频
兜兜风d'19 小时前
PyTorch深度学习实践——卷积神经网络高级篇
人工智能·pytorch·深度学习
knighthood200119 小时前
protobuf和gazebo的一些绑定
人工智能·机器人·自动驾驶
瑞华丽PLM19 小时前
通用与专业PLM选型对比 (1)
大数据·人工智能·plm·瑞华丽plm·瑞华丽
再一次等风来19 小时前
深度学习中的梯度消失与梯度爆炸
人工智能·深度学习·梯度