Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is a paper to help accomplish your project

Or you could follow this tutorial, but it uses the image gradient and not gabor filters

Here is how to fix the code so the output is not black:

import cv2
import numpy as np  

def deginrad(degree):
    radiant = 2*np.pi/360 * degree
    return radiant

def main():
    src = cv2.imread('barcode1.jpg', cv2.IMREAD_GRAYSCALE)

    # Introduce consistency in width
    const_width = 300
    aspect = float(src.shape[0]) / float(src.shape[1])
    src = cv2.resize(src, (const_width, int(const_width * aspect)))

    ### Need to change sigma in blur
    ### https://en.wikipedia.org/wiki/Gaussian_blur
    #src = cv2.GaussianBlur(src, (7,7), 0) 

    # Apply gabor kernel to identify vertical edges
    g_kernel = cv2.getGaborKernel((9,9), 8, deginrad(0), 5, 0.5, 0, ktype=cv2.CV_32F)
    ### Changed ddepth parameter to -1
    ### Now our ouptut will be in the same format as input (cv2.CV_8UC1 one channel mat of 8-bit unsigned integers 0-255)
    ### cv2.filter2d - https://docs.opencv.org/3.4.3/d4/d86/group__imgproc__filter.html#ga27c049795ce870216ddfb366086b5a04
    ### opencv mat types - https://docs.opencv.org/3.4.3/d1/d1b/group__core__hal__interface.html
    gabor = cv2.filter2D(src, -1, g_kernel)

    # Visual the gabor kernel
    h, w = g_kernel.shape[:2]
    g_kernel = cv2.resize(g_kernel, (20*w, 20*h), interpolation=cv2.INTER_CUBIC)

    cv2.imshow('src', src)
    cv2.imshow('gabor', gabor)  # gabor is just black
    cv2.imshow('gabor kernel', g_kernel)
    cv2.waitKey(0)

if __name__ == "__main__":
    main()

image description