def gen_image(I): global recorrelate, corr, heatmaps, map_out if recorrelate: heatmaps = np.zeros((len(filters_ind), I.shape[0], I.shape[1]), dtype=np.float32) for j, i in enumerate(filters_ind): heatmaps[j] = correlate(I, filters[i], step=2) recorrelate = False corr = np.max(heatmaps, axis=0) map_out = CMAP(corr)[:,:,:3] * 255 print(np.max(corr)) boxes = [] output = np.zeros(I.shape[:2], dtype=np.float32) for j, i in enumerate(filters_ind): mask = (heatmaps[j] > heat_thresh[i]) * corr output = np.maximum(output, mask) mask_boxes = get_boxes(mask) boxes += min_box_size(mask_boxes, filters[i].shape[0], filters[i].shape[1], I.shape[0], I.shape[1]) I = draw_boxes(I, boxes) left = np.concatenate((I, gray2rgb(output > 0) * I), 0) right = np.concatenate((gray2rgb(corr * 255).astype(np.uint8), map_out.astype(np.uint8)), 0) height_diff = right.shape[0] - compound_filter.shape[0] filter_image = np.pad(compound_filter, [(0,height_diff), (0,0), (0,0)], mode='constant') return np.concatenate((left, right, filter_image), 1)
def denoise(self, img): """Perform image denoising Give: img: noisy image as np array stride: stride in creating sliding windows """ if img.ndim == 2: img = gray2rgb(img) # Flip color channel for input image img = img[:, :, ::-1] img /= float(cfg['SCALE']) assert (len(cfg['IMG_MEAN']) == 1 or len(cfg['IMG_MEAN']) == 3), "Format of image mean is NOT correct!" if len(cfg['IMG_MEAN']) == 1 and float(cfg['IMG_MEAN'][0]) == -1.0: # calculate the mean from image cfg['IMG_MEAN'] = np.array([img[:, :, i].mean() for i in range(3)]) logging.info("Image Mean = {}".format(cfg['IMG_MEAN'])) else: cfg['IMG_MEAN'] = [float(i) for i in cfg['IMG_MEAN']] # if need to crop, first expand the image a boarder of size crop crop = int(cfg['SAMPLING']['CONV_CROP']) if crop > 0: exp_img = np.zeros((img.shape[0] + 2 * crop, img.shape[1] + 2 * crop, 3)) + cfg['IMG_MEAN'] exp_img[crop:-crop, crop:-crop, :] = img img = exp_img self._create_img_blocks(np.copy(img) - cfg['IMG_MEAN']) if cfg['FLATTEN']: self._blocks = self._blocks.reshape(self._block_count, -1) num_batch = int((self._block_count - 1) / cfg['TEST']['MAX_BATCH_SIZE'] + 1) denoised_blocks = [] logging.info("------Start processing blocks------") logging.info("Totally {} batches to process".format(num_batch)) for i in range(num_batch): if Debug: logging.info("Processing batch {}".format(i)) idx_start = i * cfg['TEST']['MAX_BATCH_SIZE'] idx_end = min((i + 1) * cfg['TEST']['MAX_BATCH_SIZE'], self._block_count) blocks = self._blocks[idx_start:idx_end, ...] self._net.blobs[cfg['TEST']['INPUT_BLOB_NAME']].reshape( *(blocks.shape)) self._net.reshape() self._net.blobs[cfg['TEST']['INPUT_BLOB_NAME']].data[ ...] = blocks.astype(np.float, copy=True) self._net.forward() denoised_blocks.append(self._net.blobs[ cfg['TEST']['OUTPUT_BLOB_NAME']].data[...].astype(np.float, copy=True)) if num_batch > 1: denoised_blocks = np.vstack(denoised_blocks) else: denoised_blocks = np.array(denoised_blocks[0]) denoised_img = self.reconstruct_image(denoised_blocks, self._img_shape) return denoised_img
def run(self, img): self._img = gray2rgb(img) votes = [] for x, box in self._iter.run(img): valid, p, prob = self._voter.run(x) if valid: votes.append({'pred': p, 'prob': prob, 'box': box}) self._votes = votes return votes
def show(self, pred=[]): tmp = gray2rgb(self._img) if len(pred) > 0: for pt, p in zip(self._pts, pred): color = self.COLORS[p] cv2.circle(tmp, (pt[1], pt[0]), self.R, color, -1) else: for pt in self._pts: cv2.circle(tmp, (pt[1], pt[0]), self.R, self.COLORS[1], -1) imshow(tmp)
def show(self, lw=4): tmp = gray2rgb(self._img) colors = [(255, 0, 0), (0, 0, 255)] descr = ['text', 'music'] for bbs, color, desc in zip(self._bb, colors, descr): for bb in bbs: cv2.rectangle(tmp, bb[0], bb[1], color, lw) pt = (bb[0][0]+32, bb[0][1]+48) cv2.putText(tmp, desc, pt, cv2.FONT_HERSHEY_PLAIN, 3, color, 8) return tmp
def show(self, lw=4): tmp = gray2rgb(self._img) colors = [(255, 0, 0), (0, 0, 255)] descr = ['text', 'music'] for bbs, color, desc in zip(self._bb, colors, descr): for bb in bbs: cv2.rectangle(tmp, bb[0], bb[1], color, lw) pt = (bb[0][0] + 32, bb[0][1] + 48) cv2.putText(tmp, desc, pt, cv2.FONT_HERSHEY_PLAIN, 3, color, 8) return tmp
def __init__(self, img, colors=((0, 0, 255), (255, 0, 0), (0, 0, 0), ()), r=8, lw=2, off=16): self._colors = colors self._lw = lw self._off = off self._r = r self._lw = lw self._off = off self._img = gray2rgb(img) if len(img.shape) == 1 else img self._tmp = self._img.copy()
def by_name(self, name): img = sk.io.imread(os.path.join(self.path, name), as_gray=self.gray) img = sk.img_as_float(img) if self.resize: img = sk.transform.resize(img, self.resize) if self.gray: w, h = img.shape[0], img.shape[1] img = np.reshape(img, [w, h, 1]) elif len(img.shape) == 2: img = gray2rgb(img) return img
def main(): parser = argparse.ArgumentParser("Denosing image using neural network") parser.add_argument('--cfg', help='Configuration file', required=True) parser.add_argument('--image', help='Image file to denoise', required=True) parser.add_argument('--output_path', help='Output file path', default='./eval/') parser.add_argument('-e', '--evaluate', action='store_true', help="Running evaluation of PSNR", default=False) parser.add_argument( '--sigma', default=25, help="Standard deviation for generating noise during evaluation") parser.add_argument('--noisy_image', help="Noisy image for evaluation") arg = parser.parse_args() if not os.path.exists(arg.output_path): os.mkdir(arg.output_path) denoiser = ImageDenoiser(arg.cfg) img = load_image(arg.image) file_name = os.path.basename(arg.image) file_basename, file_extension = os.path.splitext(file_name) if arg.evaluate: if arg.noisy_image: noisy_img = scipy.misc.imread(arg.noisy_image).astype(np.float) # noisy_img /= 255 else: # add noise and run evaluation of psnr mean = 0 sigma = float(arg.sigma) noise = np.random.normal(mean, sigma, img.shape) noisy_img = img + noise # scipy.misc.imsave( # os.path.join(arg.output_path, file_basename + '_noisy.png'), # np.clip(noisy_img, 0, 255)) else: noisy_img = img # start denoising start = time.time() denoised_image = denoiser.denoise(noisy_img) print("Time cost: {} second".format(time.time() - start)) # Test: using Image to save file denoised_img = Image.fromarray(denoised_image.astype(np.uint8)) denoised_img.save(arg.output_path + file_basename + '_denoised.png', format='PNG') # denoised_img.save( # arg.output_path + file_basename + '_denoised.jpg', # format='JPEG', quality=100 # ) """ scipy.misc.imsave( arg.output_path + file_basename + '_denoised.png', np.clip(denoised_image, 0, 1)) """ print('Denoising Finished!') if arg.evaluate: if img.ndim == 2: img = gray2rgb(img) noisy_img = gray2rgb(noisy_img) color_channel = False else: color_channel = True rmse, psnr = computePSNR(img, denoised_image, cfg, ColorChannel=color_channel) rmse_n, psnr_n = computePSNR(img, noisy_img, cfg, ColorChannel=color_channel) print('%s: RMSE = %.4f / %.4f, PSNR = %.4f / %.4f' % (file_basename, rmse, rmse_n, psnr, psnr_n))
def save_video(filename, video): if video.ndim == 3: video = util.gray2rgb(video) vw = VideoWriter(filename, *(video.shape[:2])) vw.write_chunk(video) vw.close()