import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from numpy.dual import norm
from scipy.linalg.decomp_schur import eps
from scipy.ndimage import filters
import math
def image2():
src = cv.imread("G:\Python/img11.jpg")
height = src.shape[0] # 图片宽度
width = src.shape[1] # 图片高度
img = cv.resize(src, (int(width * 0.4), int(height * 0.4))) # 将图片缩小为原来的0.4倍
img1 = cv.cvtColor(img, cv.COLOR_BGR2RGB) # 将BGR变成RGB
# 原始图像的显示
plt.figure(1)
plt.subplot(341)
plt.imshow(img1)
plt.title("original")
plt.axis('off')
hsv = cv.cvtColor(img1, cv.COLOR_RGB2HSV)
# hsv图片显示
plt.figure(1)
plt.subplot(342)
plt.imshow(hsv)
plt.title("hsv")
plt.axis('off')
# hsv肤色阈值提取
lower_hsv = np.array([5.4, 0, 0])
upper_hsv = np.array([23.04, 255, 255])
hsv_bw = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)
# hsv阈值提取二值图像显示
plt.figure(1)
plt.subplot(343)
plt.imshow(hsv_bw , cmap = 'gray')
plt.title("hsv_bw")
plt.axis('off')
ycrcb = cv.cvtColor(img1, cv.COLOR_RGB2YCrCb)
# 将ycrcb变成ycbcr
y, cr, cb = cv.split(ycrcb)
t=cr
cr=cb
cb=t
ycbcr = cv.merge([y, cr, cb])
# ycbcr图片显示
plt.figure(1)
plt.subplot(344)
plt.imshow(ycbcr)
plt.title("ycbcr")
plt.axis('off')
# ycbcr肤色阈值提取
lower_hsv = np.array([35, 80, 131])
upper_hsv = np.array([230, 128, 173])
ycbcr_bw = cv.inRange(ycbcr, lowerb=lower_hsv, upperb=upper_hsv)
# ycbcr阈值提取二值图像显示
plt.figure(1)
plt.subplot(345)
plt.imshow(ycbcr_bw , cmap = 'gray')
plt.title("ycbcr_bw")
plt.axis('off')
t1 = hsv_bw
t2 = ycbcr_bw
t = t1 & t2
plt.figure(1)
plt.subplot(346)
plt.imshow(t , cmap = 'gray')
plt.title("extract")
plt.axis('off')
# 中值滤波
t3 = cv.medianBlur(t, 9)
plt.figure(1)
plt.subplot(347)
plt.imshow(t3 , cmap = 'gray')
plt.title("median_filter")
plt.axis('off')
#连通区域去除小面积
contours, hierarchy = cv.findContours(t3, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
for i in range(len(contours)):
area = cv.contourArea(contours[i])
if area < 1500:
cv.drawContours(t3,[contours[i]],0,0,-1)
t4 = t3
plt.figure(1)
plt.subplot(348)
plt.imshow(t4 , cmap = 'gray')
plt.title("connect region")
plt.axis('off')
# 剪切最小矩形
x, y, w, h = cv.boundingRect(t4)
t5 = t4
img2 = img1[y-5:y+h+5 , x-5:x+w+5]
cv.imwrite("G:\Python/img2.jpg",img2)
plt.figure(1)
plt.subplot(349)
plt.imshow(img2, cmap='gray')
plt.title("shear_iamge")
plt.axis('off')
plt.show()
def hog_extract():
# 归一化为256*256
t6 = cv.imread("G:\Python/img2.jpg")
t7= cv.resize(t6, (int(256), int(256)))
# 分离RGB变量
t8 = cv.cvtColor(t7, cv.COLOR_BGR2RGB)
(B, G, R) = cv.split(t8) # 提取R、G、B分量
# 高斯滤波
B= cv.GaussianBlur(B,(15,15) ,2 )
G = cv.GaussianBlur(G,(15,15) ,2 )
R = cv.GaussianBlur(R,(15,15) ,2 )
#print("R",R)
# print("G",G)
#print("B",B)
height = t8.shape[0] # 图片宽度
width = t8.shape[1] # 图片高度
mag = np.zeros((height, width), dtype = np.float16)
theta = np.zeros((height, width), dtype=np.float16)
for i in range(1, height - 1):
for j in range(1, width - 1):
maxv = 0
mdx = 0
mdy = 0
# 梯度幅值的计算
# r 分量梯度的幅值
v1 = int(R[i, j + 1]) - int(R[i, j - 1]) # x direction
v2 = int(R[i + 1, j]) - int(R[i - 1, j]) # ydirection
v = np.sqrt(v1 * v1 + v2 * v2)
if (v >= maxv):
maxv = v
mdx = v1
mdy = v2
v1 = int(G[i, j + 1]) - int(G[i, j - 1]) # x direction
v2 = int(G[i + 1, j]) - int(G[i - 1, j]) # y direction
v = np.sqrt(v1 * v1 + v2 * v2)
if (v >= maxv):
maxv = v
mdx = v1
mdy = v2
# b分量梯度的幅值
v1 = int(B[i, j + 1]) - int(B[i, j - 1]) # x direction
v2 = int(B[i + 1, j]) - int(B[i - 1, j]) # y direction
v = np.sqrt(v1 * v1 + v2 * v2)
if (v >= maxv):
maxv = v
mdx = v1
mdy = v2
mag[i, j] = float(maxv)
# 梯度相角的计算
if mdx == 0 and mdy == 0:
w =math.pi/ 2.0
elif mdx == 0 and mdy != 0:
w = math.pi / 2.0
elif mdx != 0 and mdy == 0:
w = 0
elif mdx * mdy > 0:
w = np.arctan2(mdy, mdx)
else:
w = math.pi - np.arctan2(-mdy, mdx)
mag[i, j] = maxv
theta[i, j] = w
#print("mag" , mag)
#print("theta" , theta)
# 定义感兴趣区域
xmin = 1
xmax = width
ymin = 1
ymax = height
# hog计算
gridquant = 8 #cell 8*8
thequant = 9 # bin 9
hogdes = np.zeros((1, gridquant * gridquant * thequant), dtype=np.float16)
hoghist = np.zeros((1, 9), dtype=np.float32)
# 找到每个cell
for i in range ( 0 ,height , 32 ):
for j in range (0 ,width ,32):
reggradec = np.zeros(( 32 ,32 ) , dtype = np.float16)
reggramag = np.zeros(( 32 ,32 ) , dtype = np.float16)
reggradec = theta[i:i+32 , j:j+32]
reggramag = mag[i:i + 32, j:j +32]
# 找到每个call内的每个像素
for ii in range(0, 32):
for jj in range(0, 32):
intervalnum = math.ceil(reggradec[ii, jj] / (math.pi / thequant)) # 将梯度方向在9个bin上投票(除以9得数是几就属于第几个bin)
intervalnum = max(intervalnum, 0) # 若出的后的结果<1,就算在第一个bin
intervalnum = min(intervalnum, 8) # 若出的后的结果>9,就算在第9个bin
#print("intervalnum" , intervalnum)
hoghist[0 , intervalnum]+= reggramag[ii, jj] # 将cell内每个像素点的幅值进行投票累加
hoghist = hoghist / (norm(hoghist) + eps)#将每个子区域的方向投票统计后进行标准化
#print("hoghist", hoghist)
for m in range (0 ,575,8):
for n in range (0 , 8, 1):
hogdes[0 ,m+n] = hoghist[0 ,n]
#print("hogdes", hogdes)
hogdes = hogdes / (norm(hogdes) + eps)
# 可视化
gradientx1 = np.zeros((32, 32), dtype=np.float16)
gradienty1 = np.zeros((32, 32), dtype=np.float16)
gradientx1 = (mag * [np.cos(theta)])
gradienty1 = mag * [np.sin(theta)]
gradientx1 = np.reshape( gradientx1 , (256 , 256))
gradienty1 = np.reshape(gradienty1, (256, 256))
#print("gradientx1" , gradientx1)
#print("gradienty1" , gradienty1)
x = np.linspace(0, 255, 32)
y = np.linspace(0, 255, 32)
x = x.astype('int64')
y = y.astype('int64')
#print("x" , x)
#print("y" , y)
X, Y = np.meshgrid(x,y)
#print("X" , X)
#print("Y" , Y)
imgrax = np.zeros((32, 32), dtype=np.float16)
imgray = np.zeros((32, 32), dtype=np.float16)
for i in range ( 0 , 255,8 ):
for j in range (0 ,255 ,8):
m = int(i/8)
n = int(j/8)
imgrax [m,n]= gradientx1[i,j]
imgray [m,n]= gradienty1[i,j]
imgrax [m,n]= gradientx1[i,j]
imgray [m,n]= gradienty1[i,j]
plt.fig
Python_hog_手势_手势识别_图像识别处理_源码
版权申诉
5星 · 超过95%的资源 113 浏览量
2021-10-04
01:41:08
上传
评论 1
收藏 6KB RAR 举报
周玉坤举重
- 粉丝: 61
- 资源: 4782
最新资源
- mybatis动态sql及其JAVA示例
- 微软常用运行库 游戏运行库 VC++各个版本
- 微信小程序开发教程.pptx
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- 锐捷网络认证中心网络管理.pdf
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- SD8233LF是一款用于单按键触摸及接近感应开关,其用途是替代传统的机械型开关芯片IC
- 基于YOLOv5的烟雾火焰检测算法研究
- 基于STM32的联合调试侦听设备解决方案原理图PCB源文件调试工具视频(大赛作品)
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈