Ask Your Question

Revision history [back]

mat type = 15 is not supported

I got an error while changing an eigenvector as an image

def getEigenVectorsAndEigenValues(covarianceMatx): print("Compute Eigens...") logger.info("Compute Eigens...") eigenSolution = np.linalg.eig(covarianceMatx) # print("len eigenSolution", len(eigenSolution)) # print("eigenSolution", eigenSolution) eigenValues = eigenSolution[0] # print("eigenValues", len(eigenValues)) eigenVectors = eigenSolution[1] # print("eigenVectors", type(eigenVectors)) # print("eigenVectors-1", type(eigenVectors[0])) # resized = cv2.resize(eigenVectors[2],(512,512)) # cv2.imshow("eigen",resized) # cv2.waitKey() sort = eigenValues.argsort()[::-1] eigenValues = eigenValues[sort] eigenVectors = eigenVectors[:, sort] # Each eigenvector has the same dimensionality(number # of components) as the original images, and thus # can itself be seen as an image logger.info("shape eigenValues" + str(np.asarray(eigenValues).shape)) logger.info("shape eigenVectors" + str(np.asarray(eigenVectors).shape)) logger.info("shape eigenVectors[0]" + str(np.asarray(eigenVectors[0]).shape)) logger.info("shape eigenVectors[0][0]" + str(np.asarray(eigenVectors[0][0]).shape)) logger.info("value in eigenVectors" + str(eigenValues))

# for i in range(0, 4095):
for i in range(0, 361):
    imageFromVector = np.asarray(eigenVectors[i])
    imageFromVector = imageFromVector.reshape((19, 19))
    imageFromVector2 = np.array(np.dstack([imageFromVector.real,imageFromVector.imag])) # What I added
    print("imageFromVector shape", imageFromVector.shape)
    print("imageFromVector type", type(imageFromVector))
    cv2.imshow("imageFromVector", imageFromVector2) # Error in here!

    cv2.waitKey()
    # imageFromVector.reshape((64, 64))
    file = str(i) + ".jpg"
    cv2.imwrite(filename=file, img=imageFromVector)

# resized = cv2.resize(eigenVectors[0], (512, 512))
# cv2.imshow("eigen", resized)
# cv2.waitKey()

logger.info("Compute Eigens... Done")
return eigenVectors, eigenValues

before I added imageFromVector2 = np.array(np.dstack([imageFromVector.real,imageFromVector.imag])), show an error like this image description

after I added that code, it shown

File "E:/PROJECT_FOLDER/PYTHON/PKLBPPT/PCA-with-VJ.py", line 169, in getEigenVectorsAndEigenValues cv2.imshow("imageFromVector", imageFromVector2) cv2.error: D:\Build\OpenCV\opencv-3.2.0\modules\imgcodecs\src\utils.cpp:611: error: (-15) Source image must have 1, 3 or 4 channels in function cvConvertImage

Anyone knows what's wrong with this code?

click to hide/show revision 2
No.2 Revision

updated 2017-07-28 05:12:25 -0600

berak gravatar image

mat type = 15 is not supported

I got an error while changing an eigenvector as an image

def getEigenVectorsAndEigenValues(covarianceMatx):
    print("Compute Eigens...")
    logger.info("Compute Eigens...")
    eigenSolution = np.linalg.eig(covarianceMatx)
    # print("len eigenSolution", len(eigenSolution))
    # print("eigenSolution", eigenSolution)
    eigenValues = eigenSolution[0]
    # print("eigenValues", len(eigenValues))
    eigenVectors = eigenSolution[1]
    # print("eigenVectors", type(eigenVectors))
    # print("eigenVectors-1", type(eigenVectors[0]))
    # resized = cv2.resize(eigenVectors[2],(512,512))
    # cv2.imshow("eigen",resized)
    # cv2.waitKey()
    sort = eigenValues.argsort()[::-1]
    eigenValues = eigenValues[sort]
    eigenVectors = eigenVectors[:, sort]
    # Each eigenvector has the same dimensionality(number
    # of components) as the original images, and thus
    # can itself be seen as an image
    logger.info("shape eigenValues" + str(np.asarray(eigenValues).shape))
    logger.info("shape eigenVectors" + str(np.asarray(eigenVectors).shape))
    logger.info("shape eigenVectors[0]" + str(np.asarray(eigenVectors[0]).shape))
    logger.info("shape eigenVectors[0][0]" + str(np.asarray(eigenVectors[0][0]).shape))
    logger.info("value in eigenVectors" + str(eigenValues))

str(eigenValues))

    # for i in range(0, 4095):
 for i in range(0, 361):
     imageFromVector = np.asarray(eigenVectors[i])
     imageFromVector = imageFromVector.reshape((19, 19))
     imageFromVector2 = np.array(np.dstack([imageFromVector.real,imageFromVector.imag])) # What I added
     print("imageFromVector shape", imageFromVector.shape)
     print("imageFromVector type", type(imageFromVector))
     cv2.imshow("imageFromVector", imageFromVector2) # Error in here!

     cv2.waitKey()
     # imageFromVector.reshape((64, 64))
     file = str(i) + ".jpg"
     cv2.imwrite(filename=file, img=imageFromVector)

 # resized = cv2.resize(eigenVectors[0], (512, 512))
 # cv2.imshow("eigen", resized)
 # cv2.waitKey()

 logger.info("Compute Eigens... Done")
 return eigenVectors, eigenValues

before I added imageFromVector2 = np.array(np.dstack([imageFromVector.real,imageFromVector.imag])), show an error like this image description

after I added that code, it shown

File "E:/PROJECT_FOLDER/PYTHON/PKLBPPT/PCA-with-VJ.py", line 169, in getEigenVectorsAndEigenValues
    cv2.imshow("imageFromVector", imageFromVector2)
cv2.error: D:\Build\OpenCV\opencv-3.2.0\modules\imgcodecs\src\utils.cpp:611: error: (-15) Source image must have 1, 3 or 4 channels in function cvConvertImage

cvConvertImage

Anyone knows what's wrong with this code?