简体   繁体   English

OpenCV Python:使用轨迹栏模糊图像

[英]OpenCV Python: Blur image using trackbar

I'd like to control the blur of an image by using a trackbar. 我想通过使用轨迹栏来控制图像的模糊。 The given MWE imports a picture with a trackbar that sets the aperture linear size ( ksize ) which has to be one or a positive odd number. 给定的MWE导入带有ksize的图片,该ksize设置的光圈线性大小( ksize )必须为1或正奇数。 Strangely getTrackbarPos returns a negative number which makes it necesarry to multiply ksize by -2 and subtract 1. Inside an infinite loop the image gets blurred and displayed. 奇怪的是, getTrackbarPos返回一个负数,这使得必须将ksize乘以-2并减去1。在无限循环内,图像变得模糊并显示出来。

import cv2

# Callback function for trackbar
def on_change(self):
    pass

# Reads image with 0 as GRAY and 1 as BGR
img = cv2.imread('example.JPG', 0)

# Creates window
cv2.namedWindow('Image')

# Creates Trackbar with slider position and callback function
low_k = 1  # slider start position
high_k = 21  # maximal slider position
cv2.createTrackbar('Blur', 'Image', low_k, high_k, on_change)

# Infinite loop
while(True):
    ksize = cv2.getTrackbarPos('ksize', 'Image')  # returns trackbar position
    ksize = -2*ksize-1  # medianBlur allows only odd ksize values

    # Blures input image
    median = cv2.medianBlur(img, ksize)  # source, kernel size

    cv2.imshow('Image', median)  # displays image 'median' in window
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

By running this code a window opens with the desired trackbar on top of the input image. 通过运行此代码,将打开一个窗口,在输入图像的顶部带有所需的跟踪栏。 The slider start position is one as desired but by changing the silder's position there is no blur or any significant change to the displayed image. 滑块的起始位置是所需的位置,但是通过更改贴图机的位置,不会对显示的图像造成模糊或任何重大变化。

The main question is why the returned trackbar position does not have any influence on medianBlur() . 主要问题是,为什么返回的medianBlur()位置不会对medianBlur()产生任何影响。 My first thought suggests a mistake either inside the while loop or the callback function. 我的第一个想法是在while循环或回调函数内部提出错误。 Besides that I'd like to know why getTrackbarPos returns negativ numbers. 除此之外,我想知道为什么getTrackbarPos返回负数。

I am using Python 3.6 with Anaconda 1.9.2. 我正在将Python 3.6与Anaconda 1.9.2一起使用。 Thank you for any help! 感谢您的任何帮助!

getTrackbarPos arguments expects the name of the trackbar and the name of the window. getTrackbarPos参数需要跟踪栏的名称和窗口的名称。 You are creating the trackbar with the name Blur and reading as ksize , change 您正在创建名称为Blur ksize ,并读为ksize ,更改

ksize = cv2.getTrackbarPos('Blur', 'Image')

to

ksize = cv2.getTrackbarPos('ksize', 'Image')

or change the other way around (the createTrackbar method). 或更改其他方式( createTrackbar方法)。

Also, as mentioned in the comments, you can also update ksize on the on_change callback. 另外,如注释中所述,您还可以在on_change回调中更新ksize

As a side note, you also need to adjust the way you treat odd values, if the track bar position is 1, the ksize ends up as -3 附带说明,您还需要调整处理奇数值的方式,如果轨迹栏位置为1,则ksize最终为-3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM