def demo_overlay(): """How to overlay images and highlight regions.""" # Overlay rgb = imutils.imread('../data/flamingo.jpg', mode='L') # Generate some data to overlay im_height, im_width = rgb.shape[0], rgb.shape[1] peak_pos = (im_width * 0.75, im_height * 0.15) xv, yv = np.meshgrid(np.arange(0, im_width), np.arange(0, im_height)) overlay = np.exp( -(np.power(xv - peak_pos[0], 2) + np.power(yv - peak_pos[1], 2)) / (3e4)) overlay_vis = imvis.overlay(imvis.pseudocolor(overlay), rgb, 0.7) # Highlight regions rgb = imutils.imread('../data/flamingo.jpg', mode='RGB') rgb_mask = np.zeros((rgb.shape[0], rgb.shape[1]), dtype=np.uint8) rgb_mask[160:334, 120:290] = 1 highlight = imvis.highlight(rgb, rgb_mask) # highlight another region, this time in blue rgb_mask[:] = 0 rgb_mask[200:374, 250:420] = 1 highlight = imvis.highlight(highlight, rgb_mask, color=(0, 0, 255)) # Combine all "colored" images: collage = imvis.make_collage([overlay_vis, highlight], padding=5, bg_color=(255, 255, 255)) imvis.imshow(collage, title="Overlay & Highlights", wait_ms=-1)
def gui(): # Prepare example data rgb = imutils.imread( os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'data', 'flamingo.jpg')) # 3-channel grayscale gray_ = imutils.rgb2gray(rgb, is_bgr=False) gray = np.dstack((gray_, gray_, gray_)) # 3 images with highlighted red, green, and blue channels def __highlight(img, c): res = img.copy() for i in range(res.shape[2]): if i != c: res[:, :, i] = 0 return res channels = [__highlight(rgb, c) for c in range(3)] # Exemplary "image sequence" images = [rgb, *channels, gray] app = QApplication(['Image Sequence Plot']) main_widget = DemoApplication() main_widget.displayImageSequence(images) main_widget.show() sys.exit(app.exec_())
def __load_request(self): filename, _ = QFileDialog.getOpenFileName( self, "Open image", "", 'Images (*.bmp *.jpg *.jpeg *.png *.ppm);;All Files (*.*)', '', QFileDialog.DontUseNativeDialog) if filename is not None: img = imutils.imread(filename) self.displayImage(img)
def gui(): img = imutils.imread( os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'data', 'flamingo.jpg')) app = QApplication(['Cartoonification']) main_widget = DemoApplication() main_widget.displayImage(img) main_widget.show() sys.exit(app.exec_())
def demo_primitives(): """How to draw basic shapes, text boxes, etc.""" # * rotated rect around dot, maybe img = imutils.imread('../data/ninja.jpg', mode='RGB') # Load grayscale as 3-channel image # Draw rounded box(es) vis_img = imvis.draw_rounded_rects(img, [(9, 23, 149, 106)], corner_percentage=0.2, fill_opacity=0.75, line_width=0, color=(0, 200, 200), non_overlapping=True) # Draw filled & dashed rect # vis_img = imvis.draw_rects(vis_img, [(178, 164, 43, 29)], fill_opacity=0.4, line_width=2, dash_length=10, color=(220, 0, 255)) # Draw rotated rectangle vis_img = imvis.draw_rotated_rects(vis_img, [(60, 220, 70, 45, -55)], fill_opacity=0.5, line_width=2, dash_length=15, color=(0, 200, 0)) # Draw lines - a line is a list or tuple: ((start-point), (end-point), is_dashed, color) lines = [ [(7, 184), (329, 211), True, (255, 0, 255)], # Dashed [(42, 147), (337, 168), False, (255, 0, 255)] ] # Solid vis_img = imvis.draw_lines(vis_img, lines, line_width=3, default_color=(200, 255, 0), dash_length=15) # Draw arrows - same format as lines (see above) arrows = [[(314, 20), (175, 38), False, (0, 255, 255)], ((314 + 20, 20 + 30), (175 + 20, 38 + 30), True, (255, 0, 255))] #[(320, 33), (316, 87), True, (0, 255, 255)]] vis_img = imvis.draw_arrows(vis_img, arrows, line_width=2, default_color=(0, 200, 255), dash_length=15, arrow_head_factor=0.1) # Draw text box vis_img = imvis.draw_text_box(vis_img, 'Angry', (283, 68), text_anchor='west', bg_color=(255, 0, 255), font_color=(-1, -1, -1), font_scale=1.0, font_thickness=1, padding=5, fill_opacity=0.8) imvis.imshow(vis_img, title='Drawing Primitives', wait_ms=10)
def calibrated_example(): """Returns an exemplary image with corresponding camera calibration and pose.""" img = imutils.imread('../data/ninja.jpg', mode='RGB') # Load grayscale as 3-channel image img_height, img_width = img.shape[:2] # Get camera extrinsics via PnP img_points = np.array([(4.4, 183.4), (76.6, 190.6), (155.5, 197.5), (240.2, 204.8), (328.4, 210.6), (44, 147.1), (103.2, 151.1), (167.7, 156.2), (235, 160), (305.9, 164.7), (122, 125), (175.9, 128.1), (289, 134.4), (327.1, 115.5), (33.4, 253.4)], dtype=np.float32) # Corresponding grid points grid_points = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (1, 2), (2, 2), (4, 2), (5, 3), (1, -1)] # Convert to mm grid_width_mm = 24.0 obj_points = np.array([[s[0] * grid_width_mm, s[1] * grid_width_mm, 0.0] for s in grid_points], dtype=np.float32) # Compute focal length in pixels focal_length_mm = 5.6 sensor_width_mm = 7.3 # sensor_height_mm = 5.47, sensor resolution 7296x5472 (40 MP) fl = focal_length_mm / sensor_width_mm * img_width # Good enough guess for principal point and distortion :-p cx = img_width / 2.0 cy = img_height / 2.0 dist_coeff = np.zeros(4) K = np.float64([[fl, 0, cx], [0, fl, cy], [0, 0, 1]]).reshape((3, 3, 1)) # print(K) # print(obj_points, obj_points.shape, np.transpose(obj_points)) # print(img_points.shape) # objectPoints = np.random.random((10,3,1)) # imagePoints = np.random.random((10,2,1)) # cameraMatrix = np.eye(3) # distCoeffs = np.zeros((5,1)) # ret, rvec, tvec = cv2.solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs) # Solve for the extrinsics ret, rvec, t = cv2.solvePnP(obj_points, img_points, K, dist_coeff) if not ret: raise RuntimeError('Could not recover camera pose!') # print(ret, rvec, t) # verts = cv2.projectPoints(obj_points, rvec, t, K, dist_coeff)[0].reshape(-1, 2) # print(verts) R, _ = cv2.Rodrigues(rvec) # vis_img = imvis.draw_points(img, np.transpose(img_points), color=(255, 0, 0)) # vis_img = imvis.draw_points(vis_img, np.transpose(verts), color=(0, 255, 255)) return img, K, R, t
def demo_pseudocolor(): """Pseudocoloring.""" peaks = imutils.imread('../data/peaks.png', mode='L') # For visualization purposes only, reduce input to a few # distinct categories/labels: data = ((peaks / 25) - 5).astype(np.int16) names = ['Bone', 'Magma', 'Viridis'] images = list() for name in names: pc = imvis.pseudocolor( data, limits=None, # Compute min/max from data color_map=colormaps.by_name(name, return_rgb=True)) images.append(pc) # Display as a single collage padding = 10 # Add alpha channel to render the collage nicely for the repo's README images[0] = np.dstack( (images[0], 255 * np.ones(images[0].shape[:2], dtype=np.uint8))) collage = imvis.make_collage(images, padding=padding, fixed_size_per_image=(200, 200), bg_color=(0, 0, 0, 0), num_images_per_row=len(images)) # Add labels height, width = collage.shape[:2] mask_width = (width - (len(names) - 1) * padding) / len(names) for i in range(len(names)): pos = (i * (mask_width + padding) + mask_width / 2, height - 10) collage = imvis.draw_text_box(collage, names[i], pos, text_anchor='south', bg_color=(0, 0, 0), font_color=(-1, -1, -1), font_scale=1.0, font_thickness=1, padding=5, fill_opacity=0.8) imvis.imshow(collage, title='Pseudocoloring', wait_ms=-1) imutils.imsave('../../doc/example-pseudocolor.png', collage)
import os import sys import cv2 # Add path to the pypvt3 package sys.path.append( os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'gen')) from vcp import ui_basics from vcp import imutils from vcp import imvis if __name__ == "__main__": # Load as BGR img = imutils.imread('../data/flamingo.jpg', flip_channels=True) # Select a rectangle valid, rect = ui_basics.select_rectangle( img, rect_color=(0, 255, 255), window_name='Rectangle selection demo') print('User confirmed: ', valid, ', rect: ', rect) # Select a single point valid, pt = ui_basics.select_point( img, point_color=(255, 0, 0), window_name='[Single] Point selection demo', marker='cross', thickness=3) print('User confirmed: ', valid, ', pt: ', pt)
def demo_bbox2d(): """Renders 2D bounding boxes (with annotations) & fading trajectories.""" # Bounding boxes img = imutils.imread('../data/ninja.jpg', mode='RGB') # Load grayscale as 3-channel image bboxes2d = [ # ((178, 164, 43, 29), (255, 0, 255), '', True), # Dashed violett rect (at the target's dot) (np.array([177, 76, 147, 53]), (0, 255, 255), 'Katana' ), # Also accepts a 4-element np.array ] vis_img = imvis.draw_bboxes2d(img, bboxes2d, text_anchor='south', font_scale=1.0, font_thickness=1, text_box_opacity=0.75, line_width=2) # Rotated bounding box vis_img = imvis.draw_rotated_bboxes2d(vis_img, [((252, 148, 60, 30, 15), (0, 200, 0), 'Tabi'), ((80, 68, 150, 90, -10), (0, 255, 255), 'Lens')], line_width=2, fill_opacity=0.4, font_color=(0, 0, 0), text_anchor="north", font_scale=1.0, font_thickness=1, text_padding=5, text_box_opacity=0.8) # Trajectory traj_sword = [(323, 96), (321, 83), (317, 68), (310, 54), (305, 44), (294, 35), (283, 29), (273, 27), (261, 26), (246, 28), (231, 33), (217, 40), (207, 49), (201, 62), (196, 74), (192, 87), (183, 100), (175, 112), (159, 120), (144, 123), (128, 123), (115, 119)] # traj_ninja = [(278, 148), (290, 147), (302, 150), (307, 162), # (307, 177), (287, 184), (270, 187), (256, 195), (239, 199), (222, 211), (211, 222), (204, 230), (197, 239)] vis_img = imvis.draw_fading_trajectory( vis_img, traj_sword, newest_position_first=True, smoothing_window=7, trajectory_length=-1, obj_color=(0, 0, 255), fade_color=(180, 180, 180), # Fade towards gray max_line_width=4, dash_length=-1) # from vcp import ui_basics # print(ui_basics.select_points(vis_img)) # # Flip the trajectory # trajectory = imutils.flip_points(trajectory, image_size=(im_width, im_height), flip_horizontally=True, flip_vertically=False) # # Rotate the trajectory # trajectory = imutils.rotate_points(trajectory, (im_width/2.0, im_height/2.0), np.deg2rad(-20)) # # Draw without fading # vis_img = imvis.draw_trajectory(vis_img, trajectory, smoothing_window=-1, color=(255,0,0), dash_length=10, line_width=3) # Simplify with RDP # trajectory = math2d.simplify_trajectory_rdp(trajectory, 50) # vis_img = trajvis.draw_trajectory(vis_img, trajectory, smoothing_window=-1, obj_color=(0,0,0), dash_length=10, line_width=3) return vis_img
def test_closest_point2d(): line = ((1, 1), (3, 3)) pts = [(-1, 0), (0.5, 1.5), (2.5, 1.5), (3, 3), (6, 2)] exp_line = [(-0.5, -0.5), (1, 1), (2, 2), (3, 3), (4, 4)] exp_segment = [(1, 1), (1, 1), (2, 2), (3, 3), (3, 3)] for i in range(len(pts)): res = math2d.closest_point_on_line(pts[i], line) tuple_approx(res, exp_line[i]) res = math2d.closest_point_on_line_segment(pts[i], line) tuple_approx(res, exp_segment[i]) if __name__ == "__main__": # Rotate rect: #TODO not math related - move to imvis demo rgb = imutils.imread('../../examples/data/flamingo.jpg', mode='RGB', flip_channels=False) im_height, im_width = rgb.shape[0], rgb.shape[1] center = (im_width / 2.0, im_height / 2.0) rect = (im_width * 3 / 4, im_height / 2, 50, 80) vis_img = imvis.draw_rects(rgb, [rect], fill_opacity=0.4, line_width=2, dash_length=10, color=(220, 0, 255)) rotrect = imutils.rotate_rect(rect, center, np.deg2rad(5)) vis_img = imvis.draw_rotated_rects(vis_img, [rotrect], fill_opacity=0.4, line_width=2, dash_length=10,
def __load_images(self, filenames): if filenames is None or len(filenames) == 0: return images = [imutils.imread(fn) for fn in filenames] self.displayImageSequence(images)
def demo(pseudocolor=True, inspect=False): imgs = [imutils.imread('../data/ninja-01.jpg'), imutils.imread('../data/ninja-02.jpg')] bgmodels = list() bgmodel = bgm.BackgroundModel() bgmodel.approximate_median_bgm( adaptation_step=5, fg_report_threshold=50, median_on_grayscale=True) bgmodels.append(bgmodel) bgmodel = bgm.BackgroundModel() bgmodel.block_mean_bgm( block_size=(16, 16), block_overlap=0.5, update_rate=0.01, fg_report_threshold=50, channel='grayscale') bgmodels.append(bgmodel) bgmodel = bgm.BackgroundModel() bgmodel.gaussian_mixture_bgm( history=500, detect_shadows=True, var_thresh=100, comp_thresh=0.05) bgmodels.append(bgmodel) bgmodel = bgm.BackgroundModel() bgmodel.normalized_rgb_bgm( report_as_binary=False, # binary_reporting_threshold=20, update_rate=0.1, alpha=0.1, beta=1.0) bgmodels.append(bgmodel) print(""" ! Note that the reported initialization time for the first BGM includes library initialization - maybe OpenCV (haven't tracked that down yet). """) fg_masks = list() for bgmodel in bgmodels: pyutils.tic('init') bgmodel.init(imgs[0]) t_init = pyutils.ttoc('init') pyutils.tic('apply') mask = bgmodel.report_changes(imgs[1]) t_apply = pyutils.ttoc('apply') if pseudocolor: fg_masks.append(imvis.pseudocolor(mask, limits=None, color_map=colormaps.colormap_viridis_rgb)) else: fg_masks.append(mask) print('init/apply: {:17s} {:7.2f} ms, {:7.2f} ms'.format(bgmodel.name(), t_init, t_apply)) if inspect: import iminspect iminspect.show(mask) padding = 10 # Add alpha channel to render the README visualization nicely for web display fg_masks[0] = np.dstack((fg_masks[0], 255 * np.ones(fg_masks[0].shape[:2], dtype=np.uint8))) collage = imvis.make_collage(fg_masks, padding=padding, fixed_size_per_image=(200, 266), bg_color=(0, 0, 0, 0), num_images_per_row=2) input_seq = cv2.resize(imutils.imread('../data/ninja-seq.png'), (200, 266)) collage = imvis.make_collage([input_seq, collage], padding=padding, bg_color=(0, 0, 0, 0)) # Overlay names height, width = collage.shape[:2] collage = imvis.draw_text_box(collage, 'Input', (input_seq.shape[1]/2, height/3+20), text_anchor='center', bg_color=(255, 255, 255), font_color=(-1, -1, -1), font_scale=1.0, font_thickness=1, padding=5, fill_opacity=0.8) names = [bg.name() for bg in bgmodels] mask_width = (width-input_seq.shape[1] - padding) / 2 for i in range(len(names)): pos = (input_seq.shape[1] + padding + (i % 2) * (mask_width + padding) + mask_width/2, (i // 2) * (266 + padding) + 266 - 10) collage = imvis.draw_text_box(collage, names[i], pos, text_anchor='south', bg_color=(255, 255, 255), font_color=(-1, -1, -1), font_scale=1.0, font_thickness=1, padding=5, fill_opacity=0.8) imvis.imshow(collage, title="Background Subtraction", wait_ms=-1) imutils.imsave('../../doc/example-bgm.png', collage)