def show_image(self, gain=1, processed=False, compare=False, median_filter=False, zero_bg=False, save_as=None): img = self.image.astype('float') if processed: if zero_bg: img = np.clip( img - np.min(img) - (0 if zero_bg is True else zero_bg), 0, np.inf) img *= gain if median_filter: img = cv2.medianBlur(img.astype('uint16'), median_filter) img = ImageProc.color_correct(img, self.applied_bgr_mx, max_val=self.max_val) img = ImageProc.adjust_gamma(img, self.applied_gamma, self.applied_gamma_break, max_val=self.max_val) else: img = np.clip(img * gain, 0, 2**self.bits - 1) img = ImageProc.change_color_depth(img, self.bits, 8).astype('uint8') if save_as is not None: cv2.imwrite(save_as, img) s = self.image.shape if compare: img = np.hstack((self.raw_image.astype(img.dtype), np.ones((s[0], 1, s[2]), dtype=img.dtype), img)) sc = 1 plt.imshow(np.flip(img, axis=2)) plt.show() return img, sc
def write_img(raw_imgs, outfile): imgs = [] for raw in raw_imgs: img = ImageProc.change_color_depth(raw.astype('float'), 8, bits) img = ImageProc.adjust_gamma(img, gamma, gamma_break=gamma_break, inverse=True, max_val=max_val) if bgr_cc_mx is not None: img = ImageProc.color_correct(img, bgr_cc_mx, inverse=True, max_val=max_val) imgs.append(np.expand_dims(img, axis=0)) if len(imgs) == 1: imgs = imgs[0] stacked = np.stack(imgs, axis=0) reduced = np.median(stacked, axis=0) if len(imgs) > 2 else np.min( stacked, axis=0) bg_img = np.round(reduced).squeeze().astype('uint16') cv2.imwrite(outfile, bg_img, (cv2.CV_16U, ))
def __init__(self, cam, gain, exposure, timestamp, raw_image, background_img, bg_offset=0, bits=8, applied_gamma=1.0, applied_gamma_break=0.0, applied_bgr_mx=None, debug=False): self.id = Frame.CURRENT_ID Frame.CURRENT_ID += 1 self.cam = [cam] if isinstance(cam, Camera) else cam self.resize_scale = raw_image.shape[1] / self.cam[0].width for c in self.cam: c.height, c.width = raw_image.shape[:2] self.bits = bits = int(bits) self.gain = gain self.exposure = exposure self.timestamp = timestamp self.raw_image = raw_image self.applied_gamma = applied_gamma self.applied_gamma_break = applied_gamma_break self.applied_bgr_mx = applied_bgr_mx self.debug = debug img_bits = int(str(raw_image.dtype)[4:]) max_val = 2**img_bits - 1 img = raw_image.astype('float') # NOTE: NanoCam has this, doesnt make sense in general! operation_order = reversed(( 'ex_gamma', 'depth', 'color', 'gamma', )) for op in operation_order: if op == 'depth' and img_bits != bits: img = ImageProc.change_color_depth(img, img_bits, bits) max_val = 2**bits - 1 if op == 'gamma' and applied_gamma != 1.0: img = ImageProc.adjust_gamma(img, applied_gamma, gamma_break=applied_gamma_break, inverse=True, max_val=max_val) if op == 'color' and applied_bgr_mx is not None: img = ImageProc.color_correct(img, applied_bgr_mx, inverse=True, max_val=max_val) # if op == 'ex_gamma' and GAMMA_ADJUSTMENT: # img = ImageProc.adjust_gamma(img, GAMMA_ADJUSTMENT, inverse=True, max_val=max_val) self.background_img = background_img if background_img is not None: self.image = ImageProc.remove_bg(img, background_img, gain=1, offset=bg_offset, max_val=max_val) elif self.MISSING_BG_REMOVE_STRIPES: for k in range(img.shape[2]): img[:, :, k] -= np.percentile(img[:, :, k], 50, axis=0).reshape((1, -1)) img[:, :, k] -= np.percentile(img[:, :, k], 50, axis=1).reshape((-1, 1)) img += bg_offset - np.min(img) self.image = np.clip(img, 0, max_val) else: self.image = img if bg_offset is not False: self.image = np.round(self.image).astype('uint16') self.measures = []