np.ones() shape resize using cv.trackbar

asked 2020-10-01 02:57:01 -0500

vladT gravatar image

updated 2020-10-01 06:27:35 -0500

berak gravatar image

I'm trying to create a variable sized kernel using np.ones method and trackbars in order to easily modify the size of the kernel. The problem seems to be that the width of the kernel does not decrease when the afferent parameter gets smaller. However this is not the case for the height of the kernel. The shape is correctly passed as a tuple of integers. Could anyone help me understand what's going on here?

import cv2 as cv
import numpy as np

def nothing(x):

img = cv.imread('sudoku.jpg', 0)

if img is None:
    print ('Null Image')

    cv.createTrackbar('kernel_W', 'Trackbars', 1, 100, nothing)
    cv.setTrackbarPos('kernel_W', 'Trackbars', 1)
    cv.createTrackbar('kernel_H', 'Trackbars', 1, 100, nothing)
    cv.setTrackbarPos('kernel_H', 'Trackbars', 1)
    cv.createTrackbar('bin_Th', 'Trackbars', 1, 255, nothing)
    cv.setTrackbarPos('bin_Th', 'Trackbars', 1)

    print (img.shape)
    while True:

        kSize_w = cv.getTrackbarPos('kernel_W', 'Trackbars')
        kSize_h = cv.getTrackbarPos('kernel_H', 'Trackbars')
        bin_Th_val = cv.getTrackbarPos('bin_Th', 'Trackbars')

        _, mask = cv.threshold(img, bin_Th_val, 255, cv.THRESH_BINARY)

        kernel = np.ones((kSize_h,kSize_w), dtype = np.uint8)

        dilation= cv.dilate(mask, kernel, iterations = 3)

        cv.imshow('Initial Image', img)
        cv.imshow('Kernel', kernel)
        cv.imshow('mask', mask)
        cv.imshow('dilation', dilation)

        if cv.waitKey(2) == 27:

edit retag flag offensive close merge delete



please show code, we cannot help you without

berak gravatar imageberak ( 2020-10-01 03:15:05 -0500 )edit

Added code above

vladT gravatar imagevladT ( 2020-10-01 03:24:25 -0500 )edit

please, TEXT, not screenshots

berak gravatar imageberak ( 2020-10-01 04:22:49 -0500 )edit

added above

vladT gravatar imagevladT ( 2020-10-01 04:34:48 -0500 )edit

Do not drag on tab. for kSize_w or kSize_h. Just click on any gray area bar, until u see value. Then do same as kSize_h. After that, u can continue drag kSize_w and kSize_h to see resizing changed.

supra56 gravatar imagesupra56 ( 2020-10-03 09:02:09 -0500 )edit