Skip to content

Commit 4db1aa5

Browse files
committed
add HOG feature extraction tutorial
1 parent 4b1caf4 commit 4db1aa5

File tree

8 files changed

+132
-0
lines changed

8 files changed

+132
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5353
- [How to Perform Malaria Cells Classification using TensorFlow 2 and Keras in Python](https://www.thepythoncode.com/article/malaria-cells-classification). ([code](machine-learning/malaria-classification))
5454
- [How to Make a Barcode Reader in Python](https://www.thepythoncode.com/article/making-a-barcode-scanner-in-python). ([code](general/barcode-reader))
5555
- [Image Transformations using OpenCV in Python](https://www.thepythoncode.com/article/image-transformations-using-opencv-in-python). ([code](machine-learning/image-transformation))
56+
- [How to Apply HOG Feature Extraction in Python](https://www.thepythoncode.com/article/hog-feature-extraction-in-python). ([code](machine-learning/hog-feature-extraction))
5657
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
5758
- [How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
5859
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Apply HOG Feature Extraction in Python](https://www.thepythoncode.com/article/hog-feature-extraction-in-python)
69.8 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"metadata": {
3+
"language_info": {
4+
"codemirror_mode": {
5+
"name": "ipython",
6+
"version": 3
7+
},
8+
"file_extension": ".py",
9+
"mimetype": "text/x-python",
10+
"name": "python",
11+
"nbconvert_exporter": "python",
12+
"pygments_lexer": "ipython3",
13+
"version": "3.6.6-final"
14+
},
15+
"orig_nbformat": 2,
16+
"kernelspec": {
17+
"name": "python36664bitea6884f10f474b21a2a2f022451e0d09",
18+
"display_name": "Python 3.6.6 64-bit"
19+
}
20+
},
21+
"nbformat": 4,
22+
"nbformat_minor": 2,
23+
"cells": [
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"#importing required libraries\n",
31+
"from skimage.io import imread\n",
32+
"from skimage.transform import resize\n",
33+
"from skimage.feature import hog\n",
34+
"import matplotlib.pyplot as plt\n",
35+
"%matplotlib inline"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"#reading the image\n",
45+
"img = imread('cat.jpg')\n",
46+
"plt.axis(\"off\")\n",
47+
"plt.imshow(img)\n",
48+
"print(img.shape)"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"metadata": {},
55+
"outputs": [],
56+
"source": [
57+
"#resizing image\n",
58+
"resized_img = resize(img, (128*4, 64*4))\n",
59+
"plt.axis(\"off\")\n",
60+
"plt.imshow(resized_img)\n",
61+
"print(resized_img.shape)"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": [
70+
"#creating hog features\n",
71+
"fd, hog_image = hog(resized_img, orientations=9, pixels_per_cell=(8, 8),\n",
72+
" \tcells_per_block=(2, 2), visualize=True, multichannel=True)\n",
73+
"print(fd.shape)\n",
74+
"plt.axis(\"off\")\n",
75+
"plt.imshow(hog_image, cmap=\"gray\")"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"# save the images\n",
85+
"plt.imsave(\"resized_img.jpg\", resized_img)\n",
86+
"plt.imsave(\"hog_image.jpg\", hog_image, cmap=\"gray\")"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": []
95+
}
96+
]
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#importing required libraries
2+
from skimage.io import imread
3+
from skimage.transform import resize
4+
from skimage.feature import hog
5+
import matplotlib.pyplot as plt
6+
7+
#reading the image
8+
img = imread('cat.jpg')
9+
plt.axis("off")
10+
plt.imshow(img)
11+
print(img.shape)
12+
13+
#resizing image
14+
resized_img = resize(img, (128*4, 64*4))
15+
plt.axis("off")
16+
plt.imshow(resized_img)
17+
plt.show()
18+
print(resized_img.shape)
19+
20+
#creating hog features
21+
fd, hog_image = hog(resized_img, orientations=9, pixels_per_cell=(8, 8),
22+
cells_per_block=(2, 2), visualize=True, multichannel=True)
23+
print(fd.shape)
24+
print(hog_image.shape)
25+
plt.axis("off")
26+
plt.imshow(hog_image, cmap="gray")
27+
plt.show()
28+
29+
# save the images
30+
plt.imsave("resized_img.jpg", resized_img)
31+
plt.imsave("hog_image.jpg", hog_image, cmap="gray")
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scikit-image
2+
matplotlib
Loading

0 commit comments

Comments
 (0)