-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path46 Contour Area , Approximation and Convex hull.py
98 lines (59 loc) · 2.71 KB
/
46 Contour Area , Approximation and Convex hull.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
@author:
---Contours---
Contours can be explained simply as a curve joining all the continuous points
(along the boundary), having same color or intensity.
The contours are a useful tool for shape analysis and object detection and recognition
For better accuracy, use binary images and also apply edge detection before
findig countours.
findCountour function manipulate original imge so copy it before proceeding.
findContour is like finding white object from black background.
so you must turn image in white and background is black.
We have to find and draw contours as per the requirement
"""
import cv2
import numpy as np
"""
----Contour Area , Approximation and Convex hull---
"""
#Find countour area , aprroximation and convex hull
img = cv2.imread("D:\\music\\shapes.png")
img = cv2.resize(img,(600,700))
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img1,220,255,cv2.THRESH_BINARY_INV)
#findcontour(img,contour_retrival_mode,method)
cnts,hier = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#Here cnts is a list of contours. ANd each contour is an array with x, y cordinate
#hier variable called hierarchy and it contain image information.
print("Number of contour==",cnts,"\ntotal contour==",len(cnts))
#print("Hierarchy==\n",hier)
area1 = []
# loop over the contours
for c in cnts:
# compute the center of the contour
#an image moment is a certain particular weighted average (moment) of the image pixels
M = cv2.moments(c)
#print("M==",M)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
#find area of contour
area = cv2.contourArea(c)
area1.append(area)
if area<20000:
#contour Approx - it is use to approx shape with less number of vertices
epsilon = 0.01*cv2.arcLength(c,True) #arc lenght take contour and return its perimeter
data= cv2.approxPolyDP(c,epsilon,True)
#Convexhull is used to provide proper contours convexity.
hull = cv2.convexHull(data)
cv2.drawContours(img, [c], -1, (50, 50, 150), 2)
x,y,w,h = cv2.boundingRect(hull)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(125,10,20),5)
# draw the contour and center of the shape on the image
cv2.drawContours(img, [c], -1, (50, 100, 50), 2)
cv2.circle(img, (cX, cY), 7, (255, 255, 255), -1)
cv2.putText(img, "center", (cX - 20, cY - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
cv2.imshow("original===",img)
cv2.imshow("gray==",img1)
cv2.imshow("thresh==",thresh)
cv2.waitKey(0)