Skip to content

Commit 0b592fe

Browse files
committed
100~105 HOG
1 parent 66f5038 commit 0b592fe

27 files changed

+31975
-2
lines changed

Diff for: README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,10 @@ code_095 | [ORB Feature Descriptor Matching](python/code_095) | ✔️
137137
code_096 | [Multiple Descriptor Matching Methods](python/code_096) | ✏️
138138
code_097 | [Location of Known Objects Based on Descriptor Matches](python/code_097) | ✏️
139139
code_098 | [SIFT Feature Critical Point Detection](python/code_097) | ✔️
140-
code_099 | [SIFT Feature Descriptor Matching](python/code_097) | ✔️
140+
code_099 | [SIFT Feature Descriptor Matching](python/code_097) | ✔️
141+
code_100 | [HOG Pedestrian Detection](python/code_100/opencv_100.py) | ✔️
142+
code_101 | [HOG Multiscale Detection](python/code_101/opencv_101.py) | ✏️
143+
code_102 | [HOG Extract Descriptor](python/code_102/opencv_102.py) | ✔️
144+
code_103 | [HOG Use Descriptors to Generate Sample Data](python/code_103/opencv_103.py) | ✔️
145+
code_104 | [(Detection Case)-HOG+SVM Train](python/code_104/opencv_104.py) | ✔️
146+
code_105 | [(Detection Case)-HOG+SVM Predict](python/code_105/opencv_105.py) | ✔️

Diff for: README_CN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,10 @@ code_095 | [ORB 特征描述子匹配](python/code_095) | ✔️
136136
code_096 | [多种描述子匹配方法](python/code_096) | ✏️
137137
code_097 | [基于描述子匹配的已知对象定位](python/code_097) | ✏️
138138
code_098 | [SIFT 特征关键点检测](python/code_097) | ✔️
139-
code_099 | [SIFT 特征描述子匹配](python/code_097) | ✔️
139+
code_099 | [SIFT 特征描述子匹配](python/code_097) | ✔️
140+
code_100 | [HOG 行人检测](python/code_100/opencv_100.py) | ✔️
141+
code_101 | [HOG 多尺度检测](python/code_101/opencv_101.py) | ✏️
142+
code_102 | [HOG 提取描述子](python/code_102/opencv_102.py) | ✔️
143+
code_103 | [HOG 使用描述子生成样本数据](python/code_103/opencv_103.py) | ✔️
144+
code_104 | [(检测案例)-HOG+SVM 训练](python/code_104/opencv_104.py) | ✔️
145+
code_105 | [(检测案例)-HOG+SVM 预测](python/code_105/opencv_105.py) | ✔️

Diff for: python/code_100/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理-HOG特征和应用](https://zhuanlan.zhihu.com/p/75705284)

Diff for: python/code_100/opencv_100.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import cv2 as cv
2+
3+
if __name__ == '__main__':
4+
src = cv.imread("pedestrian.png")
5+
cv.imshow("input", src)
6+
hog = cv.HOGDescriptor()
7+
hog.setSVMDetector(cv.HOGDescriptor_getDefaultPeopleDetector())
8+
# Detect people in the image
9+
(rects, weights) = hog.detectMultiScale(src,
10+
winStride=(4, 4),
11+
padding=(8, 8),
12+
scale=1.25,
13+
useMeanshiftGrouping=False)
14+
for (x, y, w, h) in rects:
15+
cv.rectangle(src, (x, y), (x + w, y + h), (0, 255, 0), 2)
16+
17+
cv.imshow("hog-detector", src)
18+
cv.waitKey(0)
19+
cv.destroyAllWindows()

Diff for: python/code_100/pedestrian.png

846 KB
Loading

Diff for: python/code_101/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理-HOG特征和应用](https://zhuanlan.zhihu.com/p/75705284)

Diff for: python/code_101/detector.jpg

46.3 KB
Loading

Diff for: python/code_101/opencv_101.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import cv2 as cv
2+
3+
if __name__ == '__main__':
4+
src = cv.imread("people1.jpg")
5+
src = cv.resize(src, (0, 0), fx=0.5, fy=0.5)
6+
cv.imshow("input", src)
7+
cv.imwrite('people_src.jpg', src)
8+
hog = cv.HOGDescriptor()
9+
hog.setSVMDetector(cv.HOGDescriptor_getDefaultPeopleDetector())
10+
# Detect people in the image
11+
(rects, weights) = hog.detectMultiScale(src,
12+
winStride=(4, 4),
13+
padding=(8, 8),
14+
scale=1.25,
15+
useMeanshiftGrouping=False)
16+
for (x, y, w, h) in rects:
17+
cv.rectangle(src, (x, y), (x + w, y + h), (0, 0, 255), 2)
18+
19+
cv.imshow("hog-detector", src)
20+
cv.imwrite('detector.jpg', src)
21+
cv.waitKey(0)
22+
cv.destroyAllWindows()

Diff for: python/code_101/pedestrian_02.png

728 KB
Loading

Diff for: python/code_101/people.jpg

116 KB
Loading

Diff for: python/code_101/people1.jpg

132 KB
Loading

Diff for: python/code_101/people_src.jpg

44.5 KB
Loading

Diff for: python/code_102/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理-HOG特征和应用](https://zhuanlan.zhihu.com/p/75705284)

Diff for: python/code_102/gaoyy_min.png

15.9 KB
Loading

Diff for: python/code_102/opencv_102.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
if __name__ == '__main__':
5+
src = cv.imread("gaoyy_min.png")
6+
print(src.shape)
7+
hog = cv.HOGDescriptor()
8+
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
9+
fv = hog.compute(gray, winStride=(8, 8), padding=(0, 0))
10+
print(len(fv))
11+
print(fv[0])
12+
cv.namedWindow('hog-descriptor', cv.WINDOW_NORMAL)
13+
cv.imshow("hog-descriptor", src)
14+
cv.waitKey(0)
15+
cv.destroyAllWindows()

Diff for: python/code_103/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理-HOG特征和应用](https://zhuanlan.zhihu.com/p/75705284)

Diff for: python/code_103/hog_bg.jpg

2.32 KB
Loading

Diff for: python/code_103/opencv_103.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import cv2 as cv
2+
import os
3+
import numpy as np
4+
5+
6+
# 把目标图放在64x128的灰色图片中间,方便计算描述子
7+
def get_hog_descriptor(image):
8+
hog = cv.HOGDescriptor()
9+
h, w = image.shape[:2]
10+
rate = 64 / w
11+
image = cv.resize(image, (64, np.int(rate*h)))
12+
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
13+
bg = np.zeros((128, 64), dtype=np.uint8)
14+
bg[:,:] = 127
15+
h, w = gray.shape
16+
dy = (128 - h) // 2
17+
bg[dy:h+dy,:] = gray
18+
fv = hog.compute(bg, winStride=(8, 8), padding=(0, 0))
19+
return fv
20+
21+
def get_data(train_data, labels, path, lableType):
22+
for file_name in os.listdir(path):
23+
img_dir = os.path.join(path, file_name)
24+
img = cv.imread(img_dir)
25+
hog_desc = get_hog_descriptor(img)
26+
one_fv = np.zeros([len(hog_desc)], dtype=np.float32)
27+
for i in range(len(hog_desc)):
28+
one_fv[i] = hog_desc[i][0]
29+
train_data.append(one_fv)
30+
labels.append(lableType)
31+
return train_data, labels
32+
33+
def get_dataset(pdir, ndir):
34+
train_data = []
35+
labels = []
36+
train_data, labels = get_data(train_data, labels, pdir, lableType=1)
37+
train_data, labels = get_data(train_data, labels, ndir, lableType=-1)
38+
39+
return np.array(train_data, dtype=np.float32), np.array(labels, dtype=np.int32)
40+
41+
if __name__ == '__main__':
42+
a, b = get_dataset("pdir/", "ndir/")
43+
#cv.destroyAllWindows()
44+
print(a.shape, b.shape)

Diff for: python/code_103/pedestrian_02.png

728 KB
Loading

Diff for: python/code_104/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理-HOG特征和应用](https://zhuanlan.zhihu.com/p/75705284)

Diff for: python/code_104/opencv_104.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import cv2 as cv
2+
import os
3+
import numpy as np
4+
5+
# 把目标图放在64x128的灰色图片中间,方便计算描述子
6+
def get_hog_descriptor(image):
7+
hog = cv.HOGDescriptor()
8+
h, w = image.shape[:2]
9+
rate = 64 / w
10+
image = cv.resize(image, (64, np.int(rate*h)))
11+
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
12+
bg = np.zeros((128, 64), dtype=np.uint8)
13+
bg[:,:] = 127
14+
h, w = gray.shape
15+
dy = (128 - h) // 2
16+
bg[dy:h+dy,:] = gray
17+
fv = hog.compute(bg, winStride=(8, 8), padding=(0, 0))
18+
return fv
19+
20+
def get_data(train_data, labels, path, lableType):
21+
for file_name in os.listdir(path):
22+
img_dir = os.path.join(path, file_name)
23+
img = cv.imread(img_dir)
24+
hog_desc = get_hog_descriptor(img)
25+
one_fv = np.zeros([len(hog_desc)], dtype=np.float32)
26+
for i in range(len(hog_desc)):
27+
one_fv[i] = hog_desc[i][0]
28+
train_data.append(one_fv)
29+
labels.append(lableType)
30+
return train_data, labels
31+
32+
def get_dataset(pdir, ndir):
33+
train_data = []
34+
labels = []
35+
train_data, labels = get_data(train_data, labels, pdir, lableType=1)
36+
train_data, labels = get_data(train_data, labels, ndir, lableType=-1)
37+
38+
return np.array(train_data, dtype=np.float32), np.array(labels, dtype=np.int32)
39+
40+
41+
def svm_train(positive_dir, negative_dir):
42+
svm = cv.ml.SVM_create()
43+
svm.setKernel(cv.ml.SVM_LINEAR)
44+
svm.setType(cv.ml.SVM_C_SVC)
45+
svm.setC(2.67)
46+
svm.setGamma(5.383)
47+
trainData, responses = get_dataset(positive_dir, negative_dir)
48+
responses = np.reshape(responses, [-1, 1])
49+
svm.train(trainData, cv.ml.ROW_SAMPLE, responses)
50+
svm.save('svm_data.dat')
51+
52+
53+
def elec_detect(image):
54+
hog_desc = get_hog_descriptor(test_img)
55+
print(len(hog_desc))
56+
one_fv = np.zeros([len(hog_desc)], dtype=np.float32)
57+
for i in range(len(hog_desc)):
58+
one_fv[i] = hog_desc[i][0]
59+
one_fv = np.reshape(one_fv, [-1, len(hog_desc)])
60+
print(len(one_fv), len(one_fv[0]))
61+
svm = cv.ml.SVM_load('svm_data.dat')
62+
result = svm.predict(one_fv)[1]
63+
print(result)
64+
65+
66+
if __name__ == '__main__':
67+
#svm_train("D:/vcprojects/dataset/elec_watch/positive/", "D:/vcprojects/dataset/elec_watch/negative/")
68+
# cv.waitKey(0)
69+
test_img = cv.imread("test.jpg")
70+
elec_detect(test_img)
71+
#cv.destroyAllWindows()

0 commit comments

Comments
 (0)