-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_8.py
More file actions
64 lines (57 loc) · 2.18 KB
/
Chapter_8.py
File metadata and controls
64 lines (57 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Shape and Contour detection from image
import cv2
import numpy as np
# FUNCTION TO GET CONTOUR AND FINAL SHAPE DETECTION
def getContours(imgCanny):
# FIND CONTOUR OF EACH DETECTED SHAPE
contours,hierarchy = cv2.findContours(imgCanny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
# LOOP TO ACCESS EACH CONTOUR
for c in contours:
# GET AREA
area = cv2.contourArea(c)
print(area)
# DRAW CONTOUR ON THE DETECTED SHAPE (blue outline)
cv2.drawContours(imgC, c, -1, (255, 0, 0), 3)
# GET ARC LENGTH
arc = cv2.arcLength(c,True)
# GET APPROX CONTOUR
approx = cv2.approxPolyDP(c,0.02*arc,True)
print("NO. OF CORNERS:",len(approx))
# GET NO.OF CORNERS
objCorner = len(approx)
# DRAW BOUNDING RECTANGLE ON THE APPROX CONTOUR
x, y, w, h = cv2.boundingRect(approx)
if objCorner == 3: # IF NO. OF CORNERS IS 3
objText = "T" # IT IS TRIANGLE
elif objCorner == 4: # IF NO. OF CORNERS IS 4
aspRatio = w/h
if aspRatio > 0.8 and aspRatio < 1.05 :
objText = "S" # SQUARE
else:objText = "R" # RECTANGLE
elif objCorner > 4: # IF NO. OF CORNERS IS 5
objText = "C" # CIRCLE
else: objText = "None"
cv2.rectangle(imgC,(x,y),(x+w,y+h),(0,250,0),2)
# PUT THE SHAPE NAME ON THE DETECTED CONTOUR
cv2.putText(imgC,objText,(x+(w//2)-5,y+(h//2)-5),
cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0),2)
# GET INPUT IMAGE
path = "Resources/imgShape.jpg"
img = cv2.imread(path)
img = cv2.resize(img,(250,250))
imgC = img.copy() # GET A COPY OF IMAGE
# CONVERT IMAGE TO GRAYSCALE
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# GET BLURRED IMAGE
imgBlur = cv2.GaussianBlur(imgGray,(7,7),2)
# EDGE DETECTED IMAGE
imgCanny = cv2.Canny(imgBlur,50,50)
# CALL FUNCTION TO DETECT SHAPE
getContours(imgCanny)
# JOIN IMAGES
stack1 = np.hstack((imgGray,imgBlur,imgCanny))
stack2 = np.hstack((img,imgC))
# SHOW IMAGES
cv2.imshow("Gray, Blurred, Edge Detected Image",stack1)
cv2.imshow("Input & Output Image",stack2)
cv2.waitKey(0)