I'm using this code:
import numpy as np
import cv2
from matplotlib import pyplot as plt
# file
filename1 = 'img1.png'
filename2 = 'img2.jpg'
img1 = cv2.imread(filename1,0)
img2 = cv2.imread(filename2,0)
# resize
height1, width1 = img1.shape
if height1 > 2000 or width1 > 2000:
img1 = cv2.resize(img1, None, fx=0.25, fy=0.25)
height1, width1 = img1.shape
height2, width2 = img2.shape
if height2 > 2000 or width2 > 2000:
img2 = cv2.resize(img2, None, fx=0.25, fy=0.25)
height2, width2 = img2.shape
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in xrange(len(matches))]
# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
if m.distance < 0.7*n.distance:
matchesMask[i]=[1,0]
draw_params = dict(matchColor = (0,255,0),
singlePointColor = (255,0,0),
matchesMask = matchesMask,
flags = 0)
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)
plt.imshow(img3,),plt.show()
and I'm having this error using flann.knnmatch:
OpenCV Error: Assertion failed (The data should normally be NULL!) in allocate, file /Users/jenkins/miniconda/1/x64/conda-bld/conda_1486587097465/work/opencv-3.1.0/modules/python/src2/cv2.cpp, line 163
Traceback (most recent call last):
File "FLANN_MATCHING.py", line 36, in <module>
matches = flann.knnMatch(des1,des2,k=2)
cv2.error: /Users/jenkins/miniconda/1/x64/conda-bld/conda_1486587097465/work/opencv-3.1.0/modules/python/src2/cv2.cpp:163: error: (-215) The data should normally be NULL! in function allocate
For what I've found out, there is not a solution for version 3.1.0, or at least I don't know how to apply the "patch" that was made here:
https://github.com/opencv/opencv/issues/5667
I installed opencv using this code:
conda install anaconda
conda install python=3.5
conda install -c menpo opencv3
is there a way to choose the 3.0.0 version in order to go through this error instead of 3.1.0?
I just wanted to learn a little bit from opencv, but I spend way more time trying to get it installed properly than coding. I've been following this tutorials but now I'm stuck trying to get FLANN to work: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html
Well finally I got to solve the problem not by using the 3.0.0 version but the 3.2.0 which can't be installed from conda.
1º I uninstalled conda and the python3 from conda completely.
https://stackoverflow.com/questions/22585235/python-anaconda-how-to-safely-uninstall
2º I installed python3 and opencv 3.2.0 using brew following this tutorial:
http://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/
Where some errors that now occur are taken into account:
http://www.pyimagesearch.com/2017/05/15/resolving-macos-opencv-homebrew-install-errors/
3º Install matplotlib.
pip3 install matplotlib
And now that code is properly working with:
python3 --version
Python 3.6.2
python3
Python 3.6.2 (default, Jul 17 2017, 16:44:32)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'