00001 import roslib
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
00020 def __init__(self, camera_configuration, opencv_id):
00021 self.config = camera_configuration
00022 self.device = opencv_id
00023
00024
00025
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
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
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
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
00091
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
00114
00115
00116
00117 def set_frame_rate(self, rate=7.5):
00118 cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FPS, rate)
00119
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
00130 def set_auto(self):
00131 csl.set_auto(self.device)
00132
00133
00134