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

效果展示:

相关推荐
2401_8414956421 小时前
【计算机视觉】霍夫变换函数的参数调整
人工智能·python·算法·计算机视觉·霍夫变换·直线检测·调整策略
FIN666821 小时前
昂瑞微:引领射频前端国产化浪潮,铸就5G时代核心竞争力
前端·人工智能·科技·5g·芯片·卫星
bylander21 小时前
【论文阅读】通义实验室,VACE: All-in-One Video Creation and Editing
论文阅读·人工智能·计算机视觉·音视频
nju_spy21 小时前
复杂结构数据挖掘(二)关联规则挖掘 Association rule mining
人工智能·数据挖掘·关联规则挖掘·apiriori·dhp·fp-growth·高频集
刀客Doc21 小时前
刀客doc:亚马逊广告再下一城,拿下微软DSP广告业务
大数据·人工智能·microsoft
AndrewHZ1 天前
【图像处理基石】GIS图像处理入门:4个核心算法与Python实现(附完整代码)
图像处理·python·算法·计算机视觉·gis·cv·地理信息系统
掘金安东尼1 天前
Google+禁用“一次性抓取100条搜索结果”,SEO迎来变革?
人工智能
FIN66681 天前
射频技术领域的领航者,昂瑞微IPO即将上会审议
前端·人工智能·前端框架·信息与通信
小麦矩阵系统永久免费1 天前
短视频矩阵系统哪个好用?2025最新评测与推荐|小麦矩阵系统
大数据·人工智能·矩阵
Mr.Lee jack1 天前
【vLLM】源码解读:高性能大语言模型推理引擎的工程设计与实现
人工智能·语言模型·自然语言处理