'Plot opencv image using PCA

I take images from the webcam and try to use PCA to convert them into an (x, y) coordinate format, one coordinate per image. I use matplotlib to plot the data. I'm new to PCA and NumPy arrays. How can I get the intended result of coordinates instead of lines?

Here's my attempt.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import cv2

plt.ion()
cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    nsamples, nx, ny = frame.shape
    print(nsamples)
    frame = frame.reshape((nsamples, nx * ny))
    pca = PCA(2)
    pca_vecs = pca.fit_transform(frame)
    x = pca_vecs[:, 0]
    y = pca_vecs[:, 1]
    plt.plot(x, y)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

and here's the result for one image. enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source