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来修改阈值,从而动态修改边界
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
https://docs.opencv.org/4.x/d9/dc8/tutorial_py_trackbar.html

上一篇:一个高质量老虎


下一篇:002.add-two-numbers