-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_contours.py
More file actions
140 lines (100 loc) · 3.73 KB
/
sorting_contours.py
File metadata and controls
140 lines (100 loc) · 3.73 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import cv2
import numpy as np
# Load our image
image = cv2.imread('images/bunchofshapes.jpg')
cv2.imshow('0 - Original Image', image)
cv2.waitKey(0)
# Create a black image with same dimensions as our loaded image
blank_image = np.zeros((image.shape[0], image.shape[1], 3))
# Create a copy of our original image
orginal_image = image
# Grayscale our image
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# Find Canny edges
edged = cv2.Canny(gray, 50, 200)
cv2.imshow('1 - Canny Edges', edged)
cv2.waitKey(0)
# Find contours and print how many were found
contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
print ("Number of contours found = ", len(contours))
#Draw all contours
cv2.drawContours(blank_image, contours, -1, (0,255,0), 3)
cv2.imshow('2 - All Contours over blank image', blank_image)
cv2.waitKey(0)
# Draw all contours over blank image
cv2.drawContours(image, contours, -1, (0,255,0), 3)
cv2.imshow('3 - All Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
# Function we'll use to display contour area
def get_contour_areas(contours):
# returns the areas of all contours as list
all_areas = []
for cnt in contours:
area = cv2.contourArea(cnt)
all_areas.append(area)
return all_areas
# Load our image
image = cv2.imread('images/bunchofshapes.jpg')
orginal_image = image
# Let's print the areas of the contours before sorting
print("Contor Areas before sorting")
print (get_contour_areas(contours))
# Sort contours large to small
sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)
#sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)[:3]
print ("Contor Areas after sorting")
print (get_contour_areas(sorted_contours))
# Iterate over our contours and draw one at a time
for c in sorted_contours:
cv2.drawContours(orginal_image, [c], -1, (255,0,0), 3)
cv2.waitKey(0)
cv2.imshow('Contours by area', orginal_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
# Functions we'll use for sorting by position
def x_cord_contour(contours):
#Returns the X cordinate for the contour centroid
if cv2.contourArea(contours) > 10:
M = cv2.moments(contours)
return (int(M['m10']/M['m00']))
else:
pass
def label_contour_center(image, c):
# Places a red circle on the centers of contours
M = cv2.moments(c)
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
# Draw the countour number on the image
cv2.circle(image,(cx,cy), 10, (0,0,255), -1)
return image
# Load our image
image = cv2.imread('images/bunchofshapes.jpg')
orginal_image = image.copy()
# Computer Center of Mass or centroids and draw them on our image
for (i, c) in enumerate(contours):
orig = label_contour_center(image, c)
cv2.imshow("4 - Contour Centers ", image)
cv2.waitKey(0)
# Sort by left to right using our x_cord_contour function
contours_left_to_right = sorted(contours, key = x_cord_contour, reverse = False)
# Labeling Contours left to right
for (i,c) in enumerate(contours_left_to_right):
cv2.drawContours(orginal_image, [c], -1, (0,0,255), 3)
M = cv2.moments(c)
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
cv2.putText(orginal_image, str(i+1), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('6 - Left to Right Contour', orginal_image)
cv2.waitKey(0)
(x, y, w, h) = cv2.boundingRect(c)
# Let's now crop each contour and save these images
cropped_contour = orginal_image[y:y + h, x:x + w]
image_name = "output_shape_number_" + str(i+1) + ".jpg"
print (image_name)
cv2.imwrite(image_name, cropped_contour)
cv2.destroyAllWindows()