This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

script for SO question

By: takumotanji on Jan 10th, 2012  |  syntax: Python  |  size: 4.89 KB  |  views: 63  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. # OpenCV 2.x's C demo
  3. # -- adapted further by Attila, Aug 2010
  4. # -- adapted by Daniel to work with PyOpenCV
  5. # -- modified a bit Minh-Tri Pham
  6. # -- horribly broken by myself 1/10/2012
  7. import sys
  8. import pyopencv as cv
  9. from pyopencv import *
  10. import time
  11.  
  12.  
  13. # flann-based search
  14. def findPairs(surf, objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors):
  15.     ptpairs = []
  16.  
  17.     N = len(objectKeypoints)
  18.     K = 2 # K as in K nearest neighbors
  19.     dim = surf.descriptorSize()
  20.  
  21.     m_object = cv.asMat(objectDescriptors).reshape(1, N)
  22.     m_image = cv.asMat(imageDescriptors).reshape(1, len(imageKeypoints))
  23.  
  24.     flann = cv.Index(m_image,cv.KDTreeIndexParams(4))
  25.  
  26.     indices = cv.Mat(N, K, cv.CV_32S)
  27.     dists = cv.Mat(N, K, cv.CV_32F)
  28.  
  29.     flann.knnSearch(m_object, indices, dists, K, cv.SearchParams(250))
  30.    
  31.     indices = indices[:,0].ravel()
  32.     dists = dists.ndarray
  33.    
  34.     for i in xrange(N):
  35.         if dists[i,0] < 0.6*dists[i,1]:
  36.             ptpairs.append((objectKeypoints[i].pt, imageKeypoints[int(indices[i])].pt))
  37.    
  38.     return ptpairs
  39.  
  40.  
  41. # a rough implementation for planar object location
  42. def locatePlanarObject(ptpairs, src_corners, dst_corners):
  43.     import numpy as _n
  44.  
  45.     n = len(ptpairs)
  46.     if n < 4:
  47.         return 0
  48.     pt1 = cv.asMat(_n.array([cv.asndarray(pair[0]) for pair in ptpairs]))
  49.     pt2 = cv.asMat(_n.array([cv.asndarray(pair[1]) for pair in ptpairs]))
  50.     h = cv.findHomography(pt1, pt2, method=cv.RANSAC, ransacReprojThreshold=5)[:]
  51.  
  52.     for i in range(4):
  53.         x = src_corners[i].x
  54.         y = src_corners[i].y
  55.         Z = 1./(h[2,0]*x + h[2,1]*y + h[2,2])
  56.         X = (h[0,0]*x + h[0,1]*y + h[0,2])*Z
  57.         Y = (h[1,0]*x + h[1,1]*y + h[1,2])*Z
  58.         dst_corners[i] = cv.Point(int(X), int(Y))
  59.  
  60.     return 1
  61.  
  62.  
  63. object_filename = "box.png"
  64. #        scene_filename = "box_in_scene.png"
  65.  
  66.  
  67.  
  68. colors = [
  69.     cv.Scalar(0,0,255),
  70.     cv.Scalar(0,128,255),
  71.     cv.Scalar(0,255,255),
  72.     cv.Scalar(0,255,0),
  73.     cv.Scalar(255,128,0),
  74.     cv.Scalar(255,255,0),
  75.     cv.Scalar(255,0,0),
  76.     cv.Scalar(255,0,255),
  77.     cv.Scalar(255,255,255),
  78. ]
  79.  
  80. # read the two images
  81. object_color = cv.imread( object_filename, cv.CV_LOAD_IMAGE_COLOR )
  82.  
  83.    
  84. capture = cv.VideoCapture(0)
  85. myloop = 1
  86.  
  87. while myloop == 1 :
  88.     cv.namedWindow("Object", 1)
  89.     cv.namedWindow("Object Correspond", 1)
  90.     frame = Mat()
  91.     capture >> frame
  92.     imwrite("last_scan.png",frame)
  93.     time.sleep(.30)
  94.  
  95.     image = cv.imread("last_scan.png" , cv.CV_LOAD_IMAGE_GRAYSCALE )
  96.  
  97.     if not object_color or not image:
  98.         print("Can not load %s and/or %s\n" \
  99.             "Usage: find_obj [<object_filename> <scene_filename>]\n" \
  100.             % (object_filename, scene_filename))
  101.         exit(-1)
  102.     object = cv.Mat(object_color.size(), cv.CV_8UC1)
  103.     cv.cvtColor( object_color, object, cv.CV_BGR2GRAY )
  104.  
  105.     # corners
  106.     src_corners = [cv.Point(0,0), cv.Point(object.cols, 0), cv.Point(object.cols, object.rows), cv.Point(0, object.rows)]
  107.     dst_corners = [cv.Point()]*4
  108.  
  109.     # find keypoints on both images
  110.     surf = cv.SURF(500, 4, 2, True)
  111.     mask = cv.Mat()
  112.     tt = float(cv.getTickCount())    
  113.     objectKeypoints = cv.vector_KeyPoint()
  114.     objectDescriptors = surf(object, mask, objectKeypoints)
  115.     print("Object Descriptors: %d\n" % len(objectKeypoints))
  116.     imageKeypoints = cv.vector_KeyPoint()
  117.     imageDescriptors = surf(image, mask, imageKeypoints)
  118.     print("Image Descriptors: %d\n" % len(imageKeypoints))
  119.     tt = float(cv.getTickCount()) - tt
  120.     print("Extraction time = %gms\n" % (tt/(cv.getTickFrequency()*1000.)))
  121.  
  122.     # create a correspond Mat
  123.     correspond = cv.Mat(image.rows+object.rows, image.cols, cv.CV_8UC1, cv.Scalar(0))
  124.  
  125.     # copy the images to correspond -- numpy way
  126.     correspond[:object.rows, :object.cols] = object[:]
  127.     correspond[object.rows:, :image.cols] = image[:]
  128.  
  129.     # find pairs
  130.     ptpairs = findPairs(surf, objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors)
  131.  
  132.     for pair in ptpairs:
  133.         cv.line( correspond, cv.asPoint(pair[0]), cv.Point(int(pair[1].x), int(pair[1].y+object.rows)), colors[8] )
  134.  
  135.     # locate planar object
  136.     if locatePlanarObject( ptpairs, src_corners, dst_corners ):
  137.         for i in range(4):
  138.             r1 = dst_corners[i]
  139.             r2 = dst_corners[(i+1)%4]
  140.             cv.line( correspond, cv.Point(r1.x, r1.y+object.rows ), cv.Point(r2.x, r2.y+object.rows ), colors[8] )
  141.             print r1
  142.             print r2
  143.  
  144.     # show the object correspondents
  145.     cv.imshow("Object Correspond", correspond)
  146.  
  147.     # draw circles
  148.     for keypt in objectKeypoints:
  149.         cv.circle(object_color, cv.asPoint(keypt.pt), int(keypt.size*1.2/9.*2), colors[0], 1, 8, 0)
  150.     cv.imshow("Object", object_color)
  151.     time.sleep(1)
  152.     cv.destroyWindow("Object Correspond")
  153.     cv.destroyWindow("Object")  
  154.        
  155.     cv.waitKey(0)
clone this paste RAW Paste Data