-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching_contours.py
54 lines (42 loc) · 1.74 KB
/
matching_contours.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
import sys
import cv2
import numpy as np
# extract reference contour from the image
def get_ref_contour(img):
ref_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(ref_gray, 127, 255, 0) # thresholding binary (THRESH_BINARY)
# find all contours in the threshold image. the values for second and third parameter are restricted to certain possible number
# of possible values
contours, hierarchy = cv2.findContours(thresh, 1, 2)
for contour in contours:
area = cv2.contourArea(contour)
img_area = img_shape[0] * img_shape[1]
if 0.05 < area/float(img_area) < 0.8:
return contour
# Extract all the contours from the image
def get_all_contours(img):
ref_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(ref_gray, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, 1, 2)
return contours
if __name__ == '__main__':
# boomerang reference image
img1 = cv2.imread(sys.argv[1])
# image containing all shapes
img2 = cv2.imread(sys.argv[2])
# Extract the reference contour
ref_contour = get_ref_contour(img1)
# Extract all the contours from the input image
input_contours = get_all_contours(img2)
closest_contour = input_contours[0]
min_dist = sys.maxint
# finding closest contour
for contour in input_contours:
# Matching shapes and taking the closest one
ret = cv2.matchShapes(ref_contour, contour, 1, 0.0)
if ret < min_dist:
min_dist = ret
closest_contour = contour
cv2.drawContours(img2, [closest_contour], -1, (0,0,0), 3)
cv2.imshow('window', img2)
cv2.waitKey()