Kernel Function&Convolution&Correlation&HOG

本文解析了核函数在机器学习中的关键作用,涵盖了内积、相似度概念,以及它在Convolution、CorrelationFiltering中的应用实例,如OpenCV的卷积与相关滤波,以及HOG方向梯度直方图的原理。
摘要由CSDN通过智能技术生成

机器学习有很多关于核函数的说法,核函数的定义和作用是什么? - 知乎

核函数 = 内积  = 相似度 

 

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)

HOG 

一文讲解方向梯度直方图(hog) - 知乎

HOG Person Detector Tutorial | Chris McCormick

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值