-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.py
More file actions
111 lines (85 loc) · 3.85 KB
/
process.py
File metadata and controls
111 lines (85 loc) · 3.85 KB
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
101
102
103
104
105
106
107
108
109
110
111
import numpy as np
import cv2
def getNorm (e):
if (e is None):
print("utils.py getNorm Error #001")
return None
e = e.copy()
e[e<0.0] = 0.0
normalizedImg = e.copy()#np.empty(e.shape, np.float32)
cv2.normalize(e, normalizedImg, 0, 255, cv2.NORM_MINMAX)
del e
return normalizedImg
def detect_blobs(image, min_cutoff = 20, blob_threshold = 220, verbose=False):
if (image is None):
print("detect_blobs() Error #001")
return None
lstBlob = []
lstMin = []
lstMax = []
lstMean = []
lstXY = []
image = image.clip(0,255)
if np.any(image > min_cutoff):
image = getNorm(image).clip(0,255).astype(np.uint8)
large = np.pad(image, 1)
temp, thresh = cv2.threshold(cv2.bitwise_not(large), blob_threshold, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = [a for a in contours if cv2.contourArea(a) > 2 and cv2.contourArea(a) < ((large.shape[0]-1) * (large.shape[1]-1))]
contours.sort(key = lambda a: cv2.contourArea(a))
for max_contour in contours:
xmax, ymax = np.max(max_contour.reshape(len(max_contour),2), axis=0)
xmin, ymin = np.min(max_contour.reshape(len(max_contour),2), axis=0)
xmax, ymax = min(xmax + 1, large.shape[1]), min(ymax + 1, large.shape[0])
xmin, ymin = max(xmin, 0), max(ymin, 0)
blob = large[ymin:ymax,xmin:xmax].astype(np.float32)
M = cv2.moments(max_contour)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
lstBlob.append(blob)
lstMin.append(xmax-xmin)
lstMax.append(ymax-ymin)
lstXY.append([cX, cY])
return lstBlob, lstMin, lstMax, lstXY
else:
if verbose:
print("detect_blobs() WARNING: image values are smaller than cutoff. cutoff value: %.01f" % cutoff)
return [], [], [], []
def pad_with(vector, pad_width, iaxis, kwargs):
pad_value = kwargs.get('padder', 0)
vector[:pad_width[0]] = pad_value
if pad_width[1] != 0:
vector[-pad_width[1]:] = pad_value
def getBlobBasedOnXYSize(matrix, xy, size):
#print("getBlobBasedOnXYSize() XY", xy)
xy = np.round(xy).astype(int)
size = np.ceil(size / 2).astype(int)
xMax = xy[0] + size[1] + 1
yMax = xy[1] + size[0] + 1
xMin = xy[0] - size[1] - 1
yMin = xy[1] - size[0] - 1
lowerY, upperY = max(yMin, 0), min(yMax, matrix.shape[0])
lowerX, upperX = max(xMin, 0), min(xMax, matrix.shape[1])
#size = (size * 2.0).astype(int)
#print("getBlobBasedOnXYSize() Area", xy, 'Size',size ,lowerY,upperY, lowerX,upperX)
blob = matrix[lowerY:upperY, lowerX:upperX]
template = np.zeros([(size[0]+1)*2,(size[1]+1)*2])
try:
template[0:(size[0]+1)*2, 0:(size[1]+1)*2] = blob
except ValueError:
y_lower_boundary = np.abs(min(yMin,0))
y_upper_boundary = (size[1]+1)*2 + min(0,matrix.shape[0]-yMax)
x_lower_boundary = np.abs(min(xMin,0))
x_upper_boundary = (size[0]+1)*2 + min(0,matrix.shape[1]-xMax)
template[y_lower_boundary:y_upper_boundary, x_lower_boundary:x_upper_boundary] = blob
return template.astype(np.float32)
def aligneCentre(AprilTagPixelSizeInMM, PixelSizeInMM, markerPixels, img):
shape = img.shape
size = int(np.round(AprilTagPixelSizeInMM / PixelSizeInMM * markerPixels))
clear = np.zeros(shape, np.float32)
xy = np.round(np.array(shape)/2).astype(int)
dim = np.array([xy - size/2, xy + size/2]).astype(int)
clear[dim[0,0]: dim[1,0], dim[0,1]: dim[1,1]] = np.ones((size, size), np.float32) * img.mean()
#clear = clear.astype(np.float32)
imgA = aligneImageECC(clear, img)
return imgA