forked from anshuln/ITSP-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_contours.py
56 lines (47 loc) · 1.32 KB
/
get_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
55
56
def min_max(array):
xmin=9999
xmax=-9999
ymin=9999
ymax=-9999
for i in range(0,len(array)):
xmin=min(xmin,array[i][0][0])
xmax=max(xmax,array[i][0][0])
ymin=min(ymin,array[i][0][1])
ymax=max(ymax,array[i][0][1])
return xmin,xmax,ymin,ymax
def area(img):
return img[0].shape[0]*img[0].shape[1]
def inside(im1,im2):
b=im1[1][0]>=im2[1][0] and im1[1][1]<=im2[1][1] and im1[1][2]>=im2[1][2] and im1[1][3]<=im1[1][3]
return b
import cv2
import numpy as np
def compress(recs):
recs=sorted(recs,key=area)
imgs=[]
pos=[]
for i in range(0,len(recs)):
f=0
for j in range(i+1,len(recs)):
if inside(recs[i],recs[j]) == True:
f=1
break
if f == 0:
imgs.append(recs[i][0])
pos.append([recs[i][1][0],recs[i][1][2]])
return(imgs,pos)
def get_smaller_images(path='Test1.jpg'):
gray=cv2.imread(path, 0)
#Might wanna resize here
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)
(__,cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts,key = cv2.contourArea, reverse = True)[:15]
ret=[]
for c in cnts:
xmin,xmax,ymin,ymax=min_max(c)
# print(min_max(c))
ret.append((image[ymin:ymax,xmin:xmax,:],[xmin,xmax,ymin,ymax]))
imgs,pos=compress(ret)
print(pos)
return imgs,pos