Python+OpenCV Canny滑动条调参

一、cv2.createTrackbar()参数介绍

第一个参数是滑动条的名字
第二个参数是滑动条被放置窗口的名字
第三个参数是滑动条的默认位置
第四个参数是 滑动条的最大值
第五个函数是回调函数
每次滑动条的滑动都会调用回调函数。回调函数通常都会含有一个默认参数,就是滑动条的位置。在下面例子中这个函数不用做任何事情,我们只需要 pass 就可以了。 
#定义回调函数
def nothing(x):
    pass

二、使用步骤

1.引库

import cv2
import numpy as np

2.读图

img = cv2.imread('butter.png')

3.创建窗口和滑动条

#创建窗口
cv2.namedWindow('Canny')

#创建滑动条,分别对应Canny的两个阈值
cv2.createTrackbar('threshold1','Canny',0,255,nothing)
cv2.createTrackbar('threshold2','Canny',0,255,nothing)

4.丢while(1)里面实时显示

while(1):

    #返回当前阈值
    threshold1=cv2.getTrackbarPos('threshold1','Canny')
    threshold2=cv2.getTrackbarPos('threshold2','Canny')

    img_output=cv2.Canny(img,threshold1,threshold2)

    #显示图片
    cv2.imshow('original',img)
    cv2.imshow('Canny',img_output)  

    #空格跳出
    if cv2.waitKey(1)==ord(' '):
        break

    #摧毁所有窗口
cv2.destroyAllWindows()

三、运行结果

杂谈 

其实,学废了一种,其他自然也就废了

一起加油,骚年

おすすめ

転載: blog.csdn.net/weixin_56903904/article/details/121584914