Skip to content

Commit bb45a76

Browse files
committed
Open-CV-Implementations
1 parent 5dd4a13 commit bb45a76

File tree

5 files changed

+262
-2
lines changed

5 files changed

+262
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ This is a repository of code files which can be used to perform some basic tasks
4141
* [Thresholding](https://github.com/altruistcoder/Open-CV-Implementations/blob/master/blurring.ipynb):
4242

4343
Python Code to perform different types of thresholding such as Simple, Inverse and Adaptive Thresholding on an image using OpenCV.
44+
45+
* [Gradients and Edge Detection](https://github.com/altruistcoder/Open-CV-Implementations/blob/master/gradients_and_edge_detection.ipynb):
46+
47+
Python Code to find Gradients of an image using Laplacian and Sobel method and also to perform Canny Edge Detection on an image using OpenCV.
48+
49+
* [Contours](https://github.com/altruistcoder/Open-CV-Implementations/blob/master/contours.ipynb):
50+
51+
Python Code to find & draw Contours present in an image using OpenCV.

blurring.ipynb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"outputs": [],
1515
"source": [
1616
"import numpy as np\n",
17-
"import matplotlib.pyplot as plt\n",
1817
"import cv2"
1918
]
2019
},

contours.ipynb

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Finding Contours in an Image"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import numpy as np\n",
17+
"import cv2"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 2,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"image = cv2.imread('sample_image.jpg')\n",
27+
"cv2.imshow(\"Original\", image)\n",
28+
"cv2.waitKey(0)\n",
29+
"cv2.destroyAllWindows()"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 3,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
39+
"cv2.imshow(\"Grayscaled Image\", gray_image)\n",
40+
"cv2.waitKey(0)\n",
41+
"cv2.destroyAllWindows()"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 4,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"blurred_image = cv2.GaussianBlur(gray_image, (11, 11), 0)\n",
51+
"cv2.imshow(\"Blurred Image\", blurred_image)\n",
52+
"cv2.waitKey(0)\n",
53+
"cv2.destroyAllWindows()"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 5,
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"name": "stdout",
63+
"output_type": "stream",
64+
"text": [
65+
"No. of Contours in the image is 0\n"
66+
]
67+
}
68+
],
69+
"source": [
70+
"canny = cv2.Canny(blurred_image, 30, 150)\n",
71+
"cv2.imshow(\"Canny\", canny)\n",
72+
"\n",
73+
"(image_returned, counts, hierarchy) = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
74+
"print(\"No. of Contours in the image is {}\".format(len(counts)))\n",
75+
"\n",
76+
"img = gray_image.copy()\n",
77+
"cv2.drawContours(img, counts, -1, (0, 0, 0), 5)\n",
78+
"cv2.imshow(\"Image with Contours\", img)\n",
79+
"\n",
80+
"cv2.waitKey(0)\n",
81+
"cv2.destroyAllWindows()"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": []
90+
}
91+
],
92+
"metadata": {
93+
"kernelspec": {
94+
"display_name": "Python 3",
95+
"language": "python",
96+
"name": "python3"
97+
},
98+
"language_info": {
99+
"codemirror_mode": {
100+
"name": "ipython",
101+
"version": 3
102+
},
103+
"file_extension": ".py",
104+
"mimetype": "text/x-python",
105+
"name": "python",
106+
"nbconvert_exporter": "python",
107+
"pygments_lexer": "ipython3",
108+
"version": "3.5.2"
109+
}
110+
},
111+
"nbformat": 4,
112+
"nbformat_minor": 2
113+
}

gradients_and_edge_detection.ipynb

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Gradients and Edge Detection"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Gradients of an Image"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"import numpy as np\n",
24+
"import cv2"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 2,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"image = cv2.imread('sample_image.jpg')\n",
34+
"cv2.imshow(\"Original\", image)\n",
35+
"cv2.waitKey(0)\n",
36+
"cv2.destroyAllWindows()"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 3,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
46+
"cv2.imshow(\"Grayscaled Image\", gray_image)\n",
47+
"cv2.waitKey(0)\n",
48+
"cv2.destroyAllWindows()"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"metadata": {},
54+
"source": [
55+
"### Laplacian Gradient"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 4,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"lap_gradient = cv2.Laplacian(gray_image, cv2.CV_64F)\n",
65+
"lap_gradient = np.uint8(np.absolute(lap_gradient))\n",
66+
"cv2.imshow(\"Laplacian Gradient\", lap_gradient)\n",
67+
"cv2.waitKey(0)\n",
68+
"cv2.destroyAllWindows()"
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"metadata": {},
74+
"source": [
75+
"### Sobel Gradient"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 5,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"sobelX = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0) # For Vertical Edge Features\n",
85+
"sobelY = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1) # For Horizontal Edge Features\n",
86+
"sobelX = np.uint8(np.absolute(sobelX))\n",
87+
"sobelY = np.uint8(np.absolute(sobelY))\n",
88+
"sobelCombined = cv2.bitwise_or(sobelX, sobelY)\n",
89+
"cv2.imshow(\"SobelX Gradient\", sobelX)\n",
90+
"cv2.imshow(\"SobelY Gradient\", sobelY)\n",
91+
"cv2.imshow(\"Sobel Combined Gradient\", sobelCombined)\n",
92+
"cv2.waitKey(0)\n",
93+
"cv2.destroyAllWindows()"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"## Canny Edge Detection"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 7,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)\n",
110+
"cv2.imshow(\"Blurred Image\", blurred_image)\n",
111+
"\n",
112+
"canny_edge_image = cv2.Canny(blurred_image, 30, 150)\n",
113+
"cv2.imshow(\"Canny Edge Detection\", canny_edge_image)\n",
114+
"\n",
115+
"cv2.waitKey(0)\n",
116+
"cv2.destroyAllWindows()"
117+
]
118+
}
119+
],
120+
"metadata": {
121+
"kernelspec": {
122+
"display_name": "Python 3",
123+
"language": "python",
124+
"name": "python3"
125+
},
126+
"language_info": {
127+
"codemirror_mode": {
128+
"name": "ipython",
129+
"version": 3
130+
},
131+
"file_extension": ".py",
132+
"mimetype": "text/x-python",
133+
"name": "python",
134+
"nbconvert_exporter": "python",
135+
"pygments_lexer": "ipython3",
136+
"version": "3.5.2"
137+
}
138+
},
139+
"nbformat": 4,
140+
"nbformat_minor": 2
141+
}

thresholding.ipynb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"outputs": [],
1515
"source": [
1616
"import numpy as np\n",
17-
"import matplotlib.pyplot as plt\n",
1817
"import cv2"
1918
]
2019
},

0 commit comments

Comments
 (0)