-
Notifications
You must be signed in to change notification settings - Fork 0
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,kernelApply 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)Series of edge finding kernels
- Input:
- gray:: Gray scaled Numpy array (INTENDED FOR GRAY SCALED IMGS)
- Output:
- high pass filtered mig
-+--- kernel ---+-:
[[-1 -1 -1]
[-1 8 -1]
[-1 -1 -1]]
- Output:
- schnarr filter img
-+--- kernel ---+-:
[[ -3 0 3]
[-10 0 10]
[ -3 0 3]]
- Output:
- sobel xaxis filter
-+--- kernel ---+-:
[[-1 0 1]
[-2 0 2]
[-1 0 1]]
- Output:
- sobel y axis filter
-+--- kernel ---+-:
[[-1 -2 -1]
[ 0 0 0]
[ 1 2 1]]
- Output:
- Laplacian filter result
-+--- kernel ---+-:
[[ 0 1 0]
[ 1 -4 1]
[ 0 1 0]]
Series of image sharpening kernels
- Input:
- img:: Numpy array
- 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]]
- Output:
- Simple standard sharpening kernel
-+--- kernel ---+-:
[[-1 -1 -1]
[-1 9 -1]
[-1 -1 -1]]
- Output:
- Extreme image sharpening
-+--- kernel ---+-:
[[ 1 1 1]
[ 1 -7 1]
[ 1 1 1]]
Canny edged detection written from scratch (No as fast as opeCV implementation but a general idea into how algo works
Steps:
-
Gaussian Filter
-
Sobel X + Y Filter
-
Compute Gradients (G) = SQRT(sobel_x_values^2 + sobel_y_values^2)
-
Normalize Gradients = G/max(G)*255
-
Compute angle between filters = arctan(sobel_y_values/sobel_x_values) #tan = opposite (y)/adjacent (x)
-
If angle < 0 += 180
-
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) -
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 -
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