日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

hog特征提取python代码_hog特征提取-python实现

發(fā)布時間:2023/12/10 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hog特征提取python代码_hog特征提取-python实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

【轉(zhuǎn)載自 https://blog.csdn.net/ppp8300885/article/details/71078555】

全部代碼:

importcv2importnumpy as npimportmathimportmatplotlib.pyplot as pltclassHog_descriptor():def __init__(self, img, cell_size=16, bin_size=8):

self.img=img

self.img= np.sqrt(img /np.max(img))

self.img= img * 255self.cell_size=cell_size

self.bin_size=bin_size

self.angle_unit= 360 /self.bin_sizeassert type(self.bin_size) == int, "bin_size should be integer,"

assert type(self.cell_size) == int, "cell_size should be integer,"

assert type(self.angle_unit) == int, "bin_size should be divisible by 360"

defextract(self):

height, width=self.img.shape

gradient_magnitude, gradient_angle=self.global_gradient()

gradient_magnitude=abs(gradient_magnitude)

cell_gradient_vector= np.zeros((height / self.cell_size, width /self.cell_size, self.bin_size))for i inrange(cell_gradient_vector.shape[0]):for j in range(cell_gradient_vector.shape[1]):

cell_magnitude= gradient_magnitude[i * self.cell_size:(i + 1) *self.cell_size,

j* self.cell_size:(j + 1) *self.cell_size]

cell_angle= gradient_angle[i * self.cell_size:(i + 1) *self.cell_size,

j* self.cell_size:(j + 1) *self.cell_size]

cell_gradient_vector[i][j]=self.cell_gradient(cell_magnitude, cell_angle)

hog_image=self.render_gradient(np.zeros([height, width]), cell_gradient_vector)

hog_vector=[]for i in range(cell_gradient_vector.shape[0] - 1):for j in range(cell_gradient_vector.shape[1] - 1):

block_vector=[]

block_vector.extend(cell_gradient_vector[i][j])

block_vector.extend(cell_gradient_vector[i][j+ 1])

block_vector.extend(cell_gradient_vector[i+ 1][j])

block_vector.extend(cell_gradient_vector[i+ 1][j + 1])

mag= lambda vector: math.sqrt(sum(i ** 2 for i invector))

magnitude=mag(block_vector)if magnitude !=0:

normalize= lambda block_vector, magnitude: [element / magnitude for element inblock_vector]

block_vector=normalize(block_vector, magnitude)

hog_vector.append(block_vector)returnhog_vector, hog_imagedefglobal_gradient(self):

gradient_values_x= cv2.Sobel(self.img, cv2.CV_64F, 1, 0, ksize=5)

gradient_values_y= cv2.Sobel(self.img, cv2.CV_64F, 0, 1, ksize=5)

gradient_magnitude= cv2.addWeighted(gradient_values_x, 0.5, gradient_values_y, 0.5, 0)

gradient_angle= cv2.phase(gradient_values_x, gradient_values_y, angleInDegrees=True)returngradient_magnitude, gradient_angledefcell_gradient(self, cell_magnitude, cell_angle):

orientation_centers= [0] *self.bin_sizefor i inrange(cell_magnitude.shape[0]):for j in range(cell_magnitude.shape[1]):

gradient_strength=cell_magnitude[i][j]

gradient_angle=cell_angle[i][j]

min_angle, max_angle, mod=self.get_closest_bins(gradient_angle)

orientation_centers[min_angle]+= (gradient_strength * (1 - (mod /self.angle_unit)))

orientation_centers[max_angle]+= (gradient_strength * (mod /self.angle_unit))returnorientation_centersdefget_closest_bins(self, gradient_angle):

idx= int(gradient_angle /self.angle_unit)

mod= gradient_angle %self.angle_unitreturn idx, (idx + 1) %self.bin_size, moddefrender_gradient(self, image, cell_gradient):

cell_width= self.cell_size / 2max_mag=np.array(cell_gradient).max()for x inrange(cell_gradient.shape[0]):for y in range(cell_gradient.shape[1]):

cell_grad=cell_gradient[x][y]

cell_grad/=max_mag

angle=0

angle_gap=self.angle_unitfor magnitude incell_grad:

angle_radian=math.radians(angle)

x1= int(x * self.cell_size + magnitude * cell_width *math.cos(angle_radian))

y1= int(y * self.cell_size + magnitude * cell_width *math.sin(angle_radian))

x2= int(x * self.cell_size - magnitude * cell_width *math.cos(angle_radian))

y2= int(y * self.cell_size - magnitude * cell_width *math.sin(angle_radian))

cv2.line(image, (y1, x1), (y2, x2), int(255 *math.sqrt(magnitude)))

angle+=angle_gapreturnimage

img= cv2.imread('person_037.png', cv2.IMREAD_GRAYSCALE)

hog= Hog_descriptor(img, cell_size=8, bin_size=8)

vector, image=hog.extract()printnp.array(vector).shape

plt.imshow(image, cmap=plt.cm.gray)

plt.show()

5. 結(jié)果分析

本文最終單幅圖像HOG特征的求取平均時間為1.8秒,相比最初版本所需的5.4秒有個長足的改進。

相比初期的版本hog梯度特征圖

可見最終版本中

能夠更加有效的區(qū)分梯度顯示邊緣。這是因為對各個像素的梯度進行了全局歸一化,并且在描繪梯度方向時加入了梯度量級的非線性映射,使得梯度方向產(chǎn)生明顯的深淺和長度差異,更易于區(qū)分邊緣,凸顯明顯的梯度變化。

此外在輸入圖像時,采用Gamma校正對輸入圖像進行顏色空間的標準化能夠抑制噪聲,使得產(chǎn)生的邊緣更加明顯,清晰。

此外改變cell的大小和直方圖方向通道的效果如下:

cell_size = 10 即 16*16個像素

可以看出增大cell的size得到的特征圖更加注重基本輪廓和邊緣,而忽略一些細節(jié),某種程度上降低了噪聲。

當通道數(shù)目為16個方向

梯度特征圖像的細節(jié)變得更加明顯,方向更多。

6. 在人臉識別,物體檢測中的應(yīng)用

在提取完圖像的HOG特征之后,可以使用SVM進行分類訓(xùn)練,能完成行人檢測等任務(wù)。

未來工作可參考Github的行人檢測項目https://github.com/icsfy/Pedestrian_Detection

---------------------

作者:ppp8300885

總結(jié)

以上是生活随笔為你收集整理的hog特征提取python代码_hog特征提取-python实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。