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.

8
  • 2
    What's the shape of your array before this line ?
    – polku
    Oct 10, 2014 at 9: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, 2014 at 9:38
  • a-Jays: I tried that, that didn't help.
    – Dinakar
    Oct 10, 2014 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, 2014 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, 2014 at 10:12

1 Answer 1

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))
2
  • dimensions of my array are (100,99) and I need to make it (100,100). How can this be done?
    – Aditya C
    Jan 27, 2017 at 17:03
  • You could use padding, reshape won't work as the product needs to be the same Jun 11, 2022 at 18:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.