机器学习有很多关于核函数的说法,核函数的定义和作用是什么? - 知乎
核函数 = 内积 = 相似度
Convolution&Correlation
http://16423.courses.cs.cmu.edu/slides/Lecture_19.pdf
Correlation Filtering
https://medium.com/@aybukeyalcinerr/correlation-vs-convolution-filtering-2711d8bb3666
'''
To make convolution filtering, there are 2 different way:
1,Flip the kernel in both dimensions in the code and then call filter2D function of opencv.
2,Use scipy library instead of opencv to make convolution filtering.
Reminder! : In correlation filtering, we will use the above filter directly but in convolution filtering, the above kernel will be flipped in both dimensions.
'''
import cv2
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
# read image
src = cv2.imread('eye.jpg',0)
# prepare the filter
kernel = np.array([[1,1,1],[1,1,0],[1,0,0]])
# apply kernel to the original image using convolution filtering
dst_conv = ndimage.convolve(src, kernel, mode='constant', cval=1.0)
# apply kernel to the original image using correlation filtering
dst_corr = cv2.filter2D(src, -1, kernel)
# concatenate images horizontally
result = np.concatenate((src, dst_corr, dst_conv), axis=1)
cv2.imwrite('result.png', result)
https://towardsdatascience.com/image-derivative-8a07a4118550
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
I = cv2.imread('edges.png', 0).astype(np.float64)
#-Derivative x
Kx = -1*np.array([[-1,0,1]])
Fx = ndimage.convolve(I, Kx)
#-Derivative y
Ky = -1*np.array([[-1],[0],[1]])
Fy = ndimage.convolve(I, Ky)
#-Gradient
#--Magnitute
magnitude = np.sqrt(Fx**2 + Fy**2) # G
#--Orientation
phase = cv2.phase(Fx, Fy, angleInDegrees=True) #theta
mask_phase = np.zeros((I.shape[0], I.shape[1],3), dtype=np.uint8)
mask_phase[ (magnitude != 0) & (phase >= 0) & (phase <= 90)] = np.array([0, 0, 255]) #red
mask_phase[ (magnitude != 0) & (phase > 90) & (phase <= 180)] = np.array([0, 255, 255]) #yellow
mask_phase[ (magnitude != 0) & (phase > 180) & (phase <= 270)] = np.array([0, 255, 0]) #green
mask_phase[ (magnitude != 0) & (phase > 270) & (phase <= 360)] = np.array([255, 0, 0]) #blue
#-Save
#We take the abs value of Fx and Fy without considering the sign of derivative. This is important to show the edge in the image.
cv2.imwrite("Fx.png",np.abs(Fx))
cv2.imwrite("Fy.png",np.abs(Fy))
cv2.imwrite("Magnitude.png",magnitude)
cv2.imwrite("Orientation.png",mask_phase)