自学内容网 自学内容网

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

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

1.创建 EdgeDrawing 对象

 ed = cv.ximgproc.createEdgeDrawing()

2.参数设置

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.获取检测结果

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

完整代码以及图像绘制

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

效果展示:


原文地址:https://blog.csdn.net/m0_71212744/article/details/145137323

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!