Ask Your Question
0

Using gabor kernel to extract vertical lines results in black image

asked 2018-10-04 04:09:51 -0600

sazr gravatar image

I understand the concept of the gabor kernel and how it can be used to identify directional edges. So I want to use it to identify barcode lines in images.

However when I filter an image with a gabor kernel I always get a blank/black result. Can you provide feedback on what I need to do to get Gabor to identify the vertical lines in an image, ie, produce a result that has white where the vertical edges are?

Input image: enter image description here

Result: enter image description here

import cv2
import numpy as np  

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

def main():
    src = cv2.imread('./images/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)))

    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)
    gabor = cv2.filter2D(src, cv2.CV_8UC3, 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()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-10-08 18:30:28 -0600

phillity gravatar image

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-10-04 04:09:51 -0600

Seen: 1,929 times

Last updated: Oct 08 '18