Search code examples
pythonopencvorb

Error when "orb.detectAndCompute" when using opencv (python)


I am trying to use ORB to find the keypoints and descriptors of previously generated spectrograms. I have this code:

start = time.time()
x = 1
img = range(101)
imgname = range(101)

# Directory Images
os.chdir("/home/undead/Documents/TempSongSpectro/") #1,2,3
for file in glob.glob("*.png"):
    img[x] = cv2.imread(file, 0)  # trainImage
    imgname[x] = os.path.splitext(file)[0]
# print "%s: %d " % (os.path.splitext(file)[0],(x))
x = x + 1

# Initiate ORB detector
orb = cv2.ORB_create(3000)

# find the keypoints and descriptors with ORB
a = 1
des = range(101)
kp = range(101)

for a in range(1, 101):
    kp[a], des[a] = orb.detectAndCompute(img[a], None)

end = time.time()
print("Initialize time: %f seconds" % (end - start))

However, I am getting the error:

OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cvtColor, file /home/undead/opencv/opencv-3.2.0/modules/imgproc/src/color.cpp, line 9710

Traceback (most recent call last):
  File "/home/undead/PycharmProjects/KavTest/Test3.py", line 37, in <module>
    kp[a], des[a] = orb.detectAndCompute(img[a], None)

cv2.error: /home/undead/opencv/opencv-3.2.0/modules/imgproc/src/color.cpp:9710: error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor

From what i gathered, the error may be related to the datatype of kp, des or img, but I'm not too sure on how to solve it. Can someone offer some assistance?


Solution

  • I think there is something wrong in your code.

    This is my code doing nearly the same.

    start = time.time()
    
    imgnames = glob.glob("/home/undead/Documents/TempSongSpectro/*.png")
    #imgnames = imgnames[:100]
    
    sz = len(imgnames)
    imgs = list(range(sz))
    
    for i, name in enumerate(imgnames, start=0):
        imgs[i] =  cv2.imread(name, 0)
    
    orb = cv2.ORB_create(3000)
    
    # find the keypoints and descriptors with ORB
    des = list(range(sz))
    kp = list(range(sz))
    
    for i in range(sz):
        kp[i], des[i] = orb.detectAndCompute(img[i], None)
    
    end = time.time()
    print("Initialize time: {:.4f} seconds".format(end - start))