def find_nn(point, neighborhood):
"""
Finds the nearest neighborhood of a vector.
Args:
point (float array): The initial point.
neighborhood (numpy float matrix): The points that are around the initial point.
Returns:
float array: The point that is the nearest neighbor of the initial point.
integer: Index of the nearest neighbor inside the neighborhood list
"""
min_dist = float('inf')
nn = neighborhood[0]
nn_idx = 0
for i in range(len(neighborhood)):
neighbor = neighborhood[i]
dist = cv2.norm(point - neighbor)
if dist < min_dist:
min_dist = dist
nn = neighbor
nn_idx = i
return nn, nn_idx
python类norm()的实例源码
def extractFeatures(self):
if len(self.image) == 0:
print 'Warning: No image detected. Features not extracted.'
return None
else:
self.net.blobs['data'].reshape(1, 3, self.crop, self.crop)
self.net.blobs['data'].data[...] = self.transformer.preprocess('data', self.image)
self.net.forward()
features = self.net.blobs[self.layer].data.copy()
features = np.reshape(features, (features.shape[0], -1))[0]
if cv2.norm(features, cv2.NORM_L2) > 0:
features = features / cv2.norm(features, cv2.NORM_L2)
return features.tolist()
def blur_measure(im):
""" See cv::videostab::calcBlurriness """
H, W = im.shape[:2]
gx = cv2.Sobel(im, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(im, cv2.CV_32F, 0, 1)
norm_gx, norm_gy = cv2.norm(gx), cv2.norm(gy)
return 1.0 / ((norm_gx ** 2 + norm_gy ** 2) / (H * W + 1e-6))
def blur_detect(im, threshold=7):
"""
Negative log-likelihood on the inverse gradient norm,
normalized by image size
"""
nll = -np.log(blur_measure(im))
return nll > threshold, nll
def normalize_result(webcam, idcard):
diff_correy = cv2.norm(settings.COREY_MATRIX, idcard, cv2.NORM_L2)
diff_wilde = cv2.norm(settings.WILDE_MATRIX, idcard, cv2.NORM_L2)
diff_min = diff_correy if diff_correy < diff_wilde else diff_wilde
diff = cv2.norm(webcam, idcard, cv2.NORM_L2)
score = float(diff) / float(diff_min)
percentage = (1.28 - score * score * score) * 10000 / 128
return {
'percentage': percentage,
'score': score,
'message': utils.matching_message(score)
}
def compare_similarity(img1, img2, mode="opencv"):
if mode == "opencv":
return cv2.norm(img1, img2)
if mode == "numpy":
return np.linalg.norm(img1-img2)
if mdoe == "naive_least_square":
return np.sqrt(np.sum((img1-img2)**2))