<P>我继续写了C++中的函数作为Python。它不是最干净的,但它很管用!如果您将这段python代码与<a href="https://stackoverflow.com/questions/8757149/any-tips-on-confidence-score-for-face-verification-as-opposed-to-face-recogniti"> another C++ example</a>中的高级概念结合起来,您就可以准确地完成您正在尝试的工作。在</p>
<pre><code> # projects samples into the LDA subspace
def subspace_project(eigenvectors_column, mean, source):
source_rows = len(source)
source_cols = len(source[0])
if len(eigenvectors_column) != source_cols * source_rows:
raise Exception("wrong shape")
flattened_source = []
for row in source:
flattened_source += [float(num) for num in row]
flattened_source = np.asarray(flattened_source)
delta_from_mean = cv2.subtract(flattened_source, mean)
# flatten the matrix then convert to 1 row by many columns
delta_from_mean = np.asarray([np.hstack(delta_from_mean)])
empty_mat = np.array(eigenvectors_column, copy=True) # this is required for the function call but unused
result = cv2.gemm(delta_from_mean, eigenvectors_column, 1.0, empty_mat, 0.0)
return result
# reconstructs projections from the LDA subspace
def subspace_reconstruct(eigenvectors_column, mean, projection, image_width, image_height):
if len(eigenvectors_column[0]) != len(projection[0]):
raise Exception("wrong shape")
empty_mat = np.array(eigenvectors_column, copy=True) # this is required for the function call but unused
# GEMM_2_T transposes the eigenvector
result = cv2.gemm(projection, eigenvectors_column, 1.0, empty_mat, 0.0, flags=cv2.GEMM_2_T)
flattened_array = result[0]
flattened_image = np.hstack(cv2.add(flattened_array, mean))
flattened_image = np.asarray([np.uint8(num) for num in flattened_image])
all_rows = []
for row_index in xrange(image_height):
row = flattened_image[row_index * image_width: (row_index + 1) * image_width]
all_rows.append(row)
image_matrix = np.asarray(all_rows)
image = normalize_hist(image_matrix)
return image
def normalize_hist(face):
face_as_mat = np.asarray(face)
equalized_face = cv2.equalizeHist(face_as_mat)
equalized_face = cv.fromarray(equalized_face)
return equalized_face
</code></pre>