ValueError:新数组的总大小必须保持不变 [英] ValueError: total size of new array must be unchanged

查看:274
本文介绍了ValueError:新数组的总大小必须保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行此网址。但是,我开始收到此错误:

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()

非常感谢任何帮助。

推荐答案

我遇到了同样的问题。我发现我改变了数据长度。 reshape 参数的乘积应该等于您要更改的数组的长度。
在你的情况下:

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))

这篇关于ValueError:新数组的总大小必须保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆