Opencv拖动条控制均值滤波卷积核大小,拖动条控制是否保存(涉及知识点:cv2.createTrackbar和cv2.getTrackbarPos的使用)

'''
    带拖动条的均值滤波
'''
import time

import cv2
import numpy as np

def callback(int):
    pass

cv2.namedWindow('dst',cv2.WINDOW_AUTOSIZE)

# 创建trackbar (trackbarname,winname,value,count,callback,userdata)
cv2.createTrackbar('ksize', 'dst', 3, 30, callback)
cv2.createTrackbar('save', 'dst', 0, 1, callback)

path = '../img/'
img_name = 'lena'
suf = '.jpg'

img_path = path+img_name+suf

img = cv2.imread(img_path)

dst = img.copy()
while True:
    cv2.imshow('img', img)
    ksize = cv2.getTrackbarPos('ksize', 'dst')  # 输出值:trackbarname,winname  输出:当前值
    if ksize%2==0:
        ksize+=1
    # dst = cv2.blur(img, (ksize,ksize))
    dst = cv2.GaussianBlur(img,(ksize,ksize),sigmaX=1)
    cv2.imshow('dst', dst)

    # 判断是否保存
    is_save = cv2.getTrackbarPos('save','dst')

    if is_save==1:
        cv2.imwrite(path+img_name+'_new'+suf,dst)
        print('已保存')
        is_save=0
    key = cv2.waitKey(300)  # 持续10ms
    if key & 0xFF == 27:
        break
cv2.destroyAllWindows()

原图:
Opencv拖动条控制均值滤波卷积核大小,拖动条控制是否保存(涉及知识点:cv2.createTrackbar和cv2.getTrackbarPos的使用)_第1张图片
结果:
Opencv拖动条控制均值滤波卷积核大小,拖动条控制是否保存(涉及知识点:cv2.createTrackbar和cv2.getTrackbarPos的使用)_第2张图片

你可能感兴趣的:(opencv笔记,opencv,均值算法,人工智能)