camera.py
Go to the documentation of this file.
00001 import roslib                      # Needed to load opencv
00002 roslib.load_manifest('hrl_camera') #
00003 
00004 import cv
00005 import hrl_opencv.adaptors as ad
00006 import camera_setup_lib as csl
00007 import numpy as np
00008 
00009 class NoFrameException(Exception):
00010     
00011     def __init__(self, value):
00012         self.value = value
00013 
00014     def __str__(self):
00015         return repr(self.value)
00016 
00017 class camera:
00018     ##
00019     # @param camera_configuration a dictionary of parameters needed for this camera
00020     def __init__(self, camera_configuration, opencv_id):
00021         self.config = camera_configuration
00022         self.device = opencv_id
00023         #self._set_registers()
00024 
00025         #create capture and related attributes
00026         self.capture = cv.CaptureFromCAM(self.device)
00027         if not self.capture:
00028             raise RuntimeError("Cannot open camera!\n")
00029         self._make_undistort_matrices()
00030 
00031 
00032     def _make_undistort_matrices(self):
00033         p = self.config
00034         some_arr = np.array([[p['focal_length_x_in_pixels'], 0, p['optical_center_x_in_pixels']],
00035                              [0, p['focal_length_y_in_pixels'], p['optical_center_y_in_pixels']],
00036                              [0, 0, 1.0]])
00037         self.intrinsic_cvmat = ad.array2cvmat(some_arr)
00038         self.distortion_cvmat = ad.array2cvmat(np.array([[p['lens_distortion_radial_1'],
00039                                                        p['lens_distortion_radial_2'],
00040                                                        p['lens_distortion_tangential_1'],
00041                                                        p['lens_distortion_tangential_2']]]))
00042         self.size = (int(p['calibration_image_width']), int(p['calibration_image_height']))
00043 
00044         #Sanity check
00045         size_image = cv.QueryFrame(self.capture)
00046         camera_image_size = cv.GetSize(size_image)
00047         if not ((camera_image_size[0] == self.size[0]) and (camera_image_size[1] == self.size[1])):
00048             raise RuntimeError('Size of image returned by camera and size declared in config. file do not match.' 
00049                                + '  Config:' + str(self.size) + ' Camera: ' + str(camera_image_size))
00050 
00051 
00052         #Set up buffers for undistortion
00053         self.raw_image       = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
00054         self.gray_image      = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
00055         self.undistort_mapx  = cv.CreateImage(self.size, cv.IPL_DEPTH_32F, 1)
00056         self.undistort_mapy  = cv.CreateImage(self.size, cv.IPL_DEPTH_32F, 1)
00057         self.unbayer_image   = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 3)
00058 
00059         self.color = p['color']
00060         if self.color == True:
00061             self.cvbayer_pattern = p['opencv_bayer_pattern'] 
00062         if self.color == True:
00063             self.undistort_image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 3)
00064         else:
00065             self.undistort_image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
00066 
00067         cv.InitUndistortMap(self.intrinsic_cvmat, self.distortion_cvmat, 
00068                             self.undistort_mapx, self.undistort_mapy)
00069         self.corrected_orientation = None
00070 
00071 
00072     def get_frame(self):
00073         self.raw_image = self.get_raw_frame()
00074         im = self.undistort_frame()
00075         if self.config.has_key('upside_down'):
00076             if self.config['upside_down']:
00077                 if self.corrected_orientation == None:
00078                     self.corrected_orientation = cv.CloneImage(im)
00079                 cv.Flip(im, self.corrected_orientation, -1)
00080                 im = self.corrected_orientation
00081         return im
00082 
00083 
00084     ## returns color image. does NOT undistort the image.
00085     def get_frame_debayered(self):
00086         self.raw_image = self.get_raw_frame()
00087         return self.convert_color()
00088 
00089     def get_raw_frame(self):
00090         # Assumes that we are going to debayer the image later, so
00091         # returns a single channel image.
00092         im = cv.QueryFrame(self.capture)
00093 
00094         if im == None:
00095             raise NoFrameException('')
00096         cv.Split(im, self.gray_image, None, None, None)
00097         return self.gray_image
00098 
00099     def undistort_frame(self):
00100         img = self.convert_color()
00101         cv.Remap(img, self.undistort_image, self.undistort_mapx, self.undistort_mapy, 
00102                  cv.CV_INTER_LINEAR, cv.ScalarAll(0)) 
00103         return self.undistort_image
00104 
00105     def convert_color(self):
00106         if self.color == True:
00107             cv.CvtColor(self.raw_image, self.unbayer_image, self.cvbayer_pattern)
00108             return self.unbayer_image
00109         else:
00110             return self.raw_image
00111 
00112     ## 
00113     # Set frame rate: 7.5, 15, 30, or 60Hz
00114     # by default we set this to a low value
00115     # to not have to deal with firewire bandwidth
00116     # issues
00117     def set_frame_rate(self, rate=7.5):
00118         cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FPS, rate)
00119         #csl.set_frame_rate(self.device, rate)
00120 
00121     def get_frame_rate(self):
00122         fps = cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FPS)
00123         return fps
00124 
00125     def set_brightness(self, brightness=150, shutter_time=97,
00126                        gain=450, exposure=None):
00127         csl.set_brightness(self.device, brightness, exposure, shutter_time, gain)
00128 
00129     ## auto - probably only used for displaying images.
00130     def set_auto(self):
00131         csl.set_auto(self.device)
00132     
00133 #if __name__ == '__main__':
00134 #    cva = ad.array2cv(np.ones((2,3)))


hrl_camera
Author(s): Hai Nguyen, Advait Jain, Cressel Anderson, Marc Killpack
autogenerated on Wed Nov 27 2013 11:37:01