def LoadImage(filename, rotate180=False, RGB=False): '''wrapper around cv.LoadImage that also handles PGM. It always returns a colour image of the same size''' if filename.endswith('.pgm'): from ..image import scanner try: pgm = PGM(filename) except Exception as e: print('Failed to load %s: %s' % (filename, e)) return None im_full = numpy.zeros((960, 1280, 3), dtype='uint8') if RGB: scanner.debayer_RGB(pgm.array, im_full) else: scanner.debayer(pgm.array, im_full) if rotate180: scanner.rotate180(im_full) return cv.GetImage(cv.fromarray(im_full)) img = cv.LoadImage(filename) if rotate180: from ..image import scanner img = numpy.ascontiguousarray(cv.GetMat(img)) scanner.rotate180(img) img = cv.GetImage(cv.fromarray(img)) return img
def hs_histogram(src): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) # Extract the H and S plane h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane] h_bins = 30 s_bins = 32 hist_size = [h_bins, s_bins] # hue varies from 0 to 180 h_ranges = [0, 180] # saturation varies from 0 to 255 s_ranges = [0, 255] ranges = [h_ranges, s_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) (_, max_value, _, _) = cv.GetMinMaxHistValue(hist) hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3) for h in range(h_bins): for s in range(s_bins): bin_val = cv.QueryHistValue_2D(hist, h, s) intensity = cv.Round(bin_val * 255 / max_value) cv.Rectangle(hist_img, (h * scale, s * scale), ((h + 1) * scale - 1, (s + 1) * scale - 1), cv.RGB(intensity, intensity, intensity), cv.CV_FILLED) return hist_img
def image_handler(self, data): """ Writes frames to avi file. """ self.current_t = rospy.get_time() # Convert to opencv image and then to ipl_image cv_image = self.bridge.imgmsg_to_cv(data, 'bgr8') ipl_image = cv.GetImage(cv_image) with self.lock: if self.cv_image_size == None: self.cv_image_size = cv.GetSize(cv_image) if not self.done: # Write video frame cv.WriteFrame(self.writer, ipl_image) # Update times and frame count - these are used elsewhere so we # need the lock with self.lock: self.frame_count += 1 self.progress_t = self.current_t - self.start_t # Publish progress message with self.lock: self.progress_msg.frame_count = self.frame_count self.progress_msg.progress_t = self.progress_t self.progress_msg.recording_message = self.recording_message self.progress_pub.publish(self.progress_msg)
def image_handler(self, data): """ Writes frames to avi file. """ if self.framerate is not None: # Convert to opencv image and then to ipl_image cv_image = self.bridge.imgmsg_to_cv(data, 'bgr8') ipl_image = cv.GetImage(cv_image) cv_image_size = cv.GetSize(cv_image) if self.frame_count == 0: self.cv_image_size = cv_image_size self.start_video_writer() if cv_image_size != self.cv_image_size: # Abort recording - image size changed. pass if self.recording: # Write video frame cv.WriteFrame(self.writer, ipl_image) self.frame_count += 1 else: stamp = data.header.stamp if self.last_stamp is not None: diff_stamp = stamp - self.last_stamp dt = diff_stamp.to_sec() self.framerate = 1 / dt self.last_stamp = stamp
def SubImage(img, region): '''return a subimage as a new image. This allows for the region going past the edges. region is of the form (x1,y1,width,height)''' (x1, y1, width, height) = region zeros = numpy.zeros((height, width, 3), dtype='uint8') ret = cv.GetImage(cv.fromarray(zeros)) (img_width, img_height) = cv.GetSize(img) if x1 < 0: sx1 = 0 xofs = -x1 else: sx1 = x1 xofs = 0 if y1 < 0: sy1 = 0 yofs = -y1 else: sy1 = y1 yofs = 0 if sx1 + width <= img_width: w = width else: w = img_width - sx1 if sy1 + height <= img_height: h = height else: h = img_height - sy1 cv.SetImageROI(img, (sx1, sy1, w - xofs, h - yofs)) cv.SetImageROI(ret, (xofs, yofs, w - xofs, h - yofs)) cv.Copy(img, ret) cv.ResetImageROI(img) cv.ResetImageROI(ret) return ret
def LoadImage(filename): '''wrapper around cv.LoadImage that also handles PGM. It always returns a colour image of the same size''' if filename.endswith('.pgm'): from ..image import scanner pgm = PGM(filename) im_full = numpy.zeros((960,1280,3),dtype='uint8') scanner.debayer(pgm.array, im_full) return cv.GetImage(cv.fromarray(im_full)) return cv.LoadImage(filename)
def get_image(self): #self.__logger.debug("Retreiving ROS image for topic: %s" % self.__topic) if self.__latest_processed_ros_image == self.__latest_ros_image: return self.__latest_cv_image #TODO: Make encoding ('passthrough', 'bgr8', etc. ) configurable? self.__latest_cv_image = cv.GetImage( self.__cv_bridge.imgmsg_to_cv(self.__latest_ros_image, self.__encoding)) self.__latest_processed_ros_image = self.__latest_ros_image return self.__latest_cv_image
def spectrum(): array = numpy.zeros((480, 640, 3), dtype='uint8') for y in range(480): for x in range(640): h = int((255 * x) / 640) s = 255 v = int((255 * y) / 480) array[y, x] = (h, s, v) hsv = cv.GetImage(cv.fromarray(array)) return hsv
def WriteImageToFile(self, image, imagetopic): if (imagetopic in self.dirFrames): filenameImage = self.dirFrames[ imagetopic] + '/{num:06d}.{ext:s}'.format( num=self.iFrame[imagetopic], ext=self.imageext) matImage = cv.GetImage( self.cvbridge.imgmsg_to_cv(image, 'passthrough')) cv.SaveImage(filenameImage, matImage) self.iFrame[imagetopic] += 1
def filter_regions(img, regions, min_score=4, frame_time=None, filter_type='simple'): '''filter a list of regions using HSV values''' ret = [] img = cv.GetImage(cv.fromarray(img)) for r in regions: if r.score is None: score_region(img, r, filter_type=filter_type) if r.score >= min_score: ret.append(r) return ret
def calc_hist(self): self.hist = cv.CreateHist([self.h_bins, self.s_bins], cv.CV_HIST_ARRAY, self.ranges, 1) hsv = cv.CreateImage(cv.GetSize(self.background_noise[0]), 8, 3) h_plane = cv.CreateMat(self.background_noise[0].height, self.background_noise[0].width, cv.CV_8UC1) s_plane = cv.CreateMat(self.background_noise[0].height, self.background_noise[0].width, cv.CV_8UC1) for i in xrange(len(self.background_noise)): cv.CvtColor(self.background_noise[i], hsv, cv.CV_BGR2HSV) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane]#, s_plane, v_plane] cv.CalcHist([cv.GetImage(i) for i in planes], self.hist, True, self.mask)
def cv2_to_cv(frame): '''Convert a cv2 image into cv format. Keyword Arguments: frame -- a cv2 numpy array representing an image. Returns a cv image. ''' container = cv.fromarray(frame) cv_image = cv.GetImage(container) return cv_image
def loadTemplates(self): u'''テンプレート画像の読み込み''' self._templates = [] for i, cvimageinfo in enumerate(config.template.images): cvmat = cv.CreateMatHeader(cvimageinfo.rows, cvimageinfo.cols, cvimageinfo.type) cv.SetData(cvmat, cvimageinfo.data) self._templates.append(A( image = cv.GetImage(cvmat), number = i, result = None, ))
def TransformImage(self, bgr, hsv, channels, size, method): assert(bgr.type == cv.CV_8UC3) assert(hsv.type == cv.CV_8UC3) chs_all = [ cv.CreateMat(hsv.rows, hsv.cols, cv.CV_8UC1) for i in range(0, 6) ] cv.Split(bgr, chs_all[CH_BLUE], chs_all[CH_GREEN], chs_all[CH_RED], None) cv.Split(hsv, chs_all[CH_HUE], chs_all[CH_SAT], chs_all[CH_VAL], None) chs = [ cv.GetImage(chs_all[ch]) for ch in channels ] dst = cv.CreateMat(hsv.rows - size[1] + 1, hsv.cols - size[0] + 1, cv.CV_32FC1) cv.CalcBackProjectPatch(chs, dst, size, self.hist_pos, method, 1.0) return dst
def image_callback(self, data): """ Image topic callback function. Uses the blobFinder to find the blobs (leds) in the image. Publishes images showing the blobs and the calibration progress. """ # Find blobs with self.lock: if not self.ready: return if self.state == WORKING: self.blobs_list, blobs_image = self.blobFinder.findBlobs(data) blobs_rosimage = self.bridge.cv_to_imgmsg( blobs_image, encoding="passthrough") else: self.blobs_list = [] blobs_rosimage = data if self.image_wait_cnt < self.image_wait_number: self.image_wait_cnt += 1 self.image_blobs_pub.publish(blobs_rosimage) # Create calibration data image cv_image = self.bridge.imgmsg_to_cv(data, desired_encoding="passthrough") ipl_image = cv.GetImage(cv_image) calib_image = cv.CreateImage(cv.GetSize(ipl_image), cv.IPL_DEPTH_8U, 3) cv.CvtColor(ipl_image, calib_image, cv.CV_GRAY2BGR) # Add calibration markers to image color = STATE_COLOR[self.state] for x, y in self.image_points: cv.Circle(calib_image, (int(x), int(y)), 3, color) ## Add text to image message = [STATE_MESSAGE[self.state]] if self.state == WORKING or self.state == FINISHED: #message.append('{0}/{1} pts'.format(len(self.image_points),self.number_of_leds)) message.append('{0}/{1} pts'.format(len(self.image_points), self.get_led_count())) if self.image_info: message.append('- {0}'.format(self.image_info)) message = ' '.join(message) cv.PutText(calib_image, message, (10, 25), self.cv_text_font, color) # Publish calibration progress image calib_rosimage = self.bridge.cv_to_imgmsg(calib_image, encoding="passthrough") self.image_calib_pub.publish(calib_rosimage)
def display_masks(image, mask_array, cv_bridge): for mask in mask_array.masks: img = cv.GetImage(image) pt1 = (mask.roi.x, mask.roi.y) pt2 = (mask.roi.x + mask.roi.width, mask.roi.y + mask.roi.height) cv.Rectangle(image, pt1, pt2, cv.Scalar(255, 0, 0), 2) if len(mask.mask.data) != 0: cv.SetImageROI( img, (mask.roi.x, mask.roi.y, mask.roi.width, mask.roi.height)) cv_mask = cv_bridge.imgmsg_to_cv(mask.mask) cv.AddS(img, (0, 50.0, 0), img, cv_mask) cv.ResetImageROI(img)
def filter_regions(img, regions, min_score=4, frame_time=None, filter_type='simple'): '''filter a list of regions using HSV values''' active_regions = [] img = cv.GetImage(cv.fromarray(img)) for r in regions: if r.scan_score is None: score_region(img, r, filter_type=filter_type) if r.scan_score >= min_score: active_regions.append(r) print(" Regions Above Threshold (Active): %i" % len(active_regions)) return active_regions
def hs_histogram(src, patch): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) hsv_patch= cv.CreateImage(cv.GetSize(patch), 8, 3) # Extract the H and S planes h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) h_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1) h_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1) s_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1) v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, v_plane, None) cv.Split(hsv, h_plane_img, s_plane_img, None, None) cv.Split(hsv_patch, h_plane_patch, s_plane_patch, None, None) #cv.Split(src, h_plane, s_plane, v_plane, None) planes = [h_plane_patch, s_plane_patch]#, s_plane, v_plane] h_bins = 30 s_bins = 32 v_bins = 30 hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) s_ranges = [0, 255] v_ranges = [0, 255] ranges = [h_ranges, s_ranges]#, s_ranges, v_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) (_, max_value, _, _) = cv.GetMinMaxHistValue(hist) hist_img = cv.CreateImage((h_bins*scale, s_bins*scale), 8, 3) back_proj_img = cv.CreateImage(cv.GetSize(src), 8, 1) cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, hist) # for h in range(h_bins): # for s in range(s_bins): # bin_val = cv.QueryHistValue_2D(hist, h, s) # intensity = cv.Round(bin_val * 255 / max_value) # cv.Rectangle(hist_img, # (h*scale, s*scale), # ((h+1)*scale - 1, (s+1)*scale - 1), # cv.RGB(intensity, intensity, intensity), # cv.CV_FILLED) return back_proj_img, hist
def cdf(channel): # -- find cum dist func for individual channel # -- find cumulative histogram and calculate hist to find # which bin (between 0,255) corresponds to what value # and store in refHist hist = cv.CreateHist([256], cv.CV_HIST_ARRAY, [[0, 256]], 1) cv.CalcHist([cv.GetImage(channel)], hist) refHist = [cv.QueryHistValue_1D(hist, x) for x in range(0, 256)] # -- calculate cdf cdf = [v / np.sum(refHist[:]) for v in refHist[:]] for x in range(1, 256): cdf[x] += cdf[x - 1] return cdf
def plot(self, image, color): """Plotting figure plot(self, image, color)""" mask = cv.fromarray(self.description['mask']) #background = cv.fromarray(image.copy()) #print cv.GetSize(background) (width, height) = cv.GetSize(mask) #cv.Set(background, color) x = self.description['offset_x'] y = self.description['offset_y'] cv_image = cv.GetImage(cv.fromarray(image)) cv.SetImageROI(cv_image, (x, y, width, height)) cv.Set(cv_image, color, mask) cv.ResetImageROI(cv_image)
def getHist(self, hBins, sBins): # Extract the H and S planes h_plane = cv.CreateMat(self.src.height, self.src.width, cv.CV_8UC1) s_plane = cv.CreateMat(self.src.height, self.src.width, cv.CV_8UC1) cv.Split(self.src, h_plane, s_plane, None, None) planes = [h_plane, s_plane] hist_size = [hBins, sBins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to 255 (pure spectrum color) s_ranges = [0, 255] ranges = [h_ranges, s_ranges] self.hist = cv.CreateHist([hBins, sBins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], self.hist) return self.hist
def calcHistogram(src, quantization=16): # Convert to HSV #src = cv.fromarray(_src) luv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, luv, cv.CV_RGB2Luv) # Extract the H and S planes size = cv.GetSize(src) L_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1) U_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1) V_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1) cv.Split(luv, L_plane, U_plane, V_plane, None) planes = [L_plane, U_plane, V_plane] #print np.asarray(L_plane),np.asarray(U_plane),np.asarray(V_plane) #Define number of bins L_bins = quantization U_bins = quantization V_bins = quantization #Define histogram size hist_size = [L_bins, U_bins, V_bins] # L_ranges = [0, 255] U_ranges = [0, 255] V_ranges = [0, 255] ranges = [L_ranges, U_ranges, V_ranges] #Create histogram hist = cv.CreateHist([L_bins, U_bins, V_bins], cv.CV_HIST_ARRAY, ranges, 1) #Calc histogram cv.CalcHist([cv.GetImage(i) for i in planes], hist) #Normalize histogram cv.NormalizeHist(hist, 1.0) #Return histogram return hist
def image_handler(self,data): """ Writes frames to avi file. """ self.current_t = rospy.get_time() # Convert to opencv image and then to ipl_image cv_image = self.bridge.imgmsg_to_cv(data,'bgr8') ipl_image = cv.GetImage(cv_image) with self.lock: if self.cv_image_size == None: self.cv_image_size = cv.GetSize(cv_image) if not self.done: # Write video frame cv.WriteFrame(self.writer,ipl_image) # Update times and frame count - these are used elsewhere so we # need the lock with self.lock: self.frame_count += 1 self.progress_t = self.current_t - self.start_t # Check to see if we are done recording - if so stop writing frames if self.current_t >= self.start_t + self.record_t: self.done = True del self.writer self.writer = None self.recording_message = 'finished' db_tools.set_bool(self.redis_db,'recording_flag',False) # Publish progress message with self.lock: self.progress_msg.frame_count = self.frame_count self.progress_msg.record_t = self.record_t self.progress_msg.progress_t = self.progress_t self.progress_msg.recording_message = self.recording_message self.progress_pub.publish(self.progress_msg)
def image_callback(self, data): """ Callback for image topic subscription - used to associate the stamp tuple with the imcomming image. """ if not self.ready: return stamp_tuple = data.header.stamp.secs, data.header.stamp.nsecs with self.lock: self.stamp_to_image[stamp_tuple] = data if self.dummy_image is None: # Create dummy image for use when a dropped frame occurs. cv_image = self.bridge.imgmsg_to_cv( data, desired_encoding="passthrough") raw_image = cv.GetImage(cv_image) dummy_cv_image = cv.CreateImage(cv.GetSize(raw_image), raw_image.depth, raw_image.channels) cv.Zero(dummy_cv_image) self.dummy_image = self.bridge.cv_to_imgmsg( dummy_cv_image, encoding="passthrough")
def LoadImage(filename, rotate180=False, height=960, width=1280): '''wrapper around cv.LoadImage that also handles PGM. It always returns a colour image of the same size''' # if filename.endswith('.pgm'): # from ..image import scanner # try: # pgm = PGM(filename) # except Exception as e: # print('Failed to load %s: %s' % (filename, e)) # return None # im_full = numpy.zeros((height,width,3),dtype='uint8') # scanner.debayer(pgm.array, im_full) # if rotate180: # scanner.rotate180(im_full) # return cv.GetImage(cv.fromarray(im_full)) img = cv.LoadImage(filename) if rotate180: from ..image import scanner img = numpy.ascontiguousarray(cv.GetMat(img)) scanner.rotate180(img) img = cv.GetImage(cv.fromarray(img)) return img
def create_labeled_image(self, ros_image, tracking_pts): """ Create labeled version of stitched image using the tracking points data """ # Convert stitched image from rosimage to opencv image. cv_image = self.bridge.imgmsg_to_cv(ros_image, desired_encoding="passthrough") ipl_image = cv.GetImage(cv_image) # Create color version of stitched image. if self.labeled_image is None: labeled_image = cv.CreateImage(cv.GetSize(ipl_image), cv.IPL_DEPTH_8U, 3) cv.Zero(labeled_image) cv.CvtColor(ipl_image, labeled_image, cv.CV_GRAY2BGR) # Write sequence number on image message = '{0}'.format(tracking_pts.seq) cv.PutText(labeled_image, message, (10, 25), self.cv_text_font, self.magenta) if tracking_pts.found: # Draw boundry box around tracked object p_list = [(int(pt.x), int(pt.y)) for pt in tracking_pts.bndry_stitching_plane] q_list = p_list[1:] + [p_list[0]] for p, q in zip(p_list, q_list): cv.Line(labeled_image, p, q, self.magenta) # Draw circles around tracked object points for pt, color in zip(tracking_pts.pts_stitching_plane, self.tracking_pts_colors): cv.Circle(labeled_image, (int(pt.x), int(pt.y)), 3, color) # Convert labeled image to ros image and publish labeled_rosimage = self.bridge.cv_to_imgmsg(labeled_image, encoding="passthrough") self.labeled_image_pub.publish(labeled_rosimage)
def histogram(self, src): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) h_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1) s_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1) v_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, v_plane, None) planes = [h_plane, s_plane] h_bins = 30 s_bins = 32 hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) s_ranges = [0, 255] ranges = [h_ranges, s_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) max_val = cv.GetMinMaxHistValue(hist)[3] max_hue_bin = max_val[0] max_sat_bin = max_val[1] # display this color in RGB h_interval = 6 s_interval = 8 hue = h_interval * max_hue_bin BGR_color = HSV_to_RGB(hue) cv.NamedWindow("About this color?", cv.CV_WINDOW_AUTOSIZE) cv.MoveWindow("About this color?", 620, 530) color_swatch = cv.CreateImage((200, 140), 8, 3) cv.Set(color_swatch, BGR_color) cv.ShowImage("About this color?", color_swatch) return BGR_color, hue
def smooth_mat(Imat, xwin, ywin, bordertype=None, val=255.0): """ Apply a gaussian blur to IMAT, with window size [XWIN,YWIN]. If BORDERTYPE is 'const', then treat pixels that lie outside of I as VAL (rather than what OpenCV defaults to). Input: cvMat IMAT: """ ''' w, h = Imat.cols, Imat.rows if bordertype == 'const': #Ibig = cv.CreateImage((w+2*xwin, h+2*ywin), I.depth, I.channels) Ibig = cv.CreateMat(h+2*ywin, w+2*xwin, Imat.type) cv.CopyMakeBorder(Imat, Ibig, (xwin, ywin), 0, value=val) cv.SetImageROI(Ibig, (xwin, ywin, w, h)) else: Ibig = Imat #Iout = cv.CreateImage((w,h), I.depth, I.channels) Iout = cv.CreateMat(h, w, I.type) cv.Smooth(Ibig, Iout, cv.CV_GAUSSIAN, param1=xwin, param2=ywin) return Iout ''' return cv.GetMat( smooth(cv.GetImage(Imat), xwin, ywin, bordertype=bordertype, val=val))
def image_depth(self): if self.image is None: return 8 return cv.GetImage(self.image).depth
def process(args): '''process a set of files''' global slipmap, mosaic scan_count = 0 files = [] for a in args: if os.path.isdir(a): files.extend(glob.glob(os.path.join(a, '*.pgm'))) else: files.append(a) files.sort() num_files = len(files) print("num_files=%u" % num_files) region_count = 0 joes = [] if opts.mavlog: mpos = mav_position.MavInterpolator(gps_lag=opts.gps_lag) mpos.set_logfile(opts.mavlog) else: mpos = None if opts.boundary: boundary = cuav_util.polygon_load(opts.boundary) else: boundary = None if opts.mosaic: slipmap = mp_slipmap.MPSlipMap(service='GoogleSat', elevation=True, title='Map') icon = slipmap.icon('planetracker.png') slipmap.add_object(mp_slipmap.SlipIcon('plane', (0,0), icon, layer=3, rotation=0, follow=True, trail=mp_slipmap.SlipTrail())) C_params = cam_params.CameraParams(lens=opts.lens) path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', 'cuav', 'data', 'chameleon1_arecont0.json') C_params.load(path) mosaic = cuav_mosaic.Mosaic(slipmap, C=C_params) if boundary is not None: mosaic.set_boundary(boundary) if opts.joe: joes = cuav_util.polygon_load(opts.joe) if boundary: for i in range(len(joes)): joe = joes[i] if cuav_util.polygon_outside(joe, boundary): print("Error: joe outside boundary", joe) return icon = slipmap.icon('flag.png') slipmap.add_object(mp_slipmap.SlipIcon('joe%u' % i, (joe[0],joe[1]), icon, layer=4)) joelog = cuav_joe.JoeLog('joe.log') if opts.view: viewer = mp_image.MPImage(title='Image') frame_time = 0 for f in files: if mpos: frame_time = cuav_util.parse_frame_time(f) try: if opts.roll_stabilised: roll = 0 else: roll = None pos = mpos.position(frame_time, opts.max_deltat,roll=roll) slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw) except mav_position.MavInterpolatorException as e: print e pos = None else: pos = None # check for any events from the map if opts.mosaic: slipmap.check_events() mosaic.check_events() if f.endswith('.pgm'): pgm = cuav_util.PGM(f) im = pgm.array if pgm.eightbit: im_8bit = im else: im_8bit = numpy.zeros((960,1280,1),dtype='uint8') if opts.gamma != 0: scanner.gamma_correct(im, im_8bit, opts.gamma) else: scanner.reduce_depth(im, im_8bit) im_full = numpy.zeros((960,1280,3),dtype='uint8') scanner.debayer(im_8bit, im_full) im_640 = numpy.zeros((480,640,3),dtype='uint8') scanner.downsample(im_full, im_640) else: im_orig = cv.LoadImage(f) (w,h) = cuav_util.image_shape(im_orig) im_full = im_orig im_640 = cv.CreateImage((640, 480), 8, 3) cv.Resize(im_full, im_640, cv.CV_INTER_NN) im_640 = numpy.ascontiguousarray(cv.GetMat(im_640)) im_full = numpy.ascontiguousarray(cv.GetMat(im_full)) count = 0 total_time = 0 img_scan = im_640 t0=time.time() for i in range(opts.repeat): if opts.fullres: regions = scanner.scan(im_full) regions = cuav_region.RegionsConvert(regions, 1280, 960) else: regions = scanner.scan(img_scan) regions = cuav_region.RegionsConvert(regions) count += 1 t1=time.time() if opts.filter: regions = cuav_region.filter_regions(im_full, regions, frame_time=frame_time, min_score=opts.minscore, filter_type=opts.filter_type) scan_count += 1 # optionally link all the images with joe into a separate directory # for faster re-running of the test with just joe images if pos and opts.linkjoe and len(regions) > 0: cuav_util.mkdir_p(opts.linkjoe) if not cuav_util.polygon_outside((pos.lat, pos.lon), boundary): joepath = os.path.join(opts.linkjoe, os.path.basename(f)) if os.path.exists(joepath): os.unlink(joepath) os.symlink(f, joepath) if pos and len(regions) > 0: joelog.add_regions(frame_time, regions, pos, f, width=1280, height=960, altitude=opts.altitude) if boundary: regions = cuav_region.filter_boundary(regions, boundary, pos) region_count += len(regions) if opts.mosaic and len(regions) > 0: composite = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions) thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions)) mosaic.add_regions(regions, thumbs, f, pos) if opts.compress: jpeg = scanner.jpeg_compress(im_full, opts.quality) jpeg_filename = f[:-4] + '.jpg' if os.path.exists(jpeg_filename): print('jpeg %s already exists' % jpeg_filename) continue chameleon.save_file(jpeg_filename, jpeg) if opts.view: if opts.fullres: img_view = im_full else: img_view = img_scan mat = cv.fromarray(img_view) for r in regions: (x1,y1,x2,y2) = r.tuple() (w,h) = cuav_util.image_shape(img_view) x1 = x1*w//1280 x2 = x2*w//1280 y1 = y1*h//960 y2 = y2*h//960 cv.Rectangle(mat, (max(x1-2,0),max(y1-2,0)), (x2+2,y2+2), (255,0,0), 2) cv.CvtColor(mat, mat, cv.CV_BGR2RGB) viewer.set_image(mat) total_time += (t1-t0) if t1 != t0: print('%s scan %.1f fps %u regions [%u/%u]' % ( f, count/total_time, region_count, scan_count, num_files))