Home > Backend Development > Python Tutorial > Detailed introduction to the example of image recognition using the SIFT method of the Scipy package in Python

Detailed introduction to the example of image recognition using the SIFT method of the Scipy package in Python

高洛峰
Release: 2018-05-16 15:53:58
Original
5334 people have browsed it

scipy

The scipy package contains various toolboxes dedicated to common problems in scientific computing. Its different sub-modules correspond to different applications. Like interpolation, integration, optimization, image processing, special functions and so on.
scipy can be compared with other standard scientific computing libraries, such as GSL (GNU C or C++ Scientific Computing Library), or the Matlab toolbox. scipy is the core package for scientific computing programs in Python; it is used to efficiently calculate numpy matrices, allowing numpy and scipy to work together.
Before implementing a program, it is worth checking whether the required data processing method already exists in scipy. As non-professional programmers, scientists always like to reinvent the wheel, resulting in code that is full of bugs, unoptimized, and difficult to share and maintain. In contrast, Scipy programs are optimized and tested and should therefore be used whenever possible.
Scipy is composed of some sub-modules with specific functions. They all depend on numpy, but each one is basically independent.
Give an example of installation in Debian Linux (although I use -- on windows):

Copy code The code is as follows:

sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas 
python-sympy python-nose
Copy after login

The standard way of importing Numpy and these scipy modules is:

import numpy as np
from scipy import stats # 其它子模块相同
Copy after login

The main scipy namespace mostly contains real numpy functions (try scipy.cos which is np.cos). These are just for historical reasons, there is generally no reason to use import scipy in your code.

Use image matching SIFT algorithm for LOGO detection
First the rendering:

Detailed introduction to the example of image recognition using the SIFT method of the Scipy package in Python

Detailed introduction to the example of image recognition using the SIFT method of the Scipy package in Python It is the logo,

Detailed introduction to the example of image recognition using the SIFT method of the Scipy package in Python

The code is as follows.

#coding=utf-8 
import cv2 
import scipy as sp 
 
img1 = cv2.imread('x1.jpg',0) # queryImage 
img2 = cv2.imread('x2.jpg',0) # trainImage 
 
# Initiate SIFT detector 
sift = cv2.SIFT() 
 
# 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) 
 
print 'matches...',len(matches) 
# Apply ratio test 
good = [] 
for m,n in matches: 
  if m.distance < 0.75*n.distance: 
    good.append(m) 
print &#39;good&#39;,len(good) 
# ##################################### 
# visualization 
h1, w1 = img1.shape[:2] 
h2, w2 = img2.shape[:2] 
view = sp.zeros((max(h1, h2), w1 + w2, 3), sp.uint8) 
view[:h1, :w1, 0] = img1 
view[:h2, w1:, 0] = img2 
view[:, :, 1] = view[:, :, 0] 
view[:, :, 2] = view[:, :, 0] 
 
for m in good: 
  # draw the keypoints 
  # print m.queryIdx, m.trainIdx, m.distance 
  color = tuple([sp.random.randint(0, 255) for _ in xrange(3)]) 
  #print &#39;kp1,kp2&#39;,kp1,kp2 
  cv2.line(view, (int(kp1[m.queryIdx].pt[0]), int(kp1[m.queryIdx].pt[1])) , (int(kp2[m.trainIdx].pt[0] + w1), int(kp2[m.trainIdx].pt[1])), color) 
 
cv2.imshow("view", view) 
cv2.waitKey()
Copy after login

More details about the example of using the SIFT method of the Scipy package for image recognition in Python Please pay attention to the PHP Chinese website for articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template