annotate_images.py
Go to the documentation of this file.
00001 #    print 'Following are the key that you can use:'
00002 #    print '1. Hit ESC to end'
00003 #    print '2. Hit - to decrease the size of the image'
00004 #    print '3. Hit + (without the shift key) to increase the image size'
00005 #    print '4. Hit c to detect chessboard corners'
00006 
00007 import sys, time
00008 import numpy as np, math
00009 import glob, commands
00010 
00011 import roslib; roslib.load_manifest('modeling_forces')
00012 import cv
00013 from cv_bridge.cv_bridge import CvBridge, CvBridgeError
00014 
00015 import hrl_lib.util as ut
00016 
00017 # call func on the 8 neighbors of x,y and on x,y
00018 def call_neighborhood(func,func_params,x,y):
00019     func(x+0,y+0,*func_params)
00020     func(x+1,y+0,*func_params)
00021     func(x+1,y-1,*func_params)
00022     func(x+0,y-1,*func_params)
00023     func(x-1,y-1,*func_params)
00024     func(x-1,y+0,*func_params)
00025     func(x-1,y+1,*func_params)
00026     func(x-0,y+1,*func_params)
00027     func(x+1,y+1,*func_params)
00028 
00029 
00030 def on_mouse(event, x, y, flags, param):
00031     im, clicked_list = param[0], param[1]
00032     scale_factor = param[2]
00033     if event == cv.CV_EVENT_LBUTTONDOWN:
00034         #call_neighborhood(cv.SetPixel,(im,(255,0,0)),x,y)
00035         clicked_list.append((x/scale_factor, y/scale_factor)) # these are in mm
00036 
00037 
00038 def annotate_image(cv_im, mech_info_dict, dir):
00039     #cv_im = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_GRAYSCALE)
00040     size = cv.GetSize(cv_im)
00041 
00042     print 'Image size:', size[0], size[1] # col, row
00043     wnd = 'Image Annotate'
00044     cv.NamedWindow(wnd, cv.CV_WINDOW_AUTOSIZE)
00045     disp_im = cv.CloneImage(cv_im)
00046     new_size = (size[0]/2, size[1]/2)
00047     scale_factor = 1
00048 
00049     checker_origin_height = mech_info_dict['height']
00050     # chesscoard corners mat
00051     cb_dims = (5, 8) # checkerboard dims. (x, y)
00052     sq_sz = 19 # size of checkerboard in real units.
00053     cb_offset = 500,500
00054     cb_coords = cv.CreateMat(2, cb_dims[0]*cb_dims[1], cv.CV_64FC1)
00055     n = 0
00056     for r in range(cb_dims[1]):
00057         for c in range(cb_dims[0]):
00058             cb_coords[0,n] = c*sq_sz + cb_offset[0] # x coord
00059             cb_coords[1,n] = r*sq_sz + cb_offset[1] # y coord
00060             n += 1
00061 
00062     clicked_list = []
00063     recreate_image = False
00064     mechanism_calc_state = 0
00065     mechanism_calc_dict = {}
00066     while True:
00067         for p in clicked_list:
00068             x,y = p[0]*scale_factor, p[1]*scale_factor
00069             cv.Circle(disp_im, (x,y), 3, (255,0,0))
00070         cv.ShowImage(wnd, disp_im)
00071         k = cv.WaitKey(10)
00072         k = k%256
00073 
00074         if k == 27:
00075             # ESC
00076             break
00077         elif k == ord('='):
00078             # + key without the shift
00079             scale_factor = scale_factor*1.2
00080             recreate_image = True
00081         elif k == ord('-'):
00082             # - key
00083             scale_factor = scale_factor/1.2
00084             recreate_image = True
00085         elif k == ord('c'):
00086             # find chessboard corners.
00087             succ, corners = cv.FindChessboardCorners(disp_im, cb_dims)
00088             if succ == 0:
00089                 print 'Chessboard detection FAILED.'
00090             else:
00091                 # chessboard detection was successful
00092                 cv.DrawChessboardCorners(disp_im, cb_dims, corners, succ)
00093                 cb_im = cv.CreateMat(2, cb_dims[0]*cb_dims[1], cv.CV_64FC1)
00094                 corners_mat = np.array(corners).T
00095                 n = 0
00096                 for r in range(cb_dims[1]):
00097                     for c in range(cb_dims[0]):
00098                         cb_im[0,n] = corners_mat[0,n] # x coord
00099                         cb_im[1,n] = corners_mat[1,n] # y coord
00100                         n += 1
00101                 H = cv.CreateMat(3, 3, cv.CV_64FC1)
00102                 H1 = cv.FindHomography(cb_im, cb_coords, H)
00103                 Hnp = np.reshape(np.fromstring(H1.tostring()), (3,3))
00104                 print 'Homography:'
00105                 print Hnp
00106 
00107                 d = cv.CloneImage(disp_im)
00108                 cv.WarpPerspective(d, disp_im, H1, cv.CV_WARP_FILL_OUTLIERS)
00109                 cv_im = cv.CloneImage(disp_im)
00110         elif k == ord('1'):
00111             # calculate width of the mechanism
00112             del clicked_list[:]
00113             cv.SetMouseCallback(wnd, on_mouse, (disp_im, clicked_list, scale_factor))
00114             recreate_image = True
00115             print 'Click on two endpoints to mark the width of the mechanism'
00116             mechanism_calc_state = 1
00117         elif k == ord('2'):
00118             # distance of handle from the hinge
00119             del clicked_list[:]
00120             cv.SetMouseCallback(wnd, on_mouse, (disp_im, clicked_list, scale_factor))
00121             recreate_image = True
00122             print 'Click on handle and hinge to compute distance of handle from hinge.'
00123             mechanism_calc_state = 2
00124         elif k == ord('3'):
00125             # height of the handle above the ground
00126             del clicked_list[:]
00127             cv.SetMouseCallback(wnd, on_mouse, (disp_im, clicked_list, scale_factor))
00128             recreate_image = True
00129             print 'Click on handle top and bottom to compute height of handle above the ground.'
00130             mechanism_calc_state = 3
00131         elif k == ord('4'):
00132             # top and bottom edge of the mechanism
00133             del clicked_list[:]
00134             cv.SetMouseCallback(wnd, on_mouse, (disp_im, clicked_list, scale_factor))
00135             recreate_image = True
00136             print 'Click on top and bottom edges of the mechanism.'
00137             mechanism_calc_state = 4
00138         elif k ==ord('d'):
00139             # display the calculated values
00140             print 'mechanism_calc_dict:', mechanism_calc_dict
00141             print 'mech_info_dict:', mech_info_dict
00142         elif k == ord('s'):
00143             # save the pkl
00144             ut.save_pickle(mechanism_calc_dict, dir+'/mechanism_calc_dict.pkl')
00145             print 'Saved the pickle'
00146         #elif k != -1:
00147         elif k != 255:
00148             print 'k:', k
00149 
00150         if recreate_image:
00151             new_size = (int(size[0]*scale_factor),
00152                         int(size[1]*scale_factor))
00153             disp_im = cv.CreateImage(new_size, cv_im.depth, cv_im.nChannels)
00154             cv.Resize(cv_im, disp_im)
00155             cv.SetMouseCallback(wnd, on_mouse, (disp_im, clicked_list, scale_factor))
00156             recreate_image = False
00157 
00158         if len(clicked_list) == 2:
00159             if mechanism_calc_state == 1:
00160                 w = abs(clicked_list[0][0] - clicked_list[1][0])
00161                 print 'Width in mm:', w
00162                 mechanism_calc_dict['mech_width'] = w/1000.
00163             if mechanism_calc_state == 2:
00164                 w = abs(clicked_list[0][0] - clicked_list[1][0])
00165                 print 'Width in mm:', w
00166                 mechanism_calc_dict['handle_hinge_dist'] = w/1000.
00167             if mechanism_calc_state == 3:
00168                 p1, p2 = clicked_list[0], clicked_list[1]
00169                 h1 = (cb_offset[1] - p1[1])/1000. + checker_origin_height
00170                 h2 = (cb_offset[1] - p2[1])/1000. + checker_origin_height
00171                 mechanism_calc_dict['handle_top'] = max(h1, h2)
00172                 mechanism_calc_dict['handle_bottom'] = min(h1, h2)
00173             if mechanism_calc_state == 4:
00174                 p1, p2 = clicked_list[0], clicked_list[1]
00175                 h1 = (cb_offset[1] - p1[1])/1000. + checker_origin_height
00176                 h2 = (cb_offset[1] - p2[1])/1000. + checker_origin_height
00177                 mechanism_calc_dict['mechanism_top'] = max(h1, h2)
00178                 mechanism_calc_dict['mechanism_bottom'] = min(h1, h2)
00179 
00180             mechanism_calc_state = 0
00181 
00182 
00183 
00184 
00185 if __name__ == '__main__':
00186 
00187     import optparse
00188     p = optparse.OptionParser()
00189     p.add_option('-d', '--dir', action='store', default='',
00190                  type='string', dest='dir', help='mechanism directory')
00191     opt, args = p.parse_args()
00192 
00193 #    dir_list = commands.getoutput('ls -d %s/*/'%(opt.dir)).splitlines()
00194 #    dir_list = dir_list[:]
00195 #    for direc in dir_list:
00196     img_list = glob.glob(opt.dir+'/*.jpg')
00197     mech_info_dict = ut.load_pickle(opt.dir+'/mechanism_info.pkl')
00198     for img in img_list:
00199         cv_im = cv.LoadImage(img)
00200         annotate_image(cv_im, mech_info_dict, opt.dir)
00201 
00202     sys.exit()
00203 
00204 #    cv.DestroyAllWindows()
00205     #print 'k:', chr(k)
00206 
00207 


2010_biorob_everyday_mechanics
Author(s): Advait Jain, Hai Nguyen, Charles C. Kemp (Healthcare Robotics Lab, Georgia Tech)
autogenerated on Wed Nov 27 2013 11:58:43