Monday 15 April 2013

python - ValueError: total size of new array must be unchanged -



python - ValueError: total size of new array must be unchanged -

i trying execute code url. however, started getting error:

des = np.array(des,np.float32).reshape((1,128)) valueerror: total size of new array must unchanged

i have not made major changes though. paste did:

import scipy sp import numpy np import cv2 # load images img =cv2.imread("image1.png") # convert them grayscale imgg =cv2.cvtcolor(img,cv2.color_bgr2gray) # surf extraction surf = cv2.featuredetector_create("surf") surfdescriptorextractor = cv2.descriptorextractor_create("surf") kp = surf.detect(imgg) kp, descritors = surfdescriptorextractor.compute(imgg,kp) # setting samples , responses knn samples = np.array(descritors) responses = np.arange(len(kp),dtype = np.float32) # knn training knn = cv2.knearest() knn.train(samples,responses) modelimages = ["image2.png"] modelimage in modelimages: # loading template image , searching similar keypoints template = cv2.imread(modelimage) templateg= cv2.cvtcolor(template,cv2.color_bgr2gray) keys = surf.detect(templateg) keys,desc = surfdescriptorextractor.compute(templateg, keys) h,des in enumerate(desc): des = np.array(des,np.float32).reshape((1,128)) retval, results, neigh_resp, dists = knn.find_nearest(des,1) res,dist = int(results[0][0]),dists[0][0] if dist<0.1: # draw matched keypoints in reddish color color = (0,0,255) else: # draw unmatched in bluish color #print dist color = (255,0,0) #draw matched key points on original image x,y = kp[res].pt center = (int(x),int(y)) cv2.circle(img,center,2,color,-1) #draw matched key points on template image x,y = keys[h].pt center = (int(x),int(y)) cv2.circle(template,center,2,color,-1) cv2.imshow('img',img) cv2.imshow('tm',template) cv2.waitkey(0) cv2.destroyallwindows()

any help on appreciated.

i had same issue. found changed info length. product of reshape arguments should equal length of array changing. in case:

des = np.array(des,np.float32).reshape(1, len(des))

python opencv image-processing

No comments:

Post a Comment