Skip to content

Commit b38150e

Browse files
committed
add more files of opencv tutorials
1 parent 733787c commit b38150e

9 files changed

+120
-1
lines changed

Readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* image_blending_using_pyramids_opencv.py
2626
* Find_and_draw_contours_using_opencv.py
2727
* basic_motion_detection_using_opencv.py
28+
* shape_detection_opencv.py
29+
* histogram_opencv.py
30+
* template_matching_opencv.py
2831

2932
## Helpful Documentations⭐:
3033
* OpenCV : https://docs.opencv.org/master/

histogram_opencv.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import cv2
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
img = cv2.imread('test_images/lena.jpg', 0)
6+
# img = np.zeros((200,200) , np.uint8)
7+
# cv2.rectangle(img ,(0,50) , (100,100), (127) , -1)
8+
9+
# b,g,r = cv2.split(img)
10+
11+
# cv2.imshow('Main image' , img)
12+
# cv2.imshow('Blue Channel' , b)
13+
# cv2.imshow('Green Channel' , g)
14+
# cv2.imshow('Red Channel' , r)
15+
16+
# Histogram in Matplotlib
17+
18+
# plt.hist(img.ravel() , 256 , [0,256])
19+
# plt.hist(b.ravel() , 256 , [0,256])
20+
# plt.hist(g.ravel() , 256 , [0,256])
21+
# plt.hist(r.ravel() , 256 , [0,256])
22+
# plt.title('Histogram of Color Image(lena.jpg)')
23+
# plt.show()
24+
25+
26+
# Histogram in Opencv
27+
28+
hist = cv2.calcHist([img], [0] , None , [256],[0,256])
29+
plt.plot(hist)
30+
plt.show()
31+
32+
33+
cv2.waitKey(0)
34+
cv2.destroyAllWindows()

output/Readme.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
<img src="edge_detection_algorithms.png">
1212

1313
# Canny Edge Detection
14-
<img src="canny_edge_detection.png">
14+
<img src="canny_edge_detection.png">
15+
16+
# Approximation Of Geometircal Shapes
17+
<img src="approximation_of_shapes.png">

output/approximation_of_shapes.png

38.1 KB
Loading

output/histogram_of_image.png

18.9 KB
Loading

shape_detection_opencv.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import cv2
2+
# import matplotlib.pyplot as plt
3+
4+
img = cv2.imread('test_images/shapes.jpg')
5+
6+
imgGrey = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
7+
8+
# find threshold
9+
_,thresh = cv2.threshold(imgGrey ,240 ,255 , cv2.THRESH_BINARY)
10+
11+
# find contours
12+
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE , cv2.CHAIN_APPROX_NONE)
13+
14+
# 2nd Argument epsilon : approximation accuracy
15+
for contour in contours:
16+
approx = cv2.approxPolyDP(contour, 0.01*cv2.arcLength(contour,True) , True)
17+
cv2.drawContours(img,[approx] , 0 , (255,0,0) , 5)
18+
19+
# find coordinates
20+
x = approx.ravel()[0]
21+
y = approx.ravel()[1]
22+
23+
# shape name
24+
if len(approx) == 3:
25+
cv2.putText(img , 'Triangle' , (x-10,y-20) , cv2.FONT_HERSHEY_SIMPLEX , 0.5 , (255,255,255),2)
26+
27+
elif len(approx) == 4:
28+
x, y , w ,h = cv2.boundingRect(approx)
29+
aspect_ratio = float(w)/h
30+
31+
if aspect_ratio >= 0.95 and aspect_ratio <= 1.05:
32+
cv2.putText(img , 'Square' , (x-10,y-20) , cv2.FONT_HERSHEY_SIMPLEX , 0.5 , (0,0,0),2)
33+
else:
34+
cv2.putText(img , 'Rectangle' , (x-10,y-20) , cv2.FONT_HERSHEY_SIMPLEX , 0.5 , (0,0,0),2)
35+
36+
elif len(approx) == 5:
37+
cv2.putText(img , 'Pentagon' , (x-10,y-10) , cv2.FONT_HERSHEY_SIMPLEX , 0.5 , (0,0,0),2)
38+
39+
elif len(approx) == 6:
40+
cv2.putText(img , 'Hexagon' , (x-10,y-20) , cv2.FONT_HERSHEY_SIMPLEX , 0.5 , (0,0,0),2)
41+
42+
elif len(approx) == 10:
43+
cv2.putText(img , 'Star' , (x-10,y-20) , cv2.FONT_HERSHEY_SIMPLEX , 0.5 , (0,0,0),2)
44+
45+
else:
46+
cv2.putText(img , 'Circle' , (x-10,y-20) , cv2.FONT_HERSHEY_SIMPLEX , 0.5 , (0,0,0),2)
47+
48+
cv2.imshow('Main Image',img)
49+
50+
cv2.waitKey(0)
51+
cv2.destroyAllWindows()
52+
53+
# plt.imshow(img , 'gray')
54+
# plt.title('Approximation of Shapes')
55+
# plt.xticks([]) , plt.yticks([])
56+
# plt.show()
57+

test_images/messi_face.jpg

21.7 KB
Loading

test_images/shapes.jpg

16.5 KB
Loading
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import cv2
2+
import numpy as np
3+
4+
img = cv2.imread('test_images/messi5.jpg')
5+
grey_img = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
6+
7+
template = cv2.imread('test_images/messi_face.jpg', 0)
8+
9+
w, h = template.shape[::-1]
10+
11+
res = cv2.matchTemplate(grey_img , template , cv2.TM_CCOEFF_NORMED)
12+
threshold = 0.95
13+
loc = np.where(res >= threshold)
14+
# print(loc)
15+
16+
for pt in zip(*loc[::-1]):
17+
cv2.rectangle(img, pt , (pt[0]+w , pt[1]+h) , (0,0,255),2)
18+
19+
20+
cv2.imshow('image' , img)
21+
cv2.waitKey(0)
22+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)