-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestImage.py
59 lines (47 loc) · 1.69 KB
/
testImage.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
import cv2
from os.path import basename
from glob import glob
def get_contours(img):
# First make the image 1-bit and get contours
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 150, 255, 0)
cv2.imwrite('thresh.jpg', thresh)
img2, contours, hierarchy = cv2.findContours(thresh, 1, 2)
# filter contours that are too large or small
size = get_size(img)
contours = [cc for cc in contours if contourOK(cc, size)]
return contours
def get_size(img):
ih, iw = img.shape[:2]
return iw * ih
def contourOK(cc, size=1000000):
x, y, w, h = cv2.boundingRect(cc)
if w < 50 or h < 50: return False # too narrow or wide is bad
area = cv2.contourArea(cc)
return area < (size * 0.5) and area > 200
def find_boundaries(img, contours):
# margin is the minimum distance from the edges of the image, as a fraction
ih, iw = img.shape[:2]
minx = iw
miny = ih
maxx = 0
maxy = 0
for cc in contours:
x, y, w, h = cv2.boundingRect(cc)
if x < minx: minx = x
if y < miny: miny = y
if x + w > maxx: maxx = x + w
if y + h > maxy: maxy = y + h
return (minx, miny, maxx, maxy)
def crop(img, boundaries):
minx, miny, maxx, maxy = boundaries
return img[miny:maxy, minx:maxx]
def process_image(fname):
img = cv2.imread(fname)
contours = get_contours(img)
#cv2.drawContours(img, contours, -1, (0,255,0)) # draws contours, good for debugging
bounds = find_boundaries(img, contours)
cropped = crop(img, bounds)
if get_size(cropped) < 400: return # too small
cv2.imwrite('cropped/' + basename(fname), cropped)
process_image('images/20191019-002735.jpeg')