3

The issue I'm having is that the two scripts below are both outputting this error: https://i.sstatic.net/jAjqm.png

TypeError: FeatureDetector.detect() takes at most 2 arguments (3 given)

which I can avoid in the script 2 below by deleting:

useProvidedKeypoints = False 

from the end of

kp, descritors = surf.detect(imgg,None,useProvidedKeypoints = False)

which leads to this error in the 2nd script :https://i.sstatic.net/CyMTF.png

TypeError: float() argument must be a string or a number

And this error in the first script: i.imgur.com/UVzNvP1.png (2 link limit add manually)

TypeError: trainData data type = 17 is not supported

Any help would be greatly appreciated and the main thing I want to come out of this is with a script I can tweak and edit till I understand the functions involved slightly better.

Summary; I'm not really sure why kp, descritors = surf.detect(imgg,None,useProvidedKeypoints = False) is telling me there too many arguments because the person who helped me write this seemed to think this should work.

1

import cv2
import numpy as np

img =cv2.imread('win18.jpg')

imgg =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

surf = cv2.SURF()
kp, descritors = surf.detect(imgg,None,useProvidedKeypoints = False)


samples = np.array(descritors)
responses = np.arange(len(kp),dtype = np.float32)


knn = cv2.KNearest()
knn.train(samples,responses)


template = cv2.imread('win17.jpg')
templateg= cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)
keys,desc = surf.detect(templateg,None,useProvidedKeypoints = False)


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: 
        color = (0,0,255)
    else: 
        print dist
        color = (255,0,0)


    x,y = kp[res].pt
    center = (int(x),int(y))
    cv2.circle(img,center,2,color,-1)

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

2

import cv2
import numpy

opencv_haystack =cv2.imread('win12.jpg')
opencv_needle =cv2.imread('win1.jpg')

ngrey = cv2.cvtColor(opencv_needle, cv2.COLOR_BGR2GRAY)
hgrey = cv2.cvtColor(opencv_haystack, cv2.COLOR_BGR2GRAY)


hessian_threshold = 85
detector = cv2.SURF(hessian_threshold)
(hkeypoints, hdescriptors) = detector.detect(hgrey, None, useProvidedKeypoints = False)
(nkeypoints, ndescriptors) = detector.detect(ngrey, None, useProvidedKeypoints = False)


rowsize = len(hdescriptors) / len(hkeypoints)
if rowsize > 1:
    hrows = numpy.array(hdescriptors, dtype = numpy.float32).reshape((-1, rowsize))
    nrows = numpy.array(ndescriptors, dtype = numpy.float32).reshape((-1, rowsize))

else:
    hrows = numpy.array(hdescriptors, dtype = numpy.float32)
    nrows = numpy.array(ndescriptors, dtype = numpy.float32)
    rowsize = len(hrows[0])


samples = hrows
responses = numpy.arange(len(hkeypoints), dtype = numpy.float32)

knn = cv2.KNearest()
knn.train(samples,responses)



    if dist < 0.1:

        color = (0, 0, 255)
    else:

        color = (255, 0, 0)

    x,y = hkeypoints[res].pt
    center = (int(x),int(y))
    cv2.circle(opencv_haystack,center,2,color,-1)

    x,y = nkeypoints[i].pt
    center = (int(x),int(y))
    cv2.circle(opencv_needle,center,2,color,-1)

cv2.imshow('haystack',opencv_haystack)
cv2.imshow('needle',opencv_needle)
cv2.waitKey(0)
cv2.destroyAllWindows()
5
  • Welcome to SO! You may learn something by making this into an SSCCE - make the script as short as possible, then start adding things back until you figure out why the error appears. Commented May 15, 2014 at 12:07
  • Also, in the future, you generally don't need to include screenshots of your IDE with the error showing - simply providing the code and the error message is usually sufficient. It might also be helpful to post the full traceback that's produced when the error occurs Commented May 15, 2014 at 12:10
  • Hey thank's for the advice, I've been trying to turn this into a SSCCE but I'm having trouble dividing the functions and such. My main goal is to detect if a picture is on screen and click it, i know how to setup clicks and such but I'm having a hard time creating my SSCCE for image detection. Could you help by shortening this script ? Commented May 15, 2014 at 12:17
  • Also what do you think I should be googling to find the documentation for these modules in particular. (OpenCv, numpy, Pillow) Commented May 15, 2014 at 12:26
  • Unfortunately, SO is not a debugging service, and I'm just trying to help in bits of time off during my day, so I don't have time to shorten the script for you. Anyways, the best way to learn is to do it yourself. Usually, googling the error message is a good start, and you should also read the specific documentation for the methods you're using. Commented May 15, 2014 at 12:28

2 Answers 2

1

Hi I know it's late but for the ones still facing the problem, try replacing detect() with detectAndCompute().
I got the error removed this way.

Sign up to request clarification or add additional context in comments.

Comments

0

when in doubt, ...

>>> s = cv2.SURF()
>>> help(s.detect)
Help on built-in function detect:

detect(...)
    detect(image[, mask]) -> keypoints

so, your assumptions about the args to SURF.detect() were quite off.

4 Comments

So there should be 3 arg then? or is there actually an extra one that I should remove?
one of us 3 can't count
sorry for pulling your leg, but it takes either an image only , or an image and a mask, and returns keypoints.
Haha good thing i came back late :P, Ok well this will help alot. Right now I'm taking darthbith's advice and trying to create a SSCCE by breaking down the script into functions and looking them up individually! Ill get it, thanks again!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.