-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpre_segment.py
76 lines (62 loc) · 2.67 KB
/
pre_segment.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
import cv2
import glob
import os
import os.path as osp
import numpy as np
from tqdm import tqdm
class Drawcnts_and_cut(object):
def __call__(self, original_img, box):
filter = lambda x: 0 if x < 0 else x
Xs = [filter(i[0]) for i in box]
Ys = [filter(i[1]) for i in box]
# Xs = [i[0] for i in box]
# Ys = [i[1] for i in box]
x1 = min(Xs)
x2 = max(Xs)
y1 = min(Ys)
y2 = max(Ys)
hight = y2 - y1
width = x2 - x1
crop_img = original_img[y1:y1 + hight, x1:x1 + width]
return crop_img
class Crop_image(object):
def __init__(self):
# set color bound
self.lower_grey = np.array([0, 0, 46]) # 灰色的hsv范围
self.upper_grey = np.array([180, 43, 220])
self.drawcnts_and_cut = Drawcnts_and_cut()
def __call__(self, img_fname, source_path, target_path):
image = cv2.imread(osp.join(source_path, img_fname))
image = cv2.resize(image, None, fx=0.5, fy=0.5)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, self.lower_grey, self.upper_grey)
cv2.imshow("draw_img", image)
cv2.waitKey(0)
cv2.imshow("draw_img", hsv)
cv2.waitKey(0)
cv2.imshow("draw_img", mask)
cv2.waitKey(0)
kernel = np.ones((5, 5), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# img_grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# ret, thresh = cv2.threshold(img_grey, 127, 255, 0)
# _, contours, hierarcy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
_, contours, hierarcy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
c = sorted(contours, key=cv2.contourArea, reverse=True)[0]
# compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect))
# draw a bounding box arounded the detected barcode and display the image
# draw_img = cv2.drawContours(image.copy(), [box], -1, (0, 0, 255), 3)
draw_img = image.copy()
draw_img = self.drawcnts_and_cut(draw_img, box)
# cv2.imwrite(osp.join(source_path, img_fname.split('.')[0]+'_.'+img_fname.split('.')[1]), draw_img)
# return draw_img
if __name__ == '__main__':
source_path = "guangdong_round2_test_a_20181011"
target_path = "pre_image"
image_list = os.listdir(source_path)
image_list = sorted(image_list, key=lambda x: int(x.split('.')[0]))
for img_fname in tqdm(image_list[111:112]):
Crop_image()(img_fname, source_path, target_path)