find the Canny edge detection whose threshold values can be varied using two trackbars

前言

题目地址:https://docs.opencv.org/4.x/da/d22/tutorial_py_canny.html
题目内容:
Write a small application to find the Canny edge detection whose threshold values can be varied using two trackbars. This way, you can understand the effect of threshold values.

Source Code

# 开发时间:2022/2/22  20:43
# Write a small application to find the Canny edge detection whose threshold values can be varied using two trackbars. This way, you can understand the effect of threshold values.

import cv2 as cv
import numpy as np

def nothing(x):
    pass

maxVal = 200
minVal = 100

# 使用时记得修改图片路径
img = cv.imread(r'D:\pycharm_professional\PycharmProjects\fisrt_try_of_opencv\opencv\test\test_img\1.jian.jpg',0)
cv.namedWindow("image")

# create trackbars for threshold change
cv.createTrackbar("maxVal","image",100,200,nothing)
cv.createTrackbar("minVal","image",0,100,nothing)

while True:
    # get currents position of four trackbars
    edge = cv.Canny(img,minVal,maxVal)
    cv.imshow("image", edge)
    k = cv.waitKey(1) & 0xFF
    if  k == 27:
        break
    try:
        maxVal = cv.getTrackbarPos("maxVal", "image")
        minVal = cv.getTrackbarPos("minVal", "image")
    except:
        break

Result

可以通过调整minVal和maxVal来修改阈值,从而动态修改边界
result

总结

参考代码地址:
https://docs.opencv.org/4.x/da/d22/tutorial_py_canny.html
https://docs.opencv.org/4.x/d9/dc8/tutorial_py_trackbar.html

猜你喜欢

转载自blog.csdn.net/booze_/article/details/123077504