14

I am trying to execute the code from this URL. However, I started getting this error:

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

I have not made any major changes though. But I will paste what I did:

import scipy as sp
import numpy as np
import cv2

# Load the images
img =cv2.imread("image1.png")

# Convert them to 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 up samples and responses for 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"]

for modelImage in modelImages:

    # Now loading a template image and searching for similar keypoints
    template = cv2.imread(modelImage)
    templateg= cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)
    keys = surf.detect(templateg)

    keys,desc = surfDescriptorExtractor.compute(templateg, keys)

    for 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 red color
            color = (0,0,255)

        else:  # draw unmatched in blue 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 this is greatly appreciated.

Community
  • 1
  • 1
Dinakar
  • 329
  • 1
  • 4
  • 19
  • 2
    What's the shape of your array before this line ? – polku Oct 10 '14 at 09:15
  • Can't you just do `.reshape(128)` ? Also ensure that `np.array(des,np.float32)` is 128 in total size. – a-Jays Oct 10 '14 at 09:38
  • a-Jays: I tried that, that didn't help. – Dinakar Oct 10 '14 at 10:00
  • polku: I am in learning process. So not sure when you ask about shape of array. I have the entire code pasted above, hope you can get information from that ? Sorry about that! – Dinakar Oct 10 '14 at 10:06
  • 2
    The error message seems to indicate that you try to reshape in a size 128 array something that is more or less than 128. To see the current shape you can use `print(des.shape)` before reshape. – polku Oct 10 '14 at 10:12
  • this print statement gave me below o/p: (64L,) – Dinakar Oct 10 '14 at 10:30
  • So I changed it to: des = np.array(des,np.float32).reshape(64) and I guess this is working, although I got new error: retval, results, neigh_resp, dists = knn.find_nearest(des,1) error: ..\..\..\..\opencv\modules\ml\src\knearest.cpp:370: error: (-5) Input samples must be floating-point matrix (x) in function CvKNearest::find_nearest – Dinakar Oct 10 '14 at 10:31
  • I thought des was a numpy array so shape would have returned n,m (where n is the number of rows and m the number of columns). Well if it's works ok, but for the rest I have no clue, I don't know OpenCV. Maybe you should edit your question. – polku Oct 10 '14 at 10:57

1 Answers1

27

I had the same issue. I found that I changed the data length. A product of reshape arguments should be equal to a length of an array which you are changing. In your case:

des = np.array(des,np.float32).reshape(1, len(des))
ikoverdyaev
  • 783
  • 1
  • 12
  • 17