def draw_rectangle(self, img, colour=(255,0,0), linewidth=2, offset=2): '''draw a rectange around the region in an image''' (x1,y1,x2,y2) = self.tuple() (wview,hview) = cuav_util.image_shape(img) (w,h) = cuav_util.image_shape(img) x1 = x1*wview//w x2 = x2*wview//w y1 = y1*hview//h y2 = y2*hview//h cv.Rectangle(img, (max(x1-offset,0),max(y1-offset,0)), (x2+offset,y2+offset), colour, linewidth)
def draw_rectangle(self, img, colour=(255, 0, 0), linewidth=2, offset=2): '''draw a rectange around the region in an image''' (x1, y1, x2, y2) = self.tuple() (wview, hview) = cuav_util.image_shape(img) (w, h) = cuav_util.image_shape(img) x1 = x1 * wview // w x2 = x2 * wview // w y1 = y1 * hview // h y2 = y2 * hview // h cv.Rectangle(img, (max(x1 - offset, 0), max(y1 - offset, 0)), (x2 + offset, y2 + offset), colour, linewidth)
def view_imagefile(self, filename): '''view an image in a zoomable window''' img = cuav_util.LoadImage(filename) (w,h) = cuav_util.image_shape(img) for r in self.regions: if r.filename == filename: r.region.draw_rectangle(img, colour=(255,0,0), linewidth=min(max(w/600,1),3), offset=max(w/200,1)) if self.view_image is None or not self.view_image.is_alive(): import wx self.view_image = mp_image.MPImage(title='View', mouse_events=True, key_events=True, can_zoom=True, can_drag=True) vmenu = MPMenuSubMenu('View', items=[ MPMenuItem('Next Image\tCtrl+N', 'Next Image', 'nextImage'), MPMenuItem('Previous Image\tCtrl+P', 'Previous Image', 'previousImage'), MPMenuItem('Fit Window\tCtrl+F', 'Fit Window', 'fitWindow'), MPMenuItem('Full Zoom\tCtrl+Z', 'Full Zoom', 'fullSize'), MPMenuItem('Brightness +\tCtrl+B', 'Increase Brightness', 'increaseBrightness'), MPMenuItem('Brightness -\tCtrl+Shift+B', 'Decrease Brightness', 'decreaseBrightness')]) self.view_menu = MPMenuTop([vmenu]) self.view_image.set_menu(self.view_menu) self.view_image.set_popup_menu(vmenu) self.view_filename = filename self.view_image.set_image(img, bgr=True) self.view_image.set_title('View: ' + os.path.basename(filename))
def view_imagefile(self, filename): '''view an image in a zoomable window''' img = cuav_util.LoadImage(filename, rotate180=self.camera_settings.rotate180) (w,h) = cuav_util.image_shape(img) for i in range(len(self.images)): if filename == self.images[i].filename: self.current_view = i self.images[i].shape = (w,h) for r in self.regions: if r.filename == filename: r.region.draw_rectangle(img, colour=(255,0,0), linewidth=min(max(w/600,1),3), offset=max(w/200,1)) if self.view_image is None or not self.view_image.is_alive(): import wx self.view_image = mp_image.MPImage(title='View', mouse_events=True, key_events=True, can_zoom=True, can_drag=True) vmenu = MPMenuSubMenu('View', items=[ MPMenuItem('Next Image\tCtrl+N', 'Next Image', 'nextImage'), MPMenuItem('Previous Image\tCtrl+P', 'Previous Image', 'previousImage'), MPMenuItem('Fit Window\tCtrl+F', 'Fit Window', 'fitWindow'), MPMenuItem('Full Zoom\tCtrl+Z', 'Full Zoom', 'fullSize'), MPMenuItem('Brightness +\tCtrl+B', 'Increase Brightness', 'increaseBrightness'), MPMenuItem('Brightness -\tCtrl+Shift+B', 'Decrease Brightness', 'decreaseBrightness')]) self.view_menu = MPMenuTop([vmenu]) self.view_image.set_menu(self.view_menu) self.view_image.set_popup_menu(vmenu) self.view_filename = filename self.view_image.set_image(img, bgr=True) self.view_image.set_title('View: ' + os.path.basename(filename))
def score_region(img, r, filter_type='simple'): '''filter a list of regions using HSV values''' (x1, y1, x2, y2) = r.tuple() (w, h) = cuav_util.image_shape(img) x = (x1 + x2) / 2 y = (y1 + y2) / 2 x1 = max(x - 10, 0) x2 = min(x + 10, w) y1 = max(y - 10, 0) y2 = min(y + 10, h) rimg = img[y1:y2, x1:x2] r.score = rgb_score(rimg)
def view_imagefile(self, filename, focus_region=None): '''view an image in a zoomable window''' img = cv2.imread(filename) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) (w,h) = cuav_util.image_shape(img) for i in range(len(self.images)): if filename == self.images[i].filename: self.current_view = i self.last_view_latlon = None self.images[i].shape = (w,h) for r in self.regions: if r.filename == filename: r.region.draw_rectangle(img, colour=(255,0,0), linewidth=min(max(w/600,1),3), offset=max(w/200,1)) if self.view_image is None or not self.view_image.is_alive(): import wx viewwidth = w viewheight = h if self.image_view_width is not None: if viewwidth > self.image_view_width: viewwidth = self.image_view_width viewheight = (h*viewwidth)/w self.view_image = mp_image.MPImage(title='View', mouse_events=True, key_events=True, can_zoom=True, can_drag=True, width=viewwidth, height=viewheight) vmenu = MPMenuSubMenu('View', items=[ MPMenuItem('Next Image\tCtrl+N', 'Next Image', 'nextImage'), MPMenuItem('Previous Image\tCtrl+P', 'Previous Image', 'previousImage'), MPMenuItem('Fit Window\tCtrl+F', 'Fit Window', 'fitWindow'), MPMenuItem('Full Zoom\tCtrl+Z', 'Full Zoom', 'fullSize'), MPMenuItem('Brightness +\tCtrl+B', 'Increase Brightness', 'increaseBrightness'), MPMenuItem('Brightness -\tCtrl+Shift+B', 'Decrease Brightness', 'decreaseBrightness'), MPMenuItem('Refresh Image\tCtrl+R', 'Refresh Image', 'refreshImage'), MPMenuItem('Place Marker\tCtrl+M', 'Place Marker', 'placeMarker')]) self.view_menu = MPMenuTop([vmenu]) self.view_image.set_menu(self.view_menu) self.view_image.set_popup_menu(vmenu) self.view_filename = filename self.view_image.set_image(img) if focus_region is not None: try: self.view_image.center(focus_region.center()) except Exception: pass self.view_image.set_title('View: ' + os.path.basename(filename)) self.view_image.fit_to_window()
def score_region(img, r, min_score=4): '''filter a list of regions using HSV values''' (x1, y1, x2, y2) = r.tuple() if True: (w,h) = cuav_util.image_shape(img) x = (x1+x2)/2 y = (y1+y2)/2 x1 = max(x-10,0) x2 = min(x+10,w) y1 = max(y-10,0) y2 = min(y+10,h) cv.SetImageROI(img, (x1, y1, x2-x1,y2-y1)) hsv = cv.CreateImage((x2-x1,y2-y1), 8, 3) cv.CvtColor(img, hsv, cv.CV_RGB2HSV) cv.ResetImageROI(img) r.score = hsv_score(hsv)
def score_region(img, r, filter_type='simple'): '''filter a list of regions using HSV values''' (x1, y1, x2, y2) = r.tuple() if True: (w,h) = cuav_util.image_shape(img) x = (x1+x2)/2 y = (y1+y2)/2 x1 = max(x-10,0) x2 = min(x+10,w) y1 = max(y-10,0) y2 = min(y+10,h) cv.SetImageROI(img, (x1, y1, x2-x1,y2-y1)) hsv = cv.CreateImage((x2-x1,y2-y1), 8, 3) cv.CvtColor(img, hsv, cv.CV_RGB2HSV) cv.ResetImageROI(img) if filter_type == 'compactness': use_compactness = True else: use_compactness = False hsv_score(r, hsv, use_compactness)
def score_region(img, r, filter_type='simple'): '''filter a list of regions using HSV values''' (x1, y1, x2, y2) = r.tuple() if True: (w,h) = cuav_util.image_shape(img) x = (x1+x2)/2 y = (y1+y2)/2 x1 = int(max(x-10,0)) x2 = int(min(x+10,w)) y1 = int(max(y-10,0)) y2 = int(min(y+10,h)) cv.SetImageROI(img, (x1, y1, x2-x1,y2-y1)) hsv = cv.CreateImage((x2-x1,y2-y1), 8, 3) cv.CvtColor(img, hsv, cv.CV_RGB2HSV) cv.ResetImageROI(img) if filter_type == 'compactness': use_compactness = True else: use_compactness = False hsv_score(r, hsv, use_compactness)
def score_region(img, r, filter_type="simple"): """filter a list of regions using HSV values""" (x1, y1, x2, y2) = r.tuple() if True: (w, h) = cuav_util.image_shape(img) x = (x1 + x2) / 2 y = (y1 + y2) / 2 x1 = max(x - 10, 0) x2 = min(x + 10, w) y1 = max(y - 10, 0) y2 = min(y + 10, h) cv.SetImageROI(img, (x1, y1, x2 - x1, y2 - y1)) hsv = cv.CreateImage((x2 - x1, y2 - y1), 8, 3) cv.CvtColor(img, hsv, cv.CV_RGB2HSV) cv.ResetImageROI(img) if filter_type == "compactness": use_compactness = True else: use_compactness = False r.score = hsv_score(hsv, use_compactness)
def score_region(img, r, filter_type='simple', target_hue=0): '''filter a list of regions using HSV values''' (x1, y1, x2, y2) = r.tuple() (w,h) = cuav_util.image_shape(img) x = (x1+x2)/2 y = (y1+y2)/2 x1 = max(x-10,0) x2 = min(x+10,w) y1 = max(y-10,0) y2 = min(y+10,h) hsv = cv2.cvtColor(img[y1:y2, x1:x2], cv2.COLOR_BGR2HSV) hsv_score(r, hsv) #set score to 0 if outside hue range if target_hue != 0: hue, saturation, lightness = cv2.split(hsv) (height,width,d) = shape(hsv) retb, dest = cv2.threshold(hue, max(target_hue-3, 0), 255, cv2.THRESH_TOZERO) retb, dest = cv2.threshold(dest, min(target_hue+3, 180), 255, cv2.THRESH_TOZERO_INV) ret, dest = cv2.threshold(dest, 0, 255, cv2.THRESH_BINARY) #if less than 5% of the pixels are in hue range, then reject if (cv2.countNonZero(dest)*100/(width*height)) < 5: r.score = 0
def view_imagefile(self, filename): '''view an image in a zoomable window''' img = cuav_util.LoadImage(filename, rotate180=self.camera_settings.rotate180) (w,h) = cuav_util.image_shape(img) for i in range(len(self.images)): if filename == self.images[i].filename: self.current_view = i self.last_view_latlon = None self.images[i].shape = (w,h) for r in self.regions: if r.filename == filename: r.region.draw_rectangle(img, colour=(255,0,0), linewidth=min(max(w/600,1),3), offset=max(w/200,1)) if self.view_image is None or not self.view_image.is_alive(): import wx self.view_image = mp_image.MPImage(title='View', mouse_events=True, key_events=True, can_zoom=True, can_drag=True) vmenu = MPMenuSubMenu('View', items=[ MPMenuItem('Next Image\tCtrl+N', 'Next Image', 'nextImage'), MPMenuItem('Previous Image\tCtrl+P', 'Previous Image', 'previousImage'), MPMenuItem('Fit Window\tCtrl+F', 'Fit Window', 'fitWindow'), MPMenuItem('Full Zoom\tCtrl+Z', 'Full Zoom', 'fullSize'), MPMenuItem('Brightness +\tCtrl+B', 'Increase Brightness', 'increaseBrightness'), MPMenuItem('Brightness -\tCtrl+Shift+B', 'Decrease Brightness', 'decreaseBrightness'), MPMenuItem('Refresh Image\tCtrl+R', 'Refresh Image', 'refreshImage'), MPMenuItem('Download Full\tCtrl+D', 'Download Full', 'downloadFull'), MPMenuItem('Place Marker\tCtrl+M', 'Place Marker', 'placeMarker')]) self.view_menu = MPMenuTop([vmenu]) self.view_image.set_menu(self.view_menu) self.view_image.set_popup_menu(vmenu) self.view_filename = filename self.view_image.set_image(img, bgr=True) self.view_image.set_title('View: ' + os.path.basename(filename))
def tag_image_available(self, color=(0,255,255)): '''tag the small thumbnail image with a marker making it clear the full image is available''' (w,h) = cuav_util.image_shape(self.small_thumbnail) cv.Rectangle(self.small_thumbnail, (w-3,0), (w-1,2), color, 2)
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_full(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) if (w,h) != (1280,960): im_full = cv.CreateImage((1280, 960), 8, 3) cv.Resize(im_orig, im_full) cv.ConvertScale(im_full, im_full, scale=0.3) else: 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_full(im_full) else: regions = scanner.scan(img_scan) count += 1 regions = cuav_region.RegionsConvert(regions) t1=time.time() if opts.filter: regions = cuav_region.filter_regions(im_full, regions, frame_time=frame_time, min_score=opts.minscore) 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, quality=opts.quality) chameleon.save_file('composite.jpg', composite) thumbs = cuav_mosaic.ExtractThumbs(cv.LoadImage('composite.jpg'), 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))
def view_imagefile(self, filename, focus_region=None): '''view an image in a zoomable window''' img = cv2.imread(filename) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) (w, h) = cuav_util.image_shape(img) for i in range(len(self.images)): if filename == self.images[i].filename: self.current_view = i self.last_view_latlon = None self.images[i].shape = (w, h) for r in self.regions: if r.filename == filename: r.region.draw_rectangle(img, colour=(255, 0, 0), linewidth=min(max(w / 600, 1), 3), offset=max(w / 200, 1)) if self.view_image is None or not self.view_image.is_alive(): import wx viewwidth = w viewheight = h if self.image_view_width is not None: if viewwidth > self.image_view_width: viewwidth = self.image_view_width viewheight = (h * viewwidth) / w self.view_image = mp_image.MPImage(title='View', mouse_events=True, key_events=True, can_zoom=True, can_drag=True, width=viewwidth, height=viewheight) vmenu = MPMenuSubMenu('View', items=[ MPMenuItem('Next Image\tCtrl+N', 'Next Image', 'nextImage'), MPMenuItem('Previous Image\tCtrl+P', 'Previous Image', 'previousImage'), MPMenuItem('Fit Window\tCtrl+F', 'Fit Window', 'fitWindow'), MPMenuItem('Full Zoom\tCtrl+Z', 'Full Zoom', 'fullSize'), MPMenuItem('Brightness +\tCtrl+B', 'Increase Brightness', 'increaseBrightness'), MPMenuItem('Brightness -\tCtrl+Shift+B', 'Decrease Brightness', 'decreaseBrightness'), MPMenuItem('Refresh Image\tCtrl+R', 'Refresh Image', 'refreshImage'), MPMenuItem('Place Marker\tCtrl+M', 'Place Marker', 'placeMarker') ]) self.view_menu = MPMenuTop([vmenu]) self.view_image.set_menu(self.view_menu) self.view_image.set_popup_menu(vmenu) self.view_filename = filename self.view_image.set_image(img) if focus_region is not None: try: self.view_image.center(focus_region.center()) except Exception: pass self.view_image.set_title('View: ' + os.path.basename(filename)) self.view_image.fit_to_window()