Skip to content
Eddy Mina edited this page Jul 31, 2019 · 6 revisions

kernel

Module with a variety of simple kernels ranging from gaussian to image sharpening

import image_processing as imp
from image_processing  import im_processing,kernel

gaussian_filter(img,kernel_size= 3 ,sigma=1,high_pass=False):::

Apply gaussian kernel to RGB numpy array

Things to note about gaussian kernel**

A larger kernel with the same sigma will be smoother

A larger sigma with same kernel increases the variance of noise

  • Input:
    • img:: Red, Green, Blue Image as Numpy array
    • kernel_size:: square kernel
    • sigma:: kernel variance
    • high_pass:: Boolean returns img-guassian if true
  • Output:
    • RGB with gaussian applied
img = im_processing.cv_read('images/img_10.jpg') #load RGB defauly
gray = kernel.gaussian_filter(img)

CLASS edge_filter::

Series of edge finding kernels

  • Input:
    • gray:: Gray scaled Numpy array (INTENDED FOR GRAY SCALED IMGS)
edge_filter(gray).high_pass()::
  • Output:
    • high pass filtered mig
-+--- kernel ---+-:

[[-1 -1 -1]
 [-1  8 -1]
 [-1 -1 -1]]
edge_filter(gray).schnarr_filter()::
  • Output:
    • schnarr filter img
-+--- kernel ---+-:

[[ -3   0   3]
 [-10   0  10]
 [ -3   0   3]]
edge_filter(gray).sobel_x()::
  • Output:
    • sobel xaxis filter
-+--- kernel ---+-:

[[-1  0  1]
 [-2  0  2]
 [-1  0  1]]
edge_filter(gray).sobel_y()::
  • Output:
    • sobel y axis filter
-+--- kernel ---+-:

[[-1 -2 -1]
 [ 0  0  0]
 [ 1  2  1]]
edge_filter(gray).laplacian()::
  • Output:
    • Laplacian filter result
-+--- kernel ---+-:

[[ 0  1  0]
 [ 1 -4  1]
 [ 0  1  0]]

CLASS sharpen::

Series of image sharpening kernels

  • Input:
    • img:: Numpy array
sharpen(img).edge_enhance()::
  • Output:
    • enhance edges in an image
-+--- kernel ---+-:

[[-0.125 -0.125 -0.125 -0.125 -0.125]
 [-0.125  0.25   0.25   0.25  -0.125]
 [-0.125  0.25   1.     0.25  -0.125]
 [-0.25   0.25   0.25   0.25  -0.125]
 [-0.125 -0.125 -0.125 -0.125 -0.125]]

sharpen(img).sharpen()::
  • Output:
    • Simple standard sharpening kernel
-+--- kernel ---+-:

[[-1 -1 -1]
 [-1  9 -1]
 [-1 -1 -1]]
sharpen(img).excessive()::
  • Output:
    • Extreme image sharpening
-+--- kernel ---+-:

[[ 1  1  1]
 [ 1 -7  1]
 [ 1  1  1]]

CLASS canny::

Canny edged detection written from scratch (No as fast as opeCV implementation but a general idea into how algo works

Steps:

  1. Gaussian Filter

  2. Sobel X + Y Filter

  3. Compute Gradients (G) = SQRT(sobel_x_values^2 + sobel_y_values^2)

  4. Normalize Gradients = G/max(G)*255

  5. Compute angle between filters = arctan(sobel_y_values/sobel_x_values) #tan = opposite (y)/adjacent (x)

  6. If angle < 0 += 180

  7. Non maximum supression Edge thinning algo used when gradient filter is computed

     For every pixel gradient (g) and angle (a):
     	edge direction = line through pixel at angle a
     	if pixel on edge > current:
     		set pixel = 255 (white)
     		other pixels= 0 (black)
    
  8. Thresholding

     Strong pixel = p > max threshold  || ~ 10%+ of brightest pixel 
     Weak pixel = max threshold  > p > min threshold  || ~ .05 * .09%  of brightest pixel 
     Irrelvant Pixel=  min threshold  > p
    
     Sets pixel vals to 0,min,and max vals 
    
  9. Hysteresis: Convert weak pixels if neighboring strong to strong

  • Input: `canny(img).isolate()

    • img:: Gray Scaled Image
  • Output:

    • Canny edges images
img = im_processing.cv_read('images/img_10.jpg','GRAY') #load Gray scale 
gray = kernel.canny(img).isolate()

Example Script

import image_processing as imp
from image_processing  import im_processing,kernel

img = imp.cv_read('images/img_9.jpg',RGB=True) #load RGB 

guassian = kernel.gaussian_filter(img,kernel_size= 3 ,sigma=1) #guassian filter 

edge = kernel.edge_filter(img) #simple fast edge filter 

high_pass= edge.high_pass()

schnarr= edge.schnarr_filter() #schnarr kernel 

sobel_x= edge.sobel_x() #sobel_x filter 

sobel_y= edge.sobel_y() #sobel_y filter 

laplacian = edge.laplacian()

sharp = kernel.sharpen(img) #sharpening class

enhance = sharp.edge_enhance() #simple edge enhancement 

sharpen = sharp.sharpen() #normal sharpen filted 

extreme = sharp.excessive() #excessive sharpening 

ims= [img,guassian,enhance,sharpen,extreme,high_pass,schnarr,sobel_x,sobel_y,laplacian]

titles= ["Original Image","Guassian Filter","Enhanced Img","Sharpened Img","Exessive Sharpening","High Pass Edge Filter","Schnarr Edge Filter","sobel_x Edge Filter","sobel_y Edge Filter","Laplacian Edge Filter"]

imp.im_subplot(ims,shape=[3,4],titles=titles,suptitle="Some Tools in Kernel Lib") #plot img subplot for comparison