FANDOM


#Feb 8 Notes

#Image kernals - No popcorn, unfortunatly

#Image kernals are normally a fixed maxtrix size, such as a 3x3. Applied to grayscale images.

#Convolution.  
# Image matx:
# 1 2 3 2 1    kernal:
# 2 4 6 8 2   .5  0  0
# 3 6 9 6 3    0  1  0
# 2 8 6 4 2    0  0 .5
# 1 2 3 2 1
#
# Multiplying smaller 3x3 matrixes within the 5x5 matrix by the 3x3 matrix
# (e.g. with the 4 in the middle of a smaller 3x3 subsmatrix) (.5*1+0*2+0*3+2*0+1*4+...ect = 9)
# This calculated value is the new value of our center.  4 now equals 9.

# Edges are issues, because we have to address them differently.  One way to fix
# is to ignore the edge pixels and make them black.
# or to 'wrap' the edge pixels, or crop and lose the data.

# To apply a kernal
# 1. Load image
# 2. Convert to grayscale
# 3. Decide on a kernal (3x3 outline kernal in the example)
# 4. Convolve the kernal with the original image and display the image. This finds areas of contrast.

# Identity kernal (000;010;000) (1 in the middle of the 3x3 matrix, not really useful)
# Gaussian blur all values in the matrix will sum up to 1.  e.g (.0625,.125,.0625;.125,.25,.125;.0625,.125,.0625)
# Emboss creates a bevel of sorts. (-2,-1,0;-1,1,1;0,1,2) Much heavier on top left
# Outline creates a contrast based outline.  (-1,-1,-1;-1,8,-1;-1,-1,-1) used to find edge pixels.
# Sharpen (0,-1,0;-1,5,-5;0,-1,0), not quite as drastic as outline.

import numpy
import cv2
img=cv2.imgread('filein.jpg',0)
kernel = [[0,1,0],[1,0,1],[0,1,0]]
imgBlur = cv2.filter2D(img, -1, kernel) # image , ddepth - desired depth normally -1, kernel is numpy array - applys to each channel idividually


#*****************************************************************************************************************************************
#Filtering methods

#Blur
imgBlur=cv2.blur(img, (5,5)) #image, arraysz - two integers represents the size of box array to use for the blue boxes use all 1's as kernel values

#Gaussian Blur
imgBlur = cv2.GaussianBlur(img, (5,5), 0) #image, arraysz, border type (0-wrap pixels)

#Median Blur
imgBlur=cv2.medianBlur(img,5) #For all the values, find median then put it in the cell (image, ksize - must be odd!!)

#Bilateral - eliminates noise, but keeps image sharp
imgBlur=cv2.bilateralFilter(img, 9, 75,75) #image, diameter of pixels = How far out to look , sigCol = larger means farther colors in the neighborhood will be mixed togther, sigSpace = larger means farther pixesl will influence



#******************************************************************************************************************************************
#Edge detection

# Canny edge dectection
# 1. Smooth image - Use a 5x5 gaussian kernal.
# 2. Find the intensity gradient of the image using a Sobel kernel (similar to outline kernel)
# 3. Calculate the gradient magnitude and the direction for each pixel
# 4. Remove any unwatned pixel (makes a 'thin' edge)
# 5. Lastly, use hysteresis to set min and max values of edges - anyting above the max value is considered a 'sure-edge' , anything below the min value is not an edge and anything connected to a 'sure-edge' is considered to be an edge as well


imgEdge=cv2.Canny(img, 100, 200) #image, min threshold, max threshold VERY SLOW









Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.