Image data of dtype complex128 cannot be converted to float. how to remove this error? [closed]

asked 2020-07-18 11:11:10 -0600

updated 2020-07-18 11:40:57 -0600

berak gravatar image

.

from numpy import mean,cov,cumsum,dot,linalg,size,flipud
import numpy as np
import matplotlib.pyplot as plt
import numpy
#from ipymol.compat import Image

from PIL import Image
import cv2
def princomp(A,numpc=0):
 # computing eigenvalues and eigenvectors of covariance matrix
 M = (A-mean(A.T,axis=1)).T # subtract the mean (along columns)
 [latent,coeff] = linalg.eig(cov(M))
 p = size(coeff,axis=1)
 idx = np.argsort(latent) # sorting the eigenvalues
 idx = idx[::-1]       # in ascending order
 # sorting eigenvectors according to the sorted eigenvalues
 coeff = coeff[:,idx]
 latent = latent[idx] # sorting eigenvalues
 if numpc < p and numpc >= 0:
  coeff = coeff[:,range(numpc)] # cutting some PCs if needed
 score = dot(coeff.T,M) # projection of the data in the new space
 return coeff,score,latent
from pylab import imread,subplot,imshow,title,gray,figure,show,NullLocator

A = cv2.imread('C:\\Users\\dell\\OneDrive\\Desktop\\Image1.png') # load an image
A = mean(A,2) # to get a 2-D array
full_pc = size(A,axis=1) # numbers of all the principal components
i = 1
dist = []
for numpc in range(0,full_pc+10,10): # 0 10 20 ... full_pc
 coeff, score, latent = princomp(A,numpc)
 Ar = dot(coeff,score).T+mean(A,axis=0) # image reconstruction
 # difference in Frobenius norm
 dist.append(linalg.norm(A-Ar,'fro'))
 # showing the pics reconstructed with less than 50 PCs
 if numpc <= 50:
  ax = subplot(2,3,i,frame_on=False)
  ax.xaxis.set_major_locator(NullLocator()) # remove ticks
  ax.yaxis.set_major_locator(NullLocator())
  i += 1 
  #imshow(flipud(Ar))
  #plt.imshow(np.flipud(Ar))
  title('PCs # '+str(numpc))
  gray()

#figure()
#imshow(flipud(A))

gray()
show()
#plt.imshow(np.flipud(A))
title('numpc FULL')
plt.show()
from pylab import plot,axis
figure()
perc = cumsum(latent)/sum(latent)
dist = dist/max(dist)
plot(range(len(perc)),perc,'b',range(0,full_pc+10,10),dist,'r')
axis([0,full_pc,0,1.1])
show()
edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant by berak
close date 2020-07-18 11:41:33.164634

Comments

sorry, but this is the opencv help site, we cannot deal with arbitrary python problems

berak gravatar imageberak ( 2020-07-18 11:42:12 -0600 )edit

btw, downvoted for:

you want to remove the errror. what about understanding the problem ?

berak gravatar imageberak ( 2020-07-18 16:02:53 -0600 )edit