# How to create Positive and Negative Eigenfaces using Mean image?

**URL:** <https://forum.opencv.org/t/how-to-create-positive-and-negative-eigenfaces-using-mean-image/8561>\
**Category:** Python\
**Tags:** facerecognition, face\
**Created:** [April 27, 2022, 11:26am UTC](https://forum.opencv.org/t/how-to-create-positive-and-negative-eigenfaces-using-mean-image/8561 "2022-04-27T11:26:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Real\_prime](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/real_prime/32/5094_2.png) [@Real\_prime](https://forum.opencv.org/u/Real_prime)\
**Post date:** [April 27, 2022, 11:26am UTC](https://forum.opencv.org/t/how-to-create-positive-and-negative-eigenfaces-using-mean-image/8561/1 "2022-04-27T11:26:45Z")

</div>

I’m trying to generate Eigenvectors of positive and negative Eigenfaces and here is the following code:

```auto
model = cv2.face.EigenFaceRecognizer_create()
model.train(np.asarray(faces), np.asarray(labels))

# Save model results
model.save(test_config.TRAINING_FILE)
mean = model.getMean().reshape(faces[0].shape)
cv2.imwrite(MEAN_FILE, normalize(mean, 0, 255, dtype=np.uint8))
eigenvectors = model.getEigenVectors()
pos_eigenvector = eigenvectors[:,0].reshape(faces[0].shape)
cv2.imwrite(POSITIVE_EIGENFACE_FILE, normalize(pos_eigenvector, 0, 255, dtype=np.uint8))
neg_eigenvector = eigenvectors[:,1].reshape(faces[0].shape)
cv2.imwrite(NEGATIVE_EIGENFACE_FILE, normalize(neg_eigenvector, 0, 255, dtype=np.uint8))

```

I’m getting an error from this block:

```auto
neg_eigenvector = eigenvectors[:,1].reshape(faces[0].shape)
IndexError: index 1 is out of bounds for axis 1 with size 1

```

After Inspecting and Debugging the block I found out that, the line:

```auto
eigenvectors = model.getEigenVectors()

```

the above line is producing a black image and its vectors

```auto
[[0.] [0.] [0.] ..., [0.] [0.] [0.]]

```

I don’t know what happened, the eigenvectors are not generated in the first place. So, is there any fix or any other alternative ways for generating Positive and Negative Eigefaces from mean Image?

Here is the [Full Code](https://github.com/realprime/attendance-system/blob/main/test_train.py).

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 27, 2022, 11:54am UTC](https://forum.opencv.org/t/how-to-create-positive-and-negative-eigenfaces-using-mean-image/8561/2 "2022-04-27T11:54:14Z")

</div>

what makes you think, there are NEGATIVE eigenvectors ?  
(sounds like a misconception to me)

also, what is `eigenvectors.shape` ?  
(error complains,it has only a single column)

and how many images / persons are in your train data ?

---

<div class="post-metadata">

**Author:** ![Real\_prime](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/real_prime/32/5094_2.png) [@Real\_prime](https://forum.opencv.org/u/Real_prime)\
**Post date:** [April 27, 2022, 12:01pm UTC](https://forum.opencv.org/t/how-to-create-positive-and-negative-eigenfaces-using-mean-image/8561/3 "2022-04-27T12:01:41Z")

</div>

now the main problem is, I can’t get eigenvectors.  
I’m getting output as

```auto
[[0.] [0.] [0.] ..., [0.] [0.] [0.]]

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 27, 2022, 12:28pm UTC](https://forum.opencv.org/t/how-to-create-positive-and-negative-eigenfaces-using-mean-image/8561/4 "2022-04-27T12:28:37Z")

</div>

could you please anwer my questions ?
