track_features_lk2.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import roslib
00004 roslib.load_manifest('image_algos')
00005 import rospy
00006 
00007 print "OpenCV Python version of lkdemo"
00008 
00009 import sys
00010 import urllib
00011 
00012 # import the necessary things for OpenCV
00013 import cv
00014 
00015 #############################################################################
00016 # some "constants"
00017 
00018 win_size = 10
00019 MAX_COUNT = 500
00020 
00021 #############################################################################
00022 # some "global" variables
00023 
00024 image = None
00025 pt = None
00026 add_remove_pt = False
00027 flags = 0
00028 night_mode = False
00029 need_to_init = False
00030 
00031 #############################################################################
00032 # the mouse callback
00033 
00034 # the callback on the trackbar
00035 def on_mouse (event, x, y, flags, param):
00036     
00037     # we will use the global pt and add_remove_pt
00038     global pt
00039     global add_remove_pt
00040     
00041     if image is None:
00042         # not initialized, so skip
00043         return
00044     
00045     if image.origin != 0:
00046         # different origin
00047         y = image.height - y
00048         
00049     if event == cv.CV_EVENT_LBUTTONDOWN:
00050         # user has click, so memorize it
00051         pt = (x, y)
00052         add_remove_pt = True
00053 
00054 def snapL(L):
00055     for i,img in enumerate(L):
00056         cv.NamedWindow("snap-%d" % i, 1)
00057         cv.ShowImage("snap-%d" % i, img)
00058         cv.WaitKey()
00059         cv.DestroyAllWindows()        
00060 
00061 def get_sample(filename, iscolor = cv.CV_LOAD_IMAGE_COLOR):
00062     image_cache = {}
00063     if not filename in image_cache:
00064         #filedata = filename
00065         imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
00066         cv.SetData(imagefiledata, filedata, len(filedata))
00067         image_cache[filename] = cv.DecodeImageM(imagefiledata, iscolor)
00068         return image_cache[filename]
00069 
00070 #############################################################################
00071 # so, here is the main part of the program
00072         
00073 if __name__ == '__main__':
00074     
00075     # frames = sys.argv[1:]
00076     # if frames == []:
00077     #     print "usage lkdemo.py <image file1> <image file2>"
00078     #     sys.exit(1)
00079     frames = sys.argv[1:]
00080     if frames == []:
00081         print "usage track_features_lk2.py <image files>"
00082         sys.exit(1)
00083 
00084     fc = 0
00085     while 1:
00086         # do forever
00087         
00088         aa = cv.LoadImage(frames[fc])
00089         bb = cv.LoadImage(frames[fc+1])
00090         a = cv.CreateImage (cv.GetSize (aa), 8, 1)
00091         b = cv.CreateImage (cv.GetSize (aa), 8, 1)
00092         cv.CvtColor (aa, a, cv.CV_BGR2GRAY)
00093         cv.CvtColor (bb, b, cv.CV_BGR2GRAY)
00094         # map = cv.CreateMat(2, 3, cv.CV_32FC1)
00095         # cv.GetRotationMatrix2D((256, 256), 10, 1.0, map)
00096         # b = cv.CloneMat(a)
00097         # cv.WarpAffine(a, b, map)
00098         
00099         eig_image = cv.CreateMat(a.height, a.width, cv.CV_32FC1)
00100         temp_image = cv.CreateMat(a.height, a.width, cv.CV_32FC1)
00101         
00102         prevPyr = cv.CreateMat(a.height / 3, a.width + 8, cv.CV_8UC1)
00103         currPyr = cv.CreateMat(a.height / 3, a.width + 8, cv.CV_8UC1)
00104         prevFeatures = cv.GoodFeaturesToTrack(a, eig_image, temp_image, 400, 0.01, 0.01)
00105         (currFeatures, status, track_error) = cv.CalcOpticalFlowPyrLK(a,
00106                                                                       b,
00107                                                                       prevPyr,
00108                                                                       currPyr,
00109                                                                       prevFeatures,
00110                                                                       (10, 10),
00111                                                                       3,
00112                                                                       (cv.CV_TERMCRIT_ITER|cv.CV_TERMCRIT_EPS,20, 0.03),
00113                                                                       0)
00114         if 1:  # enable visualization
00115             print
00116             print sum(status), "Points found in curr image"
00117             for prev,this in zip(prevFeatures, currFeatures):
00118                 iprev = tuple([int(c) for c in prev])
00119                 ithis = tuple([int(c) for c in this])
00120                 cv.Circle(a, iprev, 3, 255)
00121                 cv.Circle(a, ithis, 3, 0)
00122                 cv.Line(a, iprev, ithis, 128)
00123                 
00124             snapL([a, b])
00125 
00126         fc = (fc + 1) % len(frames)
00127         #exit
00128         if fc == len(frames):
00129             break


image_algos
Author(s): Dejan Pangercic
autogenerated on Mon Oct 6 2014 09:35:23