gpt4 book ai didi

python - 平均脸-算法

转载 作者:太空狗 更新时间:2023-10-29 17:33:05 25 4
gpt4 key购买 nike

问题和代码在最后我希望这个问题属于这里而不属于 TCS 的堆栈。我正在尝试通过 Turk and Pentland's "Eigenfaces for Recognition" 中的算法.

第 74 页(左栏最后一段):

Let the training (...) The average face of the set is defined by [*]

其中 [*] 是一个等式,表明平均人脸等于图像总和除以其计数。为了使用这个等式,我使用 OpenCV 和 numpy 创建了 python 脚本。

在第 75 页上有图 1。它应该代表图 1 中的平均面孔。(第 74 页)这就是我想要实现的目标。

作为人脸集,我使用了 Faces94 中的所有人脸.当我计算传统平均值 (1/M*sum) 时,结果如下所示:

enter image description here

这与预期相去甚远,主要是因为那些奇怪的“ Blob ”。但是,当我计算平均值时,面孔比实际多(例如 1/(2*M) * 总和),结果看起来更准确:

enter image description here

我认为在转换 int8<->int 时存在一些问题,但我无法证明。如果有人发现代码有任何问题,即使不是解决方案也请告诉我。

问题:我做错了什么/如何做才能获得更好的结果。这是代码:

import numpy as np
import glob
import cv2
from cv2 import imread

dir = "../images/faces94/**/**.jpg"

files = list(glob.iglob(dir, recursive=True))
img = np.zeros(imread(files[0],0).shape)
img = img.astype('int')
for i in range(len(files)):
img += imread(files[i],0).astype('int')

img = np.divide(img,len(files)*2) # HERE you can change it to np.divide(img,len(files)) in order to see bad result
img = np.mod(img,128)
img = img.astype(np.int8)

cv2.imshow("image", img)
cv2.waitKey(0)

最佳答案

感谢@Divakar我在我的代码中发现了两个问题。

  1. opencv 中的图像数组是基于 uint8 而不是 int8
  2. 我正在做模数 (np.mod(img,128)),因此我的图像范围从 0 到 127,应该是从 0 到 255。

关于python - 平均脸-算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41452508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com