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

效果展示:

相关推荐
浠寒AI1 小时前
智能体模式篇(上)- 深入 ReAct:LangGraph构建能自主思考与行动的 AI
人工智能·python
weixin_505154462 小时前
数字孪生在建设智慧城市中可以起到哪些作用或帮助?
大数据·人工智能·智慧城市·数字孪生·数据可视化
Best_Me072 小时前
深度学习模块缝合
人工智能·深度学习
YuTaoShao2 小时前
【论文阅读】YOLOv8在单目下视多车目标检测中的应用
人工智能·yolo·目标检测
算家计算2 小时前
字节开源代码模型——Seed-Coder 本地部署教程,模型自驱动数据筛选,让每行代码都精准落位!
人工智能·开源
伪_装3 小时前
大语言模型(LLM)面试问题集
人工智能·语言模型·自然语言处理
gs801403 小时前
Tavily 技术详解:为大模型提供实时搜索增强的利器
人工智能·rag
music&movie3 小时前
算法工程师认知水平要求总结
人工智能·算法
量子位4 小时前
苹果炮轰推理模型全是假思考!4 个游戏戳破神话,o3/DeepSeek 高难度全崩溃
人工智能·deepseek
黑鹿0224 小时前
机器学习基础(四) 决策树
人工智能·决策树·机器学习