1

I tried the following code:

import cv2
import numpy as np

   
def nothing(x):
  pass

cv2.namedWindow('Image')
img = cv2.imread("kakashi.jpg")

low = 1
high = 100

cv2.createTrackbar('Blur', 'Image',low,high,nothing)
while (True):
    ksize = cv2.getTrackbarPos('ksize', 'Image')
    ksize = -2*ksize-1
    image = cv2.medianBlur(img,ksize)
    
    cv2.imshow('Image', image)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

By changing the position of the slider there are no effects on the image. I am having trouble solving this. I guess there is something I am missing! Please help!

Mr.J
  • 181
  • 1
  • 10
  • 1
    Your medianBlur filter size is always negative, if ksize is positive. Did you mean to use +2*ksize-1 not -2*ksize-1? – fmw42 Jun 13 '21 at 21:30
  • I dont think so. it has to be negative. Here is the reference https://stackoverflow.com/questions/53152665/opencv-python-blur-image-using-trackbar – Mr.J Jun 13 '21 at 22:04
  • 1
    the "reference" has that same bug. – Christoph Rackwitz Jun 13 '21 at 23:02
  • OK. Thanks, I did not know that. Sorry for questioning it. I have not used getTrackbarPos() and a negative value seemed odd to me. Check the value of ksize being used in medianBlur to see if it is what you expect. Check to see that while(True) is actually True. Did you actually verify that your getTrackbarPos value is being returned as negative? – fmw42 Jun 13 '21 at 23:03
  • gettrackbarpos will only return -1 if the trackbar is not found (which is because you are looking for ksize when its called Blur), it should always return a number between low & high, so you should remove the - from the ksize – Ta946 Jun 14 '21 at 06:59
  • Yeah made the necessary changes and got the results! Thanks – Mr.J Jun 14 '21 at 11:12

1 Answers1

2

There is a simple mistake, you are doing. You defined your trackbar name in here as "Blur":

cv2.createTrackbar('Blur', 'Image',low,high,nothing)

Then you are calling your trackbar as "ksize"

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

which is wrong so if you simply change this line with this one:

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

it will fix.

Note: As @fmw42 mentioned in comments, there are also some problems about median size you are calculating. You may recheck it.

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
  • 1
    Thanks! The code is working just fine now! Yes I change the ksize. Afterwards I change it to cv2.blur instead of the median blur. That works better for me! – Mr.J Jun 14 '21 at 11:11