def img_offset(img1, img2, cx, cy, scatter): dx = dy = 0 diff = measure.compare_mse(img1, img2) w, h = img1.shape for x in range(cx - scatter, cx + scatter + 1): for y in range(cy - scatter, cy + scatter + 1): i1_x_fr, i2_x_fr = 0, abs(x) i1_x_to, i2_x_to = w - abs(x), w if x < 0: i1_x_fr, i2_x_fr = i2_x_fr, i1_x_fr i1_x_to, i2_x_to = i2_x_to, i1_x_to i1_y_fr, i2_y_fr = 0, abs(y) i1_y_to, i2_y_to = h - abs(y), h if y < 0: i1_y_fr, i2_y_fr = i2_y_fr, i1_y_fr i1_y_to, i2_y_to = i2_y_to, i1_y_to ndiff = measure.compare_mse(img1[i1_x_fr: i1_x_to, i1_y_fr: i1_y_to],\ img2[i2_x_fr: i2_x_to, i2_y_fr: i2_y_to]) if ndiff < diff: diff = ndiff dx, dy = x, y return dx, dy
def test_NRMSE_no_int_overflow(): camf = cam.astype(np.float32) cam_noisyf = cam_noisy.astype(np.float32) assert_almost_equal(compare_mse(cam, cam_noisy), compare_mse(camf, cam_noisyf)) assert_almost_equal(compare_nrmse(cam, cam_noisy), compare_nrmse(camf, cam_noisyf))
def alignforrange(dist, base, first, second): sumF = 162162162.0 sumS = 162162162.0 minNumF = (-dist, -dist) minNumS = (-dist, -dist) for lx in range(-dist, dist + 1): for ly in range(-dist, dist + 1): fir = roll(first, lx, axis = 0) fir = roll(fir, ly, axis = 1) sec = roll(second, lx, axis = 0) sec = roll(sec, ly, axis = 1) alx = abs(lx) aly = abs(ly) fir = fir[: fir.shape[0] - alx, : fir.shape[1] - aly] sec = sec[: sec.shape[0] - alx, : sec.shape[1] - aly] # print(fir.shape, sec.shape) shape = min(fir.shape[0], base.shape[0]) , min(fir.shape[1], base.shape[1]) bas = base[ : shape[0], : shape[1]] fir = fir[ : shape[0]] sF = compare_mse(bas, fir) shape = min(sec.shape[0], base.shape[0]) , min(sec.shape[1], base.shape[1]) bas = base[ : shape[0], : shape[1]] sec = sec[ : shape[0]] sS = compare_mse(bas, sec) if (sumF > sF): minNumF = (lx, ly) sumF = sF #print("fir changed:", sumF, minNumF) if (sumS > sS): minNumS = (lx, ly) sumS = sS return minNumF, minNumS
def evaluate_(self): mse, psnr, ssim = np.zeros([2, self.len]), np.zeros([2, self.len]), np.zeros([2, self.len]) for i in range(self.len): real_A = self.data[i][0].numpy()[0] real_B = self.data[i][1].numpy()[0] fake_A = self.data[i][2].cpu().detach().numpy()[0] from sklearn.metrics import mean_squared_error mse_input = compare_mse(real_B, real_A) mse_output = compare_mse(fake_A, real_A) ssim_input = compare_ssim(real_B[0, :, :], real_A[0, :, :], multichannel=True) ssim_output = compare_ssim(fake_A[0, :, :], real_A[0, :, :], multichannel=True) mse[:, i] = [mse_input, mse_output] ssim[:, i] = [ssim_input, ssim_output] plt.plot([i * 0.2 for i in range(self.len)], mse[0, :], label="Real_Image") plt.plot([i * 0.2 for i in range(self.len)], mse[1, :], label="Gan_Image") plt.title("MSE") # plt.ylim(0, 1) ax = plt.gca() ax.xaxis.set_major_locator(MultipleLocator(0.4)) plt.legend() plt.xlabel("Focus") plt.show() plt.plot([i * 0.2 for i in range(self.len)], ssim[0, :]) plt.plot([i * 0.2 for i in range(self.len)], ssim[1, :]) plt.title("SSIM") plt.show()
def test_NRMSE_no_int_overflow(): camf = cam.astype(np.float32) cam_noisyf = cam_noisy.astype(np.float32) with expected_warnings(['DEPRECATED']): assert_almost_equal(compare_mse(cam, cam_noisy), compare_mse(camf, cam_noisyf)) assert_almost_equal(compare_nrmse(cam, cam_noisy), compare_nrmse(camf, cam_noisyf))
def frame_capture(path): # Get file list sorted by date modified file_list = [ s for s in os.listdir(path) if os.path.isfile(os.path.join(path, s)) ] file_list.sort(key=lambda s: os.path.getmtime(os.path.join(path, s))) for file in file_list: # Path to video file vid_obj = cv2.VideoCapture(os.path.join(path, file)) print(file) # Frame counter count = 0 # Get first frame # success = 1 success, image = vid_obj.read() try: os.mkdir(path + "\\Frames") except FileExistsError: pass while success: # function extract frames success, next_image = vid_obj.read() try: # Comparing frames if compare_mse(next_image, image) > 3: print("Difference Factor : " + str(compare_mse(next_image, image))) # Saves the frames with frame-count cv2.imwrite( path + '\\Frames\\' + file + "_frame%d.jpg" % count, next_image) # # resize image # scale_percent = 20 # percent of original size # width = int(next_image.shape[1] * scale_percent / 100) # height = int(next_image.shape[0] * scale_percent / 100) # dim = (width, height) # cv2.imshow('image', cv2.resize(next_image, dim, interpolation=cv2.INTER_AREA)) # cv2.waitKey(1000) print("Path : " + path + '\\Frames\\' + file + "_frame%d.jpg" % count) print( "--------------------------------------------------------" ) # else: image = next_image # Advance 60 frames = 1 sec count += 60 vid_obj.set(1, count) except AttributeError: print('Next') print( "--------------------------------------------------------") cv2.destroyAllWindows()
async def on_callback_query(self, message): ''' Async method to be called when user clicks on a keyboard item. @param message: dictionary containing information about the message received from user; see https://core.telegram.org/bots/api#message for more information ''' callback_id = message['id'] message_payload = message['data'].split(":") command = message_payload[0] parameter = message_payload[1] chat = await self.administrator.getChat() self._log("callback - " + message['data'], chat) await self.bot.answerCallbackQuery(callback_id, text = 'Fetching data. Please wait.') image_url = "" response_message = "" # if callback was called as a result of user clicking on bus shuttle services keyboard if (command == CALLBACK_COMMAND_BUS): image_url = NTU_WEBSITE + BUS_SERVICES[parameter]["image_url"] response_message = BUS_SERVICES[parameter]["info"] else: # if callback was called as a result of user clicking on locations keyboard location = LOCATIONS[parameter] image_url = CAM_BASE_IMAGE_URL + location + ".jpg?rand=" + str(time.time()) response_message = time.strftime("%a, %d %b %y") + " - <b>" + parameter + "</b>" # if location has a profile (because not all may have a profile) if location in LOCATION_PROFILES: # read the current screenshot from the URL as a file image_file = BytesIO(request.urlopen(image_url).read()) image = misc.imread(image_file) # set default profile to average matching_profile = ("average", measure.compare_mse(image, LOCATION_PROFILES[location]["average"])) profiles = [key for key in LOCATION_PROFILES[location].keys()] if "average" in profiles: profiles.remove("average") # remove this from profiles to be compared with as it is already the default for profile in profiles: # calculate the mse between the profile and the current image mse = measure.compare_mse(image, LOCATION_PROFILES[location][profile]) # set this as the profile with most resemblance if the mse is lower than the current set if mse < matching_profile[1]: matching_profile = (profile, mse) response_message += "\nApproximated crowd density: <b>" + matching_profile[0].upper() + "</b>" await self.sender.sendMessage(response_message, parse_mode='HTML') asyncio.ensure_future(self.sender.sendPhoto(image_url))
def test_images(test_file): basename = os.path.basename(test_file) reference_file = os.path.join(cwd, 'reference',basename) test_data = np.array(Image.open(test_file)) reference_data = np.array(Image.open(reference_file)) msg = "{} does not equal {}".format(test_file, reference_file) assert compare_mse(test_data, reference_data) < 0.01, msg
def assess(model, X_test, Y_test, flatten=False): test_num = X_test.shape[0] ssim_sum = 0 mse_sum = 0 psnr_sum = 0 predict = model.predict(X_test) if flatten: predict = np.reshape(predict, (test_num, 32, 32, 1)) Y_test = np.reshape(Y_test, (test_num, 32, 32, 1)) for j in range(test_num): # original image img1 = np.squeeze(Y_test[j]) img1 = 255 * (img1 + 1) / 2 # reconstruction img2 = np.squeeze(predict[j]) img2 = 255 * (img2 + 1) / 2 # compute SSIM ssim = measure.compare_ssim(img1, img2, data_range=255) ssim_sum = ssim_sum + ssim # compute PSNR psnr = measure.compare_psnr(img1, img2, data_range=255) psnr_sum = psnr_sum + psnr # compute MSE mse = measure.compare_mse(img1, img2) mse_sum = mse_sum + mse ssim_average = ssim_sum / test_num mse_average = mse_sum / test_num psnr_average = psnr_sum / test_num print('\nSSIM:', ssim_average, " MSE: ", mse_average, " PSNR: ", psnr_average) return ssim_average, mse_average, psnr_average
def test_lsqr(self): restored = lsqr_restore(self.low_res, self.A, x0=np.zeros((100, 100)), iter_lim=None, damp=0) im = normalize(self.im, 0, 1).astype(float) restored = normalize(restored, 0, 1).astype(float) mse = compare_mse(im, restored) # Averaged squared error < 0.009. In a scale from 0-255 that's an averaged squared error approx. < 2 DN self.assertLess(mse, 9e-3)
def add_baseline(self, name, predicted, target, frame_nr): self.state['SSIM_%s_val' % name].append(self.ssim(predicted, target)) self.state['SSIM_%s_frame' % name].append(frame_nr) self.state['SSIM_%s_hue' % name].append("SSIM %s" % name.replace('_', ' ').title()) self.state['MSE_%s_val' % name].append(np.sqrt(measure.compare_mse(predicted, target))) self.state['MSE_%s_frame' % name].append(frame_nr) self.state['MSE_%s_hue' % name].append("RMSE %s" % name.replace('_', ' ').title())
def _obtain_current_result(self, step): """ puts in self.current result the current result. also updates the best result :return: """ if step == self.num_iter - 1 or step % 8 == 0: sound1_np = torch_to_np( torch.cat(torch.chunk(self.sound1_out, self.N, dim=0), dim=-1)) sound1_out_np = torch_to_np( torch.cat(torch.chunk(self.sound1, self.N, dim=0), dim=-1)) mask1_out_np = (torch_to_np( torch.cat(torch.chunk(self.mask1_out, self.N, dim=0), dim=-1)) * 255.).astype(np.uint8) im_np = torch_to_np( torch.cat(torch.chunk(self.images_torch[::2], self.N, dim=0), dim=-1)) mse = compare_mse( im_np, torch_to_np( torch.cat(torch.chunk(self.sound1[::2], self.N, dim=0), dim=-1))) self.psnrs.append(mse) self.current_result = SeparationResult(mask1=mask1_out_np[0], sound1=sound1_out_np, sound1_out=sound1_np, mse=mse) if self.best_result is None or self.best_result.mse > self.current_result.mse: self.best_result = self.current_result
def calc_error(orig_img, proc_img, err='PSNR'): if (err == 'PSNR'): return measure.compare_psnr(np.array(orig_img), np.array(proc_img)) elif (err == 'MSE'): return measure.compare_mse(np.array(orig_img), np.array(proc_img)) else: raise ValueError('Error type accepted are: MSE or PSNR')
def save_scores(scores, data, generated, i): A = ((data["image"][0].transpose(0, 2).cpu().detach().numpy()) + 1) * 128 B = ((generated[0].transpose(0, 2).cpu().detach().numpy()) + 1) * 128 scores[i][0] += compare_ssim(A, B, multichannel=True, data_range=256) scores[i][1] += compare_psnr(A, B, data_range=256) scores[i][2] += compare_mse(A, B) return scores
def run_metrics(image_file_name1, image_file_name2): image_name1 = io.imread(image_file_name1) image_name2 = io.imread(image_file_name2) peak_signal_to_noise_ratio = measure.compare_psnr(image_name1, image_name2) print("PSNR Peak signal to noise ratio is %s" % peak_signal_to_noise_ratio) mse = measure.compare_mse(image_name1, image_name2) print("MSE Mean square error between the images is %s" % mse) rmse = measure.compare_nrmse(image_name1, image_name2) print("RMSE Normalised root mean square error between the images is %s" % rmse) ssim = measure.compare_ssim(image_name1, image_name2, multichannel=True) print("SSIM Structural Similarity Index is %s" % ssim) #[M3,M4] = minkowski_distance(image_name1,image_name2) #print ("Minkowski distance is %s %s"%(M3,M4)) #AD = average_difference(image_name1,image_name2) #print ("AD Average difference is %s"%AD) #SC = structural_content(image_name1,image_name2) #print ("SC Structural Content is %s"%SC) #NK = normalised_cross_correlation(image_name1,image_name2) #print ("NK normalised cross correlation is %s"%NK) #MD = maximum_difference(image_name1,image_name2) #print ("Maximum difference is %s"%MD) return { 'peaktonoise': peak_signal_to_noise_ratio, 'mse': mse, 'rmse': rmse, 'ssim': ssim, 'score': peak_signal_to_noise_ratio }
def img_comp(gt, pr, mses=None, nrmses=None, psnrs=None, ssims=None): if ssims is None: ssims = [] if psnrs is None: psnrs = [] if nrmses is None: nrmses = [] if mses is None: mses = [] gt, pr = np.squeeze(gt), np.squeeze(pr) gt = gt.astype(np.float32) if gt.ndim == 2: n = 1 gt = np.reshape(gt, (1, gt.shape[0], gt.shape[1])) pr = np.reshape(pr, (1, pr.shape[0], pr.shape[1])) else: n = np.size(gt, 0) for i in range(n): mses.append( compare_mse(prctile_norm(np.squeeze(gt[i])), prctile_norm(np.squeeze(pr[i])))) nrmses.append( compare_nrmse(prctile_norm(np.squeeze(gt[i])), prctile_norm(np.squeeze(pr[i])))) psnrs.append( compare_psnr(prctile_norm(np.squeeze(gt[i])), prctile_norm(np.squeeze(pr[i])), 1)) ssims.append( compare_ssim(prctile_norm(np.squeeze(gt[i])), prctile_norm(np.squeeze(pr[i])))) return mses, nrmses, psnrs, ssims
def irregularImagePSNR(im_true, im_test, mask, data_range=255): """ Compute the peak signal to noise ratio (PSNR) for an image. Parameters ---------- im_true : ndarray Ground-truth image. im_test : ndarray Test image. data_range : int 255 The data range of the input image mask : (0,1) Binary ndarray Returns ------- psnr : float The PSNR metric. reference: skimage.measure.compare_psnr(), skimage.measure.compare_mse() """ img_true = im_true * mask img_test = im_test * mask total_numb = mask.size #print(total_numb) nonzero_numb = np.count_nonzero(mask) #print(nonzero_numb) tempmes = measure.compare_mse(img_true, img_test) mse = tempmes * total_numb / nonzero_numb psnr = 10 * np.log10((data_range ** 2) / mse) return psnr
def compare_all(image_a, image_b): # peak signal to noise ratio (see https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio) psnr_score = compare_psnr(image_a, image_b) print("PSNR: {}".format(str(psnr_score)), end=" dB\n\n") # mean squared error (see https://en.wikipedia.org/wiki/Mean_squared_error) mse_score = compare_mse(image_a, image_b) print("MSE: {}".format(str(mse_score))) print("Range [0, +INF) where 0 is identical", end="\n\n") # normalized root mean squared error (see https://en.wikipedia.org/wiki/Root-mean-square_deviation) nrmse_score = compare_nrmse(image_a, image_b, norm_type='Euclidean') print("NRMSE: {}".format(str(nrmse_score)), end="\n\n") # structural similarity measure (see https://en.wikipedia.org/wiki/Structural_similarity) ssim_score = compare_ssim(image_a, image_b, full=False, multichannel=True) print("SSIM: {}".format(str(ssim_score))) print("Range [-1, +1] where +1 is identical", end="\n\n") pae_score = pae(image_a, image_b) print("PAE: {}".format(str(pae_score))) print("Range [0, +INF) where 0 is identical", end="\n\n") mae_score = mae(image_a, image_b) print("MAE: {}".format(str(mae_score))) print("Range [0, +INF) where 0 is identical", end="\n\n") return { 'psnr_score': psnr_score, 'mse_score': mse_score, 'nrmse_score': nrmse_score, 'ssim_score': ssim_score, 'mae_score': mae_score, }
def plot_image_diff(noisy, reference, plot_title): """Helper function to display denoising""" difference = noisy - reference mse = compare_mse(reference, noisy) nrmse = compare_nrmse(reference, noisy) psnr = compare_psnr(reference, noisy) subtitle = 'norm: %(norm).4f\nMSE: %(MSE).4f\nNRMSE: %(NRMSE).4f\nPSNR: %(PSNR).4fdB' % { 'norm': np.sqrt(np.sum(difference**2)), 'MSE': mse, 'NRMSE': nrmse, 'PSNR': psnr } print( plot_title + ': norm: %(norm).4f\tMSE: %(MSE).4f\tNRMSE: %(NRMSE).4f\tPSNR: %(PSNR).4fdB' % { 'norm': np.sqrt(np.sum(difference**2)), 'MSE': mse, 'NRMSE': nrmse, 'PSNR': psnr }) plt.gray() plt.subplot(1, 2, 1) plt.title('Noisy') plt.imshow(noisy) plt.xticks(()) plt.yticks(()) plt.subplot(1, 2, 2) plt.title(subtitle) plt.imshow(reference) plt.xticks(()) plt.yticks(())
def MSE(srcpath, dstpath, scale=256): scr = io.imread(srcpath) dst = io.imread(dstpath) scr = transform.resize(scr, (scale, scale)) dst = transform.resize(dst, (scale, scale)) mse = measure.compare_mse(scr, dst) return mse
def img_mse(im1, im2): """Calculates the root mean square error (RSME) between two images""" try: return compare_mse(img_as_float(im1), img_as_float(im2)) except ValueError: print(f'RMS issue, Img1: {im1.size[0]} {im1.size[1]}, Img2: {im2.size[0]} {im2.size[1]}') raise KeyboardInterrupt
def compareImages(self, filename1, filename2): ref_image = io.imread(filename1) ref_image = color.rgb2gray(ref_image) ref_image = resize(ref_image, (123, 274), anti_aliasing=True) image = io.imread(filename2) image = color.rgb2gray(image) image = resize(image, (123, 274), anti_aliasing=True) if (measure.compare_mse(ref_image, image) < 0.05): print("True: {}".format(measure.compare_mse(ref_image, image))) return True else: print("False") print("File1: {}; File2: {}".format(filename1, filename2)) return False
def buildVertPanorama(step=375, delay=10): time.sleep(delay) with mss.mss() as capture: monitor = capture.monitors[1] imgArray = [numpy.array(capture.grab(monitor))] for _ in range(20): mousemove(movex=step, movey=0, delay=0) imgArray.append(numpy.array(capture.grab(monitor))) # stitcher = cv2.createStitcher(False) # result = stitcher.stitch(imgArray) # cv2.imshow('Verticle Stitch test', result) # if cv2.waitKey(25) & 0xFF == ord('q'): # cv2.destroyAllWindows() orig = imgArray[0] for i in imgArray: print("MSE: ", compare_mse(orig, i)) print("ssim: ", compare_ssim(orig, i, multichannel=True)) print("psnr: ", compare_psnr(orig, i)) # cv2.imshow('Verticle Stitch test', i) # cv2.waitKey(0) # cv2.destroyAllWindows() return imgArray #def mean_square_error()
def metrics(X, adv_X, avg=True): imgs = X.shape[0] mse = np.zeros(imgs) ps = np.zeros(imgs) ss = np.zeros(imgs) L_0 = np.zeros(imgs) L_2 = np.zeros(imgs) L_inf = np.zeros(imgs) for i in range(imgs): L_2[i] = np.linalg.norm((X[i, ...] - adv_X[i, ...]).flatten()) L_0[i] = np.linalg.norm((X[i, ...] - adv_X[i, ...]).flatten(), ord=0) L_inf[i] = np.linalg.norm((X[i, ...] - adv_X[i, ...]).flatten(), ord=np.inf) mse[i] = compare_mse(X[i, ...], adv_X[i, ...]) ps[i] = compare_psnr(X[i, ...], adv_X[i, ...], data_range=2) ss[i] = compare_ssim(X[i, ...], adv_X[i, ...], data_range=2, multichannel=True, gaussian_weights=False) if avg: # return np.mean(L_0[L_0>0]), np.mean(L_2[L_2>0]), np.mean(L_inf[L_inf>0]), np.mean(ss[ss<1]), np.mean(mse[mse>0]), np.mean(ps[ps<100]) return L_0.mean(), L_2.mean(), L_inf.mean(), ss.mean(), mse.mean( ), ps.mean() else: return L_0, L_2, L_inf, ss, mse, ps
def add(self, predicted, target, frame_nr, *args): if "Own"in args: spatial_score, scale_score = self.score(predicted, target) self.intermitted.append(spatial_score) self.frame.append(frame_nr) self.hue.append("Spatial") self.intermitted.append(scale_score) self.frame.append(frame_nr) self.hue.append("Scaling") self.own = True if "SSIM" in args: ssim_score = self.ssim(predicted, target) self.state['SSIM_val'].append(ssim_score) self.state['SSIM_frame'].append(frame_nr) self.state['SSIM_hue'].append("SSIM") self.SSIM = True if "RMSE" in args: self.state['MSE_val'].append(np.sqrt(measure.compare_mse(predicted, target))) self.state['MSE_frame'].append(frame_nr) self.state['MSE_hue'].append("RMSE") self.MSE = True if "pHash" in args: self.state['pHash_val'].append(self.pHash(predicted, target, "hamming")) self.state['pHash_frame'].append(frame_nr) self.state['pHash_hue'].append("pHash - hamming") self.phash = True if "pHash2" in args: self.state['pHash2_val'].append(self.pHash(predicted, target, "jaccard")) self.state['pHash2_frame'].append(frame_nr) self.state['pHash2_hue'].append("pHash - jaccard") self.phash2 = True
def metricCompute(uncomp: str, decomp: str, out_dir: str, residual_dir: str, mode="residual", info=None)->None: """ mode: residual: to use residual, else does on raw frames """ if info is None: timestamp = datetime.now().strftime("%Y%m%d-%H%M%S") out_dir = os.path.join(out_dir, timestamp) else: out_dir = os.path.join(out_dir, info) if not os.path.exists(out_dir): os.makedirs(out_dir) print("Making output directory") filePath = os.path.join(out_dir, 'log.txt') file = open(filePath, 'w+') file.write("Video SSIM and MSE\n") # file.write("Quality: %d" %(quality)) #pdb.set_trace() uncomp_frames = sorted(glob(os.path.join(uncomp, '*.png'))) decomp_frames = sorted(glob(os.path.join(decomp, '*.png'))) residual_frames = None if mode=='residual': residual_frames = sorted(glob(os.path.join(residual_dir, '*.npy'))) # incase different things for each were used limit = min(len(uncomp_frames), len(decomp_frames), len(residual_frames)) else: limit = min(len(uncomp_frames), len(decomp_frames)) residual_im = 0 saveName = os.path.join(out_dir, "Frame") avg_ssim = 0 avg_mse = 0 for i in range(limit): uncomp_im = cv2.imread(uncomp_frames[i]) decomp_im = cv2.imread(decomp_frames[i]) if mode == 'residual': #pdb.set_trace() residual_im = np.load(residual_frames[i]) * 255.0 residual_im = np.transpose(residual_im.astype(np.int32), axes=(1,2,0)) rec_im = np.clip(decomp_im + residual_im, 0, 255) ssim_value = compare_ssim(uncomp_im, rec_im, multichannel=True, full=False, gradient=False) mse_value = compare_mse(uncomp_im, rec_im) avg_ssim += ssim_value avg_mse += ssim_value if mode == 'residual': cv2.imwrite(saveName + str(i) + ".png", rec_im) file.write("Frame {}: SSIM {} , MSE {} \n".format(i, ssim_value, mse_value)) file.write("Average: SSIM {} , MSE {} \n".format(avg_ssim/limit, avg_mse/limit)) file.close()
def test(): set_gpu(2) cfig = ConfigFactory('srcnn') image = cv.imread('./baby_GT.bmp') image = modcrop(image, cfig.scale_factor) image = cv.cvtColor(image, cv.COLOR_BGR2YCrCb) (hei, wid, cha) = image.shape im_label = image[:, :, 0] im_input = cv.resize(im_label, (0, 0), fx=1.0 / cfig.scale_factor, fy=1.0 / cfig.scale_factor, interpolation=cv.INTER_CUBIC) im_input = cv.resize(im_input, (0, 0), fx=cfig.scale_factor, fy=cfig.scale_factor, interpolation=cv.INTER_CUBIC).astype(np.float32) # input and ground truth of network im_Y_label = im_label.reshape((1, hei, wid, 1)).astype(np.float32) / 255.0 im_Y_input = im_input.reshape((1, hei, wid, 1)).astype(np.float32) / 255.0 # inference inference = srcnn(im_Y_input, padding='SAME', name='srcnn') loss = tf.losses.mean_squared_error(im_Y_label, inference) # start session init = tf.global_variables_initializer() sess = tf.InteractiveSession() sess.run(init) saver = tf.train.Saver(max_to_keep=cfig.max_ckpt_keep) ckpt = tf.train.get_checkpoint_state(cfig.ckpt_router) if ckpt and ckpt.model_checkpoint_path: print('load model', ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) test_loss, test_inference = sess.run([loss, inference]) test_inference[test_inference > 1.0] = 1.0 test_inference[test_inference < 0.0] = 0.0 test_inference = test_inference * 255.0 # test_inference and label should be float32 or compute diff will overflow test_inference = test_inference.reshape( (test_inference.shape[1], test_inference.shape[2])).astype(dtype=np.uint8) ori_modcrop_image = cv.cvtColor(image, cv.COLOR_YCrCb2BGR) cv.imwrite('./test_save/ori_modcrop.bmp', ori_modcrop_image) image[:, :, 0] = test_inference sr = cv.cvtColor(image, cv.COLOR_YCrCb2BGR) cv.imwrite('./test_save/srcnn_rescon.bmp', sr) test_inference = test_inference.astype(np.float32) im_Y_label = im_Y_label[0, :, :, 0] * 255. err = compare_mse(im_Y_label, test_inference) psnr_metric = 10 * np.log10((255**2) / err) print(psnr_metric)
def get_scores(gt, x, multichan=False): gt_, x_ = norm_minmse(gt, x) mse = compare_mse(gt_, x_) psnr = compare_psnr(gt_, x_, data_range=1.) ssim = compare_ssim(gt_, x_, data_range=1., multichannel=multichan) return np.sqrt(mse), psnr, ssim
def calc_quanti(im1, im2): # im1 = (to_numpy(im1) * 1)#.astype(np.uint8) # im2 = (to_numpy(im2) * 1)#.astype(np.uint8) mse = compare_mse(im1, im2) ssim = compare_ssim(im1, im2, multichannel=True, data_range=im2.max() - im2.min()) psnr = compare_psnr(im2, im1, data_range=im2.max() - im2.min()) return mse, psnr, ssim
def print_vector_comparison(X, Y, comparison_name=None): difference = X - Y norm = np.sqrt(np.sum(difference ** 2)) mse = compare_mse(X, Y) nrmse = compare_nrmse(X, Y) cos = cosine_similarity(X, Y) if comparison_name is not None: printing_text = comparison_name + ':Norm:%(NORM).4f\tMSE:%(MSE).4f\tNRMSE:%(NRMSE).4f\tCos:%(COS).4f' % {'NORM': norm, 'MSE': mse, 'NRMSE': nrmse, 'COS': cos} print(printing_text) return norm, mse, nrmse, cos
def get_denoise_metrics(input, output, report): image_file_name1 = input image_file_name2 = output image_name1 = io.imread(image_file_name1) image_name2 = io.imread(image_file_name2) # estimate the standard deiviation of the images std_1 = numpy.std(numpy.std(numpy.array(image_name1))) std_2 = numpy.std(numpy.std(numpy.array(image_name2))) print("std is %2.10f" % std_1) # print ("Standard deviation of the images are"%(std_1,std_2)) # estimate the peak signal to noise ratio (PSNR) between the image peak_signal_to_noise_ratio = measure.compare_psnr(image_name1, image_name2) print("Peak signal to noise ratio is %s" % peak_signal_to_noise_ratio) # estimate the mean square error between the images mse = measure.compare_mse(image_name1, image_name2) print("Mean square error between the images is %s" % mse) # estimate the normalised root mean square error between the images rmse = measure.compare_nrmse(image_name1, image_name2) print("Normalised root mean square error between the images is %s" % rmse) resp = open(report, 'w') resp.write("std1 is %2.10f \n" % std_1) resp.write("std2 is %2.10f \n" % std_2) resp.write( "Peak signal to noise ratio is %s \n" % peak_signal_to_noise_ratio) resp.write("Mean square error between the images is %s \n" % mse) resp.write( "Normalised root mean squre error between the images is %s \n" % rmse) resp.close()
def run_metrics(image_file_name1,image_file_name2 ): image_name1 = io.imread(image_file_name1) image_name2 = io.imread(image_file_name2) peak_signal_to_noise_ratio = measure.compare_psnr (image_name1,image_name2) print ("PSNR Peak signal to noise ratio is %s"%peak_signal_to_noise_ratio) mse = measure.compare_mse(image_name1,image_name2) print ("MSE Mean square error between the images is %s"%mse) rmse = measure.compare_nrmse(image_name1,image_name2) print ("RMSE Normalised root mean square error between the images is %s"%rmse) ssim = measure.compare_ssim(image_name1,image_name2, multichannel=True) print ("SSIM Structural Similarity Index is %s"%ssim) #[M3,M4] = minkowski_distance(image_name1,image_name2) #print ("Minkowski distance is %s %s"%(M3,M4)) #AD = average_difference(image_name1,image_name2) #print ("AD Average difference is %s"%AD) #SC = structural_content(image_name1,image_name2) #print ("SC Structural Content is %s"%SC) #NK = normalised_cross_correlation(image_name1,image_name2) #print ("NK normalised cross correlation is %s"%NK) #MD = maximum_difference(image_name1,image_name2) #print ("Maximum difference is %s"%MD) return {'peaktonoise':peak_signal_to_noise_ratio ,'mse': mse, 'rmse': rmse, 'ssim':ssim,'score':peak_signal_to_noise_ratio}
def recognize(img, digit_templates): #draw(img, "Initial image", 4, 3, 1) corrected_img = intensity_correction(img) #draw(corrected_img, "Corrected image", 4, 3, 4) binary_img, binary_mask = get_binary(corrected_img) labeled_img = measure.label(binary_img, background=0) #draw(labeled_img, "labeled", 4, 3, 12) props = measure.regionprops(labeled_img) #print(len(props)) main_rect = measure.regionprops(binary_mask) main_rect = main_rect[0].bbox #print("main rect = ", main_rect) k = 0.6 center = main_rect[3]*k + (1-k)*main_rect[1] #print(center) final = np.zeros(labeled_img.shape) regions = np.zeros((0, 3)) for i in range(0, len(props)): left = props[i].bbox[1] height = props[i].bbox[2] - props[i].bbox[0] width = props[i].bbox[3] - props[i].bbox[1] if height > width and left < center and height < 3*width: regions = np.append(regions, [[height, props[i].bbox[1], i]], axis=0) #final[props[i].bbox[0]:props[i].bbox[2], props[i].bbox[1]:props[i].bbox[3]] = 1. regions = regions[np.argsort(regions[:,0])] regions[::-1] = regions[:] #print(regions) if len(regions) > 3: regions = regions[:4] regions = regions[np.argsort(regions[:,1])] #additional check average_top_first = (props[int(regions[0][2])].bbox[0]+props[int(regions[1][2])].bbox[0]+props[int(regions[2][2])].bbox[0]) / 3 last_disp = abs(average_top_first - props[int(regions[3][2])].bbox[0]) average_top_last = (props[int(regions[1][2])].bbox[0]+props[int(regions[2][2])].bbox[0]+props[int(regions[3][2])].bbox[0]) / 3 first_disp = abs(average_top_last - props[int(regions[0][2])].bbox[0]) #print(abs(first_disp - last_disp)) if abs(first_disp - last_disp) > 1: if (first_disp >= last_disp): regions = regions[1:] else: regions = regions[:-1] else: #print(regions) regions = regions[np.argsort(regions[:,0])] regions[::-1] = regions[:] regions = regions[:3] regions = regions[np.argsort(regions[:,1])] #print(regions) plt.clf() #draw(labeled_img, "labeled", 3, 2, 2) #draw(corrected_img, "corrected", 3, 2, 1) result = [0, 0, 0] for i in range(0, len(regions)): digit_mask = props[int(regions[i][2])].image #draw(digit_mask, "digit", 3, 3, 4 + i) bbox = props[int(regions[i][2])].bbox digit_img = corrected_img[bbox[0]:bbox[2], bbox[1]:bbox[3]] digit_mask = 1. - digit_mask.astype(np.float) coef = .4 digit_img = (1 - coef) * digit_img + coef * digit_mask index = 0 min_mse = 0 for j in range(0, 10): mse = measure.compare_mse(digit_img, transform.resize(digit_templates[j], digit_img.shape)) if j == 0: min_mse = mse elif min_mse > mse: min_mse = mse index = j #draw(digit_img, str(index), 3, 3, 7 + i) result[i] = index #figManager = plt.get_current_fig_manager() #figManager.window.showMaximized() #plt.show() return (result[0], result[1], result[2])
def quadratique(img1, img2): if img1.shape == img2.shape: return measure.compare_mse(img1, img2) else: print("Les images ne sont pas de la même taille")
print image_name2.shape #estimate the standard deiviation of the images std_1 = numpy.std (numpy.std (numpy.array(image_name1))) std_2 = numpy.std (numpy.std (numpy.array(image_name2))) print ("std is %2.10f"%std_1) #print ("Standard deviation of the images are"%(std_1,std_2)) #estimate the peak signal to noise ratio (PSNR) between the image peak_signal_to_noise_ratio = measure.compare_psnr (image_name1,image_name2) print ("Peak signal to noise ratio is %s"%peak_signal_to_noise_ratio) # estimate the mean square error between the images mse = measure.compare_mse(image_name1,image_name2) print ("Mean square error between the images is %s"%mse) # estimate the normalised root mean square error between the images rmse = measure.compare_nrmse(image_name1,image_name2) ssim = measure.compare_ssim(image_name1,image_name2) print ("Normalised root mean squre error between the images is %s"%rmse) print ("SSIM is %s"%ssim)
def main(): image_base_dir = '/home/dek/makerfaire-booth/2018/burger/experimental/dek/train_object_detector/decoded' canonical_dir = 'canonical' # template = os.path.join(image_base_dir, 'bottombun.0.00.27.34.-24.61.0.81.png') fig, axes = plt.subplots(7, 7, figsize=(7, 6), sharex=True, sharey=True) fig.delaxes(axes[0][0]) ssims = numpy.zeros( (len(BurgerElement.__members__), len(BurgerElement.__members__)), dtype=float) mses = numpy.zeros( (len(BurgerElement.__members__), len(BurgerElement.__members__)), dtype=float) for i, layer in enumerate(BurgerElement.__members__): template = os.path.join(canonical_dir, '%s.png' % layer) img1 = imread(template) # img1_padded = numpy.zeros( (WIDTH, HEIGHT,3), dtype=numpy.uint8) img1_padded = numpy.resize( [255,255,255], (WIDTH, HEIGHT, 3)) s = img1.shape w = s[0] h = s[1] nb = img1_padded.shape[0] na = img1.shape[0] lower1 = (nb) // 2 - (na // 2) upper1 = (nb // 2) + (na // 2) nb = img1_padded.shape[1] na = img1.shape[1] lower2 = (nb) // 2 - (na // 2) upper2 = (nb // 2) + (na // 2) img1_padded[lower1:upper1, lower2:upper2] = img1 img1_padded_float = img1_padded.astype(numpy.float64)/255. print img1_padded_float.shape img1_gray = rgb2gray(img1_padded_float) descriptor_extractor = ORB() try: descriptor_extractor.detect_and_extract(img1_gray) except RuntimeError: continue keypoints1 = descriptor_extractor.keypoints descriptors1 = descriptor_extractor.descriptors axes[i][0].imshow(img1_padded_float) axes[i][0].set_title("Template image") for j, layer2 in enumerate(BurgerElement.__members__): rot, tx, ty, scale = get_random_orientation() img2 = draw_example(layer2, WIDTH, HEIGHT, rot, tx, ty, scale) # match = os.path.join(canonical_dir, '%s.png' % layer2) # img2 = imread(match) img2_padded = numpy.resize( [255,255,255], (WIDTH, HEIGHT, 3)) s = img2.shape img2_padded[:s[0], :s[1]] = img2 img2_padded_float = img2_padded.astype(numpy.float64)/255. img2_gray = rgb2gray(img2_padded_float) try: descriptor_extractor.detect_and_extract(img2_gray) except RuntimeError: continue keypoints2 = descriptor_extractor.keypoints descriptors2 = descriptor_extractor.descriptors matches12 = match_descriptors(descriptors1, descriptors2, cross_check=True) src = keypoints2[matches12[:, 1]][:, ::-1] dst = keypoints1[matches12[:, 0]][:, ::-1] model_robust, inliers = \ ransac((src, dst), SimilarityTransform, min_samples=4, residual_threshold=2) if not model_robust: print "bad" continue img2_transformed = transform.warp(img2_padded_float, model_robust.inverse, mode='constant', cval=1) sub = img2_transformed - img1_padded_float ssim = compare_ssim(img2_transformed, img1_padded_float, win_size=5, multichannel=True) mse = compare_mse(img2_transformed, img1_padded_float) ssims[i,j] = ssim mses[i,j] = mse axes[0][j].imshow(img2_padded_float) axes[0][j].set_title("Match image") axes[i][j].imshow(img2_transformed) axes[i][j].set_title("Transformed image") axes[i][j].set_xlabel("SSIM: %9.4f MSE: %9.4f" % (ssim, mse)) # ax = plt.gca() # plot_matches(ax, img1, img2, keypoints1, keypoints2, matches12) print ssims print numpy.argmax(ssims, axis=1) print numpy.argmin(mses, axis=1) plt.show()