-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontour_testing.py
100 lines (82 loc) · 3.11 KB
/
contour_testing.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
99
100
import numpy as np
import cv2
import pprint
import subprocess
CONTOUR_THRESH = 10 # size of minimum allowed contour
def get_contours(im, method=cv2.RETR_TREE):
assert(im != None)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,method,cv2.CHAIN_APPROX_SIMPLE)
return contours
model_im = cv2.imread('images/swan.jpg')
# The model contour we work with.
model_c = get_contours(model_im, method=cv2.RETR_CCOMP)[0]
# print model_contours, len(model_contours)
# print hierarchy
# img_model = np.zeros(model_im.shape,np.uint8)
# colors = [(0,255,0), (255,0,0), (0,0,255), (255,255,0), (0,255,255), (255,0,255)]
#cv2.drawContours(img_model, model_c, -1, colors[i%6], 1)
#cv2.drawContours(img_model, [model_c], -1, (0,255,0), -1)
# cv2.imshow('model',img_model)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# Replace with Matlab? get edges
im = cv2.imread('images/ispy_edges.png')
assert(im != None)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
im_closure = cv2.morphologyEx(im, cv2.MORPH_CLOSE, kernel)
im_dilate = cv2.dilate(im, kernel, iterations=1)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(imgray, (5,5), 0)
ret,gauss_thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow('closure',im_closure)
cv2.imshow('dilate',im_dilate)
cv2.imshow('thresh,Gaussian',gauss_thresh)
gthresh_closure = cv2.morphologyEx(im, cv2.MORPH_CLOSE, kernel)
cv2.imshow('thresh,closure',gthresh_closure)
cv2.waitKey(0)
cv2.destroyAllWindows()
contours_closure = get_contours(im_closure)
contours_dilate = get_contours(im_dilate)
contours_gthresh = get_contours(gthresh_closure)
#contours = [c for c in contours if len(c) > CONTOUR_THRESH]
# Compute each contour cost_table.
# Run binary
# p = subprocess.Popen(['./shape_tree', cur_contour_file, model_file],
# stdout=subprocess.PIPE)
# for line in p.stdout:
# args = line.split(' ')
# if args[0] == 'best':
# s,e,num = args[1:]
# cost_tables.append((s,e,float(num)))
# elif args[0] == 'overall':
# num_overall = args[1:]
# p_status = p.wait()
# print num, 'overall', num_overall
# print 'cv2',cv2.matchShapes(c, model_c, 1, 0.0)
# img = np.zeros(im.shape, np.uint8)
# cv2.drawContours(img, c, -1, 255, 1)
# cv2.drawContours(img, model_c, -1, (0,255,0), 1)
# cv2.imshow('contour', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
for contours in [contours_closure, contours_dilate, contours_gthresh]:
print 'num',len(contours)
print 'Global image:'
img = np.zeros(im.shape,np.uint8)
colors = [(0,255,0), (255,0,0), (0,0,255), (255,255,0), (0,255,255), (255,0,255)]
for i,c in enumerate(contours):
# if len(c) < 10: continue
cv2.drawContours(img, [c], -1, colors[i%6], 1)
cv2.imshow('asdf',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# print 'Each contour closure:'
# for i,c in enumerate(contours_closure):
# img = np.zeros(im.shape,np.uint8)
# # if len(c) < 10: continue
# cv2.drawContours(img, [c], -1, colors[i%6], 1)
# cv2.imshow('img',img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()