我无法看到我出错的地方。我有python代码使用OpenCV2来计算2个图像的特征,然后匹配并绘制最相似的点。如果我运行单独的代码,它可以工作,我可以更改第二个图像文件的名称,当我尝试自动化它循环通过图像目录与第一个图像进行比较我在Ubuntu 14.04,OpenCV 3.10上得到以下错误
除了我的代码粗糙之外,任何人都会看到为什么循环错误,但个别比较没有?
opencv / modules / core / src / stat.cpp:3749:错误:(-215)type == src2.type()&& src1.cols == src2.cols&& (函数batchDistance
中的(type == CV_32F || type == CV_8U)个人代码
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')
循环代码
下面是遍历目录的代码,将图像加载到OpenCV中,计算图像的关键点和特征,并在获得匹配数量和绘制前40个匹配项之前将其与搜索图像进行比较
#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(greyCrest,None)
#Get the folder of Images to be matched with ORB 1 by 1 against the Search Image
for ImageToProcess in os.listdir('/MatchWithORB'):
print "Processing Image", ImageToProcess
#Load the current Image as greyscale and calculate the keypoints, descriptors
CurrentImage = cv2.imread(ImageToProcess,cv2.IMREAD_GRAYSCALE)
currImageKeyPoints, currImageDescriptors = orb2.detectAndCompute(CurrentImage,None)
#Get the matches between the current image and the crest
matches = BruteForceMatch.match(Crestdescriptor,currImageDescriptors)
Title = "Crest Total Matched " + str(len(matches)) +" Plotting Top 40 " +ImageToProcess
# Sort them so the top matches are higher
matches = sorted(matches, key = lambda x:x.distance)
MatchImage = cv2.drawMatches(greyCrest,Crestkeypoints,CurrentImage,currImageKeyPoints,matches[:40],CurrentImage,flags=2)
plt.imshow(MatchImage)
plt.draw()
plt.pause(10)
#tidyup
BruteForceMatch.clear
print"Looping"
答案 0 :(得分:0)
一个古老的问题,但是答案是,如果未检测到任何功能,detectAndCompute将返回“ None”而不是一个空列表。然后,随着代码继续匹配,它将尝试将功能列表与None类型匹配,从而为您提供所描述的错误。 简便的检查方法:
if des1 is None or des2 is None:
print(des1)
print(des2)
pdb.set_trace()
他们可能会是无