-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path44 Contours and its Introduction.py
79 lines (38 loc) · 1.57 KB
/
44 Contours and its Introduction.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
"""
@author: Osama Shakeel
---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
finding 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
img = cv2.imread("D:\\music\\logo.jpg")
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img1,20,255,0)
#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)
"""
drawcontour(img,cnts,id of contour,color,thickness)#here if we draw all
contour just pass -1
Draw the contours
"""
img = cv2.drawContours(img,cnts,-1,(176,10,15),4)
#ouput----
cv2.imshow("original===",img)
cv2.imshow("gray==",img1)
cv2.imshow("thresh==",thresh)
cv2.waitKey(0)
cv2.destroyAllwindow()