-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageHandle.py
71 lines (59 loc) · 2.16 KB
/
ImageHandle.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
import cv2
import base64
import numpy as np
def splitImg(image):
# 先将图像转化成灰度,再转化成二值图像
mask = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
_, mask = cv2.threshold(mask, 125, 255, cv2.THRESH_BINARY)
# 检测边缘
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
images = []
locs = []
for c in contours:
_, r = cv2.minEnclosingCircle(c) # 找到最小圆,并返回圆心坐标和半径
x, y, w, h = cv2.boundingRect(c)
# x, y, r = (int(x), int(y), int(r))
if 100 < r < 300:
img_cut = image[y: y + h, x: x + w]
# cv2.imwrite('img_cut.jpg', img_cut)
images.append(img_cut)
locs.append((x, y, w, h))
return images, locs
def CVEncodeb64(image):
encoded, buffer = cv2.imencode('.jpg', image)
return base64.b64encode(buffer)
def b64DecodeCV(image):
img = base64.b64decode(image)
npimg = np.frombuffer(img, dtype=np.uint8)
return cv2.imdecode(npimg, 1)
# def synchronize(locs, weights):
# num_conflict = False
# if len(locs) != len(weights):
# num_conflict = True
# print("> numbers of splited images and weight sensors conflict")
# return num_conflict, None
#
# # get indexes of x in locs from small to large
# # weight_sorts = sorted(range(len(locs)), key=lambda i: locs[i][0])
# # weights = [weights[i] for i in weight_sorts]
# locs_array = np.array([locs[i][0] for i in range(len(locs))])
# locs_sorts = np.sort(locs_array)
# weights_index = []
# for loc in locs_array:
# weights_index.append(np.where(locs_sorts == loc)[0][0])
#
# weights = [weights[i] for i in weights_index]
#
# return num_conflict, weights
def sortImgByHX711(images, locs, got_weight):
sync_images = [None, None, None]
locs_x = np.array(locs)[:, 0]
# 根据locs_x的大小逆序对images排序
images = np.array(images)[locs_x.argsort()[::-1]].tolist()
index = 0
for i in range(got_weight.size):
if not got_weight[i]:
continue
sync_images[i] = images[index]
index += 1
return sync_images