A detailed example of image recognition using the Scipy package's SIFT method in Python

Source: Internet
Author: User
This article explains in detail how the SIFT algorithm can detect local features in images. the algorithm principle is quite complex...! The powerful third-party Python package Scipy contains the SIFT method for implementing the SIFT algorithm. we only need to use it. Scipy

The scipy package contains a Toolbox dedicated to common problems in scientific computing. Different submodules correspond to different applications. Such as interpolation, integration, optimization, image processing, and special functions.
Scipy can be compared with other standard scientific computing libraries, such as GSL (gnu c or C ++ scientific computing library) or Matlab toolbox. Scipy is the core package of the Scientific Computing Program in Python. it is used to calculate the numpy matrix effectively to allow 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 a non-professional programmer, scientists always like to re-invent the wheel, resulting in vulnerable, unoptimized code that is hard to share and maintain. On the contrary, the Scipy program has been optimized and tested, so it should be used as much as possible.
Scipy is composed of some sub-modules with specific functions. all of them depend on numpy, but each of them is basically independent.
Here is an example of Debian Linux installation (although I use -- on windows --):

The code is as follows:

Sudo apt-get install python-numpy python-scipy python-matplotlib ipython-notebook python-pandas python-sympy python-nose

The standard method for importing Numpy and these scipy modules is:

Import numpy as npfrom scipy import stats # Other sub-modules are the same

Most of the main scipy namespaces contain real numpy functions (try to use scipy. cos as np. cos ). These are only for historical reasons. Generally, there is no reason to use import scipy in your code.

Use image matching SIFT algorithm for LOGO detection
First:

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 'good',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 'kp1,kp2',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()

For more details about examples of image recognition using the Scipy package's SIFT method in Python, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.