OpenCV2 BatchDistance Error -215 when looping through images while individual comparisons work correctly

asked 2016-06-10 04:00:53 -0600

mobmsc gravatar image

I had asked this question on stackoverflow but now think this is a better place for it

I have python code to use OpenCV2 (3.10) to calculate the features of 2 images, match then and plot the most similar points. If I run the individual code it works and I can manually change the 2nd image file and the code will plot the matches, My problem is when I try to automate it to loop through a directory of images to compare to the 1st image I get the following error on Ubuntu 14.04, OpenCV 3.10 and Python 2.7

Besides the roughness of my code, anyone see why the loop errors out but the individual comparisons don't?

opencv/modules/core/src/stat.cpp:3749: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function batchDistance

Individual Code Will match without triggering the error for every test image I supply by manually editing the 2nd image filepath value.

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

#Load the crest and create a grey version
crest = cv2.imread('Crest.png')
greyCrest = cv2.cvtColor(crest,cv2.COLOR_BGR2GRAY)

#Create the ORB Object and BruteForce
orb=cv2.ORB_create()
#Use Hamming Distance as its ORD (other algorithms would use a different distance measure)
BruteForceMatch = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck=True)
#Calculate the keypoints and descriptors for the crest
Crestkeypoints,Crestdescriptor = orb.detectAndCompute(greyCrest,None)

#
testimage = cv2.imread('OtherImage.jpg')
testkeypoints, testdescriptors = orb.detectAndCompute(testimage,None)

#Basic Idea
matches = BruteForceMatch.match(Crestdescriptor,testdescriptors)
#Sort the matches so the strongest are at the top
matches = sorted(matches, key = lambda x:x.distance)
Title = "Total Matched " + str(len(matches)) +" Plotting Top 40"
#display the top 40 matches on the plot
MatchImage = cv2.drawMatches(greyCrest,Crestkeypoints,testimage,testkeypoints,matches[:40],testimage,flags=2)
#This gets rid of the blue tint to the end image when plotting OpenCV image in Matplotlib because of the RGB ,BGR difference
plt.imshow(cv2.cvtColor(MatchImage, cv2.COLOR_BGR2RGB))
plt.title(Title)
plt.show()
#Pause for 10 Seconds so you can see the plot
plt.pause(10)
#Have to close as for some reason the figures weren't plotting correctly when I ran the code the 2nd time
plt.close('all')

Looping Code

Below is code to loop through a directory, load the image into OpenCV, calculate the keypoints and features of the image and compare it to a search image before getting the number of matches and plotting the top 40 matches

#Load the crest and create a grey version
import cv2
import os
import numpy as np
from matplotlib import pyplot as plt
    crest = cv2.imread('Crest.png')
    greyCrest = cv2.cvtColor(crest,cv2.COLOR_BGR2GRAY)

    #Create the ORB Object and BruteForce
    orb2=cv2.ORB_create()
    #Use Hamming Distance as its ORD (other algorithms would use a different distance measure)
    BruteForceMatch = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck=True)
    #Calculate the keypoints and descriptors for the crest
    Crestkeypoints,Crestdescriptor = orb2.detectAndCompute ...
(more)
edit retag flag offensive close merge delete