有没有办法制作可变内核?

时间:2019-06-08 21:29:15

标签: python opencv

我正在尝试在打开的cv上制作变量内核。我的代码如下。主要问题是我无法在没有引号的情况下设置内核

内核就是这样

kernel1 = np.ones((3,3), np.uint8) 
kernel2 = np.ones((3,5), np.uint8)
kernel3 = np.ones((5,5), np.uint8)   

我的代码是

 def printIterations(x):
      print ("iterations",x)
 def printKernel(x):
      print ("kernal_type= kernel",x)

 cv2.namedWindow('processed')

 cv2.createTrackbar('iterations','processed',0,20,printIterations)
 cv2.createTrackbar('kernel','processed',1,3,printKernel)

 img = cv2.imread('image.tiff',0)
 img = cv2.resize(img,(512,512))

 while 1:
     k = cv2.waitKey(1) & 0xFF
     if k == 27:
         break

     iteration = cv2.getTrackbarPos('iterations','processed')
     kernel_num=cv2.getTrackbarPos('kernel','processed')
     kernel="kernel{}".format(kernel_num)
     dilation = cv2.dilate(img, kernel, iterations=iteration)

     ret,thresh = cv2.threshold(dilation,cv2.getTrackbarPos('iterations','processed',),256,0)

     cv2.imshow('processed',thresh)

 cv2.destroyAllWindows()

0 个答案:

没有答案