user9744314
user9744314

Reputation: 97

Error:(-215) type == src2.type() && src1.cols == src2.cols && (type == 5 || type == 0) in function cv::batchDistance

I have the following code for image matching by using ORB:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread('C:\\IRRNEW\\models\\research\\object_detection\\Scanner\\IMG_Hello.jpg',0)          # queryImage
img2 = cv2.imread('C:\\IRRNEW\\models\\research\\object_detection\\Image.jpg',0) # trainImage
#orb = cv2.ORB_create()
orb = cv2.ORB_create(nfeatures=10000, scoreType=cv2.ORB_FAST_SCORE)
# find the keypoints with ORB
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
matches = sorted(matches, key = lambda x:x.distance)
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[::], None,flags=2)
a=len(matches)
print(a)
b=len(des)
print(b)
plt.imshow(img3),plt.show()

Previosly this code able to run and show me result, but now suddently give me this error:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\h.py", line 13, in <module>
matches = bf.match(des1,des2)
cv2.error: C:\projects\opencv-python\opencv\modules\core\src\stat.cpp:4022: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == 5 || type == 0) in function cv::batchDistance

Version of opencv: opencv-python 3.4.0.12

Please help, I refer solution from online but nothing help, hope you guys can share me ideas. Thanks

Upvotes: 3

Views: 12038

Answers (4)

vighnesh
vighnesh

Reputation: 71

I had the same error too!

error: (-215:Assertion failed) type == src2.type() && src1.cols == src2.cols && (type == 5 || type == 0) in function 'cv::batchDistance'

After some print debugging, I found out that one of the descriptors was actually None. So make sure both the descriptors are non None before passing them to the matcher.

Upvotes: 7

Ehsun Assadi
Ehsun Assadi

Reputation: 21

You first need to check if descriptors for both test and train images are not empty by the following statement:

if type(des1)!=NoneType and type(des2)!=NoneType:

In addition, you need to import types for checking NoneType.

For example:

import types
import numpy as np
import cv2
from matplotlib import pyplot as plt

NoneType=type(None)
img1 = cv2.imread('C:\\IRRNEW\\models\\research\\object_detection\\Scanner\\IMG_Hello.jpg',0)          # queryImage
img2 = cv2.imread('C:\\IRRNEW\\models\\research\\object_detection\\Image.jpg',0) # trainImage
#orb = cv2.ORB_create()
orb = cv2.ORB_create(nfeatures=10000, scoreType=cv2.ORB_FAST_SCORE)
# find the keypoints with ORB
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)

if type(des1)!=NoneType and type(des2)!=NoneType:
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    matches = bf.match(des1,des2)
    matches = sorted(matches, key = lambda x:x.distance)
    img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[::], None,flags=2)
    a=len(matches)
    print(a)
    b=len(des)
    print(b)
    plt.imshow(img3),plt.show()
else:
    print("There are no descriptors to be matched.")

Upvotes: 1

GOCHI
GOCHI

Reputation: 1

You have make sure, file image was read plt.imshow(img),plt.show() . Then print(des1, des2). I think you may have one of des1 or des2 has []. There are not any image distance (hamming distance cannot find out). it may original image file was very smooth.

Upvotes: 0

oopsi
oopsi

Reputation: 2019

This does not answer the original question but I got the same error so this might help someone else:

Make sure that descriptor1.shape[1] == descriptor2.shape[1]. If you're not using the cv library to compute the descriptors make sure that your descriptors first dimension is the number of points and the second dimension is the num of features dimension and not the other way around.

Upvotes: 2

Related Questions