def evaluation(root_dir): mse_total = 0 psnr_total = 0 fmse_total = 0 count = 0 for comp_name in os.listdir(root_dir): comp = Image.open(comp_name) comp = comp.resize(IMAGE_SIZE, Image.BICUBIC) comp = np.array(comp, dtype=np.float32) real = Image.open(real_name(comp_name)) real = real.resize(IMAGE_SIZE, Image.BICUBIC) real = np.array(real, dtype=np.float32) mask = Image.open(mask_name(comp_name)) mask = mask.convert('1') mask = mask.resize(IMAGE_SIZE, Image.BICUBIC) mask = np.array(mask, dtype=np.uint8) fore_area = np.sum(np.sum(mask, axis=0), axis=0) mask = mask[..., np.newaxis] mse_total += mse(comp, real) psnr_total += psnr(real, comp, data_range=comp.max() - comp.min()) fmse_total += mse(comp * mask, real * mask) * 256 * 256 / fore_area count += 1 print( "%s MSE %0.2f | PSNR %0.2f | fMSE %0.2f" % (comp_name, mse_total / count, psnr_total / count, fmse_total / count))
def run_evaluation_metrics(): # run_replace() # run_eval() a_org = cv2.imread('eval_replace_ground.jpg') a_rep = cv2.imread('eval_replace_out_aer.jpg') a_dic = cv2.imread('eval_dict.jpg') a_gan = cv2.imread('eval_gan_out_ground.jpg') a_mask = cv2.imread('eval_replace_out_cld.jpg') a_mask[a_mask > 1] = 1 from skimage.measure import compare_psnr as psnr from skimage.measure import compare_mse as mse from skimage.measure import compare_ssim as ssim for a in (a_rep, a_dic, a_gan): # ssim print("| PSNR %e MSE %e SSIM: %e" % (psnr(a_org, a), mse(a_org, a), ssim(a_org, a, multichannel=True))) # a_mask = cv2.imread('eval_replace_out_cld.jpg') # print(np.sum(a_mask // 255), a_mask.shape[0] * a_mask.shape[1]) # 11205 262144 """ | PSNR 1.805008e+01 MSE 1.018760e+03 SSIM: 8.988292e-01 | PSNR 2.858750e+01 MSE 9.001815e+01 SSIM: 9.069278e-01 | PSNR 3.486142e+01 MSE 2.122945e+01 SSIM: 9.710586e-01 """ # mask_num = np.sum(a_mask) # print("| mask num:", mask_num) # for a in (a_rep, a_dic, a_gan): # EER # print("|", np.sum((a_org / 255.0 - a / 255.0) ** 2 / (a_org / 255.0)) / mask_num) """
def get_metrics(images, metric): height, width = images.shape[0], images.shape[1] width_cutoff = width // 2 s1 = images[..., :width_cutoff, :] s2 = images[..., width_cutoff:, :] s1[s1 == np.nan] = 0 s2[s2 == np.nan] = 0 sum = 0 s1 = np.expand_dims(s1, 0) s2 = np.expand_dims(s2, 0) for left, right in zip(s1, s2): if (metric == "ssim"): sum += ssim(left, right, data_range=right.max() - right.min(), multichannel=True) elif (metric == "psnr"): sum += psnr(left, right) elif (metric == "mse"): sum += mse(left, right) else: print("Metric not recognized") exit() return sum
def run_eval_metrics(): from skimage.measure import compare_psnr as psnr from skimage.measure import compare_mse as mse from skimage.measure import compare_ssim as ssim grounds = np.load('grounds.npy') for dir in ('eval_low_rank', 'eval_dict', 'eval_two_stage1', 'eval_ground'): eval_matrics = list() for i, ground in enumerate(grounds): if i == 3 or 8 <= i <= 10: continue # ground = cv2.imread('%s/%02d.jpg' % ('eval_ground', i)) result = cv2.imread('%s/%02d.jpg' % (dir, i)) # print("| PSNR %e MSE %e SSIM: %e" % # (psnr(ground, result), # mse(ground, result), # ssim(ground, result, multichannel=True))) eval_matrics.append(( ssim(ground, result, multichannel=True), psnr(ground, result), mse(ground, result), np.mean((ground - result) ** 2), )) eval_matrics = np.array(eval_matrics) np.save('%s/eval.npy' % dir, eval_matrics) for dir in ('eval_low_rank', 'eval_dict', 'eval_two_stage1', 'eval_ground'): print("%16s SSIM PSNR MSE L2:" % dir, np.average(np.load('%s/eval.npy' % dir), axis=0))
def compare_images(imageA, imageB, title): height, width = imageA.shape imageB = cv2.resize(imageB, (width, height), cv2.INTER_LINEAR) # compute the mean squared error and structural similarity # index for the images m = mse(imageA, imageB) s = ssim(imageA, imageB) # setup the figure fig = plt.figure(title) plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s)) # show first image ax = fig.add_subplot(1, 2, 1) plt.imshow(imageA, cmap=plt.cm.gray) plt.axis("off") # show the second image ax = fig.add_subplot(1, 2, 2) plt.imshow(imageB, cmap=plt.cm.gray) plt.axis("off") # show the images plt.show()
def find_angle(original, canvas, color, where, brush_stroke_length_min, brush_stroke_length_max, brush_stroke_length_step, brush_stroke_width, min_angle=0, max_angle=180, step=12): x, y = where r = brush_stroke_length_max o = original.crop((x - r, y - r, x + r, y + r)) can = canvas.crop((x - r, y - r, x + r, y + r)) lowest_error = None best_angle = None best_length = None for brush_stroke_length in range(brush_stroke_length_min, brush_stroke_length_max, brush_stroke_length_step): for a in range(min_angle, max_angle, step): c = can.copy() d = ImageDraw.Draw(c) brushstroke(d, (r, r), a, color, brush_stroke_length, brush_stroke_width) del d error = mse(o, c) del c if lowest_error is None or error < lowest_error: lowest_error = error best_angle = a best_length = brush_stroke_length return best_angle, best_length
def common_train(model, img_rows, img_cols, BATCH_SIZE): val_noise = np.concatenate([ val_file[:, 0:5], np.zeros((val_file.shape[0], 1, img_rows, img_cols, 1)) ], axis=1) image2 = val_file[0, 5, :, :, 0] * 255 for epoch in range(total_epoch): test_gen_img = model.predict(val_noise, verbose=0) image1 = test_gen_img[0] * 255 saveimages(image2, image1[5, :, :, 0], COMPARE_DIR) for i in range(7): input_img = np.concatenate([ train_file[:, i:5 + i], np.zeros((train_file.shape[0], 1, img_rows, img_cols, 1)) ], axis=1) loss = model.fit(input_img, train_file[:, i:6 + i], batch_size=BATCH_SIZE, epochs=1, verbose=0) image1 = image1 / 255 image2 = image2 / 255 mse_s = mse(image2, image1[5, :, :, 0]) ssim_s = compute_mssim(image2, image1[5, :, :, 0]) print("[epoch %s][loss : %f] [MSE : %f] [SSIM : %f]" % (epoch, loss.history['loss'][0], mse_s, ssim_s)) model.save_weights(WEIGHTS_DIR + str(epoch) + '.h5')
def compare_image(url): grayA = cv2.imread( '/home/korbit-users/phishing_scripts/phishing_tmp2/main.png', cv2.IMREAD_GRAYSCALE) grayB = cv2.imread( '/home/korbit-users/phishing_scripts/phishing_tmp2/tmp.png', cv2.IMREAD_GRAYSCALE) grayC = cv2.imread( '/home/korbit-users/phishing_scripts/phishing_tmp2/login.png', cv2.IMREAD_GRAYSCALE) score = ssim(grayA, grayB) m = mse(grayA, grayB) score2 = ssim(grayC, grayB) m2 = mse(grayC, grayB) url = urllib.parse.unquote(url) ttt = 'https://portal.korbit.co.kr' tt = 'https://portal.korbit.co.kr/login' if (score * 100 >= 95.5 and m <= 300): # print(url) # print('score:'+str(score*100)) # print('m:'+str(m)) message = '*phishing site detected : *' + url + ' (' + str( round((score * 100), 2)) + '%)' payload = {'text': message} requests.post(slack_url, json=payload) if (ne(str(url), ttt)): shutil.copyfile( '/home/korbit-users/phishing_scripts/phishing_tmp2/tmp.png', '/home/korbit-users/phishing_scripts/phishing_tmp2/' + str(score) + '.png') if (score2 * 100 >= 95.5 and m2 <= 300): # print(url) # print('score2:'+str(score2*100)) # print('m2:'+str(m2)) message = '*phishing site detected : *' + url + ' (' + str( round((score2 * 100), 2)) + '%)' payload = {'text': message} requests.post(slack_url, json=payload) if (ne(str(url), tt)): shutil.copyfile( '/home/korbit-users/phishing_scripts/phishing_tmp2/tmp.png', '/home/korbit-users/phishing_scripts/phishing_tmp2/' + str(score2) + '.png')
def calc_mse(gt, img): """ Calculate mean squared error @param gt: ground truth @param: img: input image """ mse_const = mse(gt, img) return mse_const
def compare_images_silent(imageA, imageB): imageA = cv2.cvtColor(imageA, cv2.COLOR_RGB2GRAY) imageB = cv2.cvtColor(imageB, cv2.COLOR_RGB2GRAY) m = mse(imageA, imageB) s = ssim(imageA, imageB) p = psnr(imageA, imageB) print("PSNR: %.2f MSE: %.2f SSIM: %.2f" % (p, m, s)) return m
def get_scores(img1, img2, method="ssim"): if method == "ssim": return ssim(img1, img2) elif method == ["mse"]: return mse(img1, img2) elif method == "nrmse": return nrmse(img1, img2) return None
def compute_image_quality_metrics(ground_truth_images, ground_truth_angles, generated_images, generated_angles): # order the images according to ascending angle ground_truth_images = ground_truth_images[np.argsort(ground_truth_angles[:, 0], 0)] generated_images = generated_images[np.argsort(generated_angles[:, 0], 0)] loop_mse, loop_nrmse, loop_ssim = [], [], [] for (im_gt, im_gen) in zip(ground_truth_images, generated_images): loop_mse.append(mse(im_gt, im_gen)) loop_nrmse.append(nrmse(im_gt, im_gen)) loop_ssim.append(ssim(im_gt.squeeze(), im_gen.squeeze())) return np.array(loop_mse).mean(), np.array(loop_nrmse).mean(), np.array(loop_ssim).mean()
def computeSimilarity(imgA, imgB): for item in imgA: if item == [255, 255, 255]: ind = imgA.index(item) print(ind) del imgA[ind] del imgB[ind] imageA = np.array(imgA) imageB = np.array(imgB) return mse(imageA, imageB)
def do_all_metrics(comparison_dict): pairs_lvl0 = [k for k in comparison_dict.iterkeys()] for p in pairs_lvl0: data_keys = [j for j in comparison_dict[p].iterkeys()] regex = re.compile('2') snow = [string for string in data_keys if re.match(regex, string)] comparison_dict[p]['MSE'] = round( mse(comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]]), 3) comparison_dict[p]['SSIM'] = round( ssim(comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]]), 3) comparison_dict[p]['MSE Map'] = (comparison_dict[p][data_keys[0]] - comparison_dict[p][data_keys[1]])**2 comparison_dict[p]['SSIM Map'] = ssim(comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]], full=True)[1] comparison_dict[p]['CW-SSIM'] = cw_ssim( comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]], 40)[0] comparison_dict[p]['CW-SSIM Map'] = cw_ssim( comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]], 40)[1] comparison_dict[p]['GMS'] = gmsd(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[0] comparison_dict[p]['GMS Map'] = gmsd(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[1] comparison_dict[p][snow[0] + ' DCT Map'] = discrete_cosine( comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[0] comparison_dict[p][snow[1] + ' DCT Map'] = discrete_cosine( comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[1] comparison_dict[p][snow[0] + ' DCT Curve'] = discrete_cosine( comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[2] comparison_dict[p][snow[1] + ' DCT Curve'] = discrete_cosine( comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[3] comparison_dict[p]['FSIM'] = feature_sim(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])
def do_all_metrics(comparison_dict): pairs_lvl0 = [k for k in comparison_dict.iterkeys()] for p in pairs_lvl0: data_keys = [j for j in comparison_dict[p].iterkeys()] regex = re.compile('2') snow = [string for string in data_keys if re.match(regex, string)] comparison_dict[p]['MSE'] = round(mse(comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]]),3) comparison_dict[p]['SSIM'] = round(ssim(comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]]),3) comparison_dict[p]['MSE Map'] = (comparison_dict[p][data_keys[0]] - comparison_dict[p][data_keys[1]])**2 comparison_dict[p]['SSIM Map'] = ssim(comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]], full = True)[1] comparison_dict[p]['CW-SSIM'] = cw_ssim(comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]], 40)[0] comparison_dict[p]['CW-SSIM Map'] = cw_ssim(comparison_dict[p][data_keys[0]], comparison_dict[p][data_keys[1]], 40)[1] comparison_dict[p]['GMS'] = gmsd(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[0] comparison_dict[p]['GMS Map'] = gmsd(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[1] comparison_dict[p][snow[0]+' DCT Map'] = discrete_cosine(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[0] comparison_dict[p][snow[1]+' DCT Map'] = discrete_cosine(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[1] comparison_dict[p][snow[0]+' DCT Curve'] = discrete_cosine(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[2] comparison_dict[p][snow[1]+' DCT Curve'] = discrete_cosine(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])[3] comparison_dict[p]['FSIM'] = feature_sim(comparison_dict[p][snow[0]], comparison_dict[p][snow[1]])
def compare(original, restored): """ Side by side comparison of the original image and reconstruction effort with MSE, PSNR, and SSIM labels """ if original.dtype != restored.dtype: warnings.warn( "The images are different data types. Converting both images to floats within 0-1 range" ) original = normalize(original.astype(float), 0, 1) restored = normalize(restored.astype(float), 0, 1) fig, axes = plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True, subplot_kw={'adjustable': 'box-forced'}) ax = axes.ravel() mse_original = mse(original, original) psnr_original = psnr(original, original) ssim_original = ssim(original, original) mse_restored = mse(original, restored) psnr_restored = psnr(original, restored) ssim_restored = ssim(original, restored) label = 'MSE: {:.2f}, PSNR: {:.2F}, SSIM: {:.2f}' ax[0].imshow(original, cmap='gray', vmin=0, vmax=1) ax[0].set_xlabel(label.format(mse_original, psnr_original, ssim_original)) ax[0].set_title('Original image') ax[1].imshow(restored, cmap='gray', vmin=0, vmax=1) ax[1].set_xlabel(label.format(mse_restored, psnr_restored, ssim_restored)) ax[1].set_title('Restored image') plt.tight_layout() plt.show()
def quant(res, org): res = res[:, :, 0] org = org[:, :, 0] #print(img1) '''mse = np.mean( (img1 - img2) ** 2 ) #print(img1.shape) if mse == 0: return 0, 100 PIXEL_MAX = 255.0 return mse, 20 * math.log10(PIXEL_MAX / math.sqrt(mse)), ss ''' ss = (1 + ssim(res, org, data_range=res.max() - res.min())) / 2 ms = mse(res, org) ps = psnr(res, org) return ms, ps, ss
def computeSimilarity(self, imgA, imgB): instanceImgA = [] instanceImgB = [] instanceImgA[:] = imgA instanceImgB[:] = imgB for item in instanceImgA: if item == [255, 255, 255]: ind = instanceImgA.index(item) del instanceImgA[ind] del instanceImgB[ind] imageA = np.array(instanceImgA) imageB = np.array(instanceImgB) return mse(imageA, imageB)
def run_metrics(images): """ Test the given collection of image pairs with a set of measures and return the results """ mse_results = list() ssim_results = list() for ((image_a_name, image_a), (image_b_name, image_b)) in images: mse_results.append(mse(image_a, image_b)) ssim_results.append( ssim(image_a, image_b, multichannel=True, data_range=image_a.max() - image_b.min())) return list(zip(mse_results, ssim_results))
def show_with_diff(image, reference, title): # The image textSize = 22 difference = image - reference image_seY = np.sqrt(np.sum(difference**2)) image_mseY = mse(image, reference) image_psnrY = psnr(reference, image) image_ssimY = ssim(image, reference) plt.figure(figsize=(20, 10)) plt.subplot(1, 3, 1) plt.title('Face Original\n', size=textSize, fontweight="bold") plt.imshow(reference, vmin=0, vmax=1, cmap=plt.cm.gray, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.subplot(1, 3, 2) plt.title( 'Difference \n$\ell_2: {0:.2f}, MSE: {1:.4f}, PSNR: {2:.2f}, SSIM: {3:.2f}$' .format(image_seY, image_mseY, image_psnrY, image_ssimY), size=textSize, fontweight="bold") plt.imshow(difference, vmin=-0.5, vmax=0.5, cmap=plt.cm.PuOr, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.subplot(1, 3, 3) if (title == 'Noise filled image'): plt.title('Face with Gaussian Noise\n$\sigma^{2}=0.005$', size=textSize, fontweight="bold") else: plt.title('Face after Cloud K-SVD\n', size=textSize, fontweight="bold") plt.imshow(image, vmin=0, vmax=1, cmap=plt.cm.gray, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.savefig('results\\' + timestr + '\\' + title + '.pdf')
def main(): img = cv2.imread(INVENTORY_IMAGE_PATH, cv2.IMREAD_COLOR) item_list = build_item_list(DATA_DIR) # Canny params thresh = 1000 thresh_2 = thresh/3 elipse = (9, 9) # Sim measure to use sim_type = 'mse' print("extracting coords") for x in range(GRID_COORDS[0][0], GRID_COORDS[1][0], GRID_SQUARE_SIZE_PX): for y in range(GRID_COORDS[0][1], GRID_COORDS[2][1], GRID_SQUARE_SIZE_PX): item_image = img[y:y + GRID_SQUARE_SIZE_PX, x:x + GRID_SQUARE_SIZE_PX] item = extract_item(item_image, thresh, thresh_2, elipse) cv2.imshow("extraction", np.hstack([item_image, item])) cv2.waitKey(0) best_sim = None best_sim_name = None best_sim_image = None for icon_name, icon_image in item_list: ssim_sim = ssim(item, icon_image, multichannel=True) mse_sim = mse(item, icon_image) if sim_type == 'mse': sim = np.round(mse_sim, 2) is_best_sim = not best_sim or sim < best_sim else: sim = np.round(ssim_sim, 2) is_best_sim = not best_sim or sim < best_sim if is_best_sim: best_sim = sim best_sim_name = icon_name best_sim_image = icon_image print(best_sim) cv2.imshow(best_sim_name, np.hstack([item_image, best_sim_image])) cv2.waitKey(0)
def show_with_diff(image, reference, title): # The image image_normY = np.sqrt(np.sum((image - reference)**2)) image_mseY = mse(image, reference) image_psnrY = psnr(reference, image) image_ssimY = ssim(image, reference) """Helper function to display denoising""" plt.figure(figsize=(15, 5)) plt.subplot(1, 3, 1) plt.title('Original\n') plt.imshow(reference, vmin=0, vmax=1, cmap=plt.cm.gray, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.subplot(1, 3, 2) difference = image - reference plt.title( 'Difference \n$\ell_2$: {0:.2f}, $MSE$: {1:.4f}, $PSNR$: {2:.2f}, $SSIM$: {3:.2f}' .format(image_normY, image_mseY, image_psnrY, image_ssimY)) plt.imshow(difference, vmin=-0.5, vmax=0.5, cmap=plt.cm.PuOr, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.subplot(1, 3, 3) plt.title('Reconstruction\n') plt.imshow(image, vmin=0, vmax=1, cmap=plt.cm.gray, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.suptitle(title, size=16) plt.subplots_adjust(0.02, 0.02, 0.98, 0.82, 0.02, 0.1) plt.savefig('results\\' + timestr + '\\' + title + '.pdf') plt.savefig('results\\' + timestr + '\\' + title + '.png')
def getResults(data, model): if HR == False: ground_truth = data['p0_true'] das = data['p0_recons'] else: ground_truth = data['p0_hr'] das = imresize(data['p0_recons'], [512, 512]) arti = data['p0_hil'] arti = arti.reshape([1, 256, 256, 1]) pred = model.predict(arti, steps=1) if HR == True: pred = pred.reshape([512, 512]) else: pred = pred.reshape([256, 256]) temp = ssim(ground_truth, pred, data_range=pred.max() - pred.min()) temp2 = psnr(ground_truth, pred, data_range=pred.max() - pred.min()) temp3 = mse(ground_truth, pred) return temp, temp2, temp3
def do_all_metrics(): pairs = [k for k in compare_years_l1.iterkeys()] for p in pairs: t = [k for k in combinations(compare_years_l1[p].iterkeys(), 2)] yr1 = t[0][0] yr2 = t[0][1] compare_years_l1[p]['MSE'] = round( mse(compare_years_l1[p][yr1], compare_years_l1[p][yr2]), 3) compare_years_l1[p]['MSE Map'] = (compare_years_l1[p][yr1] - compare_years_l1[p][yr2])**2 ssim_results = ssim(compare_years_l1[p][yr1], compare_years_l1[p][yr2], full=True) compare_years_l1[p]['SSIM'] = round(ssim_results[0], 3) compare_years_l1[p]['SSIM Map'] = ssim_results[1] cw_ssim_results = cw_ssim(compare_years_l1[p][yr1], compare_years_l1[p][yr2], 20) #width of filter compare_years_l1[p]['CW-SSIM'] = round(cw_ssim_results[0], 3) compare_years_l1[p]['CW-SSIM Map'] = cw_ssim_results[1] gmsd_results = gmsd(compare_years_l1[p][yr1], compare_years_l1[p][yr2]) compare_years_l1[p]['GMSD'] = round(gmsd_results[0], 3) compare_years_l1[p]['GMSD Map'] = gmsd_results[1] tr_res = discostrans(compare_years_l1[p][yr1], compare_years_l1[p][yr2]) compare_years_l1[p]['DCT Map ' + yr1] = tr_res[0] compare_years_l1[p]['DCT Map ' + yr2] = tr_res[1] compare_years_l1[p]['DCT Graph ' + yr1] = tr_res[2] compare_years_l1[p]['DCT Graph ' + yr2] = tr_res[3] compare_years_l1[p]['FSIM'] = ft_sim(compare_years_l1[p][yr1], compare_years_l1[p][yr2])
def show_with_diff(image, reference): textSize = 22 difference = image - reference image_seY = np.sqrt(np.sum(difference**2)) image_mseY = mse(image, reference) image_psnrY = psnr(reference, image) image_ssimY = ssim(image, reference) plt.figure(figsize=(20, 10)) plt.subplot(1, 3, 1) plt.title('Noise filled CT Scan\n', size=textSize, fontweight="bold") plt.imshow(reference, vmin=0, vmax=1, cmap=plt.cm.bone, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.subplot(1, 3, 2) plt.title( 'Difference \n$\ell_2: {0:.2f}, MSE: {1:.4f}, PSNR: {2:.2f}, SSIM: {3:.2f}$' .format(image_seY, image_mseY, image_psnrY, image_ssimY), size=textSize, fontweight="bold") plt.imshow(difference, vmin=-0.5, vmax=0.5, cmap=plt.cm.PuOr, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.subplot(1, 3, 3) plt.title('CT Scan after Cloud K-SVD\n', size=textSize, fontweight="bold") plt.imshow(image, vmin=0, vmax=1, cmap=plt.cm.bone, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.savefig('results\\' + timestr + '\\Reconstructed image.pdf')
def compare_images(imageA, imageB, title): # compute the mean squared error and structural similarity # index for the images m = mse(imageA, imageB) s = ssim(imageA, imageB, multichannel=True) # setup the figure fig = plt.figure(title) plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s)) # show first image ax = fig.add_subplot(1, 2, 1) plt.imshow(imageA) plt.axis("off") # show the second image ax = fig.add_subplot(1, 2, 2) plt.imshow(imageB) plt.axis("off") # show the images plt.show()
def compare_images(imageA, imageB, title): # compute the mean squared error and structural similarity # index for the images m = mse(imageA, imageB) s = ssim(imageA, imageB) p = psnr(imageA, imageB) # setup the figure fig = plt.figure(title) plt.suptitle("PSNR: %.2f MSE: %.2f SSIM: %.2f" % (p, m, s)) print("PSNR: %.2f MSE: %.2f SSIM: %.2f" % (p, m, s)) # show first image ax = fig.add_subplot(1, 2, 1) plt.imshow(imageA, cmap=plt.cm.gray) plt.axis("off") # show the second image ax = fig.add_subplot(1, 2, 2) plt.imshow(imageB, cmap=plt.cm.gray) plt.axis("off") # show the images plt.show()
def reg_mse(data): for d in data: mse_vals.append((mse(data[0], d)).round(2)) mse_maps.append((data[0] - d)**2)
if opt.eval: model.eval() for i, data in enumerate(dataset): if i >= opt.num_test: # only apply our model to opt.num_test images. break model.set_input(data) # unpack data from data loader model.test() # run inference visuals = model.get_current_visuals() # get image results img_path = str(data['img_path']) raw_name = img_path.replace(('[\''),'') raw_name = raw_name.replace(('.jpg\']'),'.jpg') raw_name = raw_name.split('/')[-1] image_name = '%s' % raw_name save_path = os.path.join(web_dir,'images/',image_name) for label, im_data in visuals.items(): if label=='output': output = util.tensor2im(im_data) util.save_image(output, save_path, aspect_ratio=opt.aspect_ratio) output =np.array(output, dtype=np.float32) if label=='real': real=util.tensor2im(im_data) real=np.array(real, dtype=np.float32) if label=='comp': comp=util.tensor2im(im_data) comp=np.array(comp, dtype=np.float32) mse_score_op = mse(output,real) psnr_score_op = psnr(real,output,data_range=output.max() - output.min()) print('%s | mse %0.2f | psnr %0.2f' % (image_name,mse_score_op,psnr_score_op)) webpage.save() # save the HTML
def reg_mse(data): for d in data: mse_vals.append(( mse ( data[0], d )).round(2)) mse_maps.append((data[0] - d) ** 2)
# print(up_left, down_right) cv2.rectangle(frame, up_left1, down_right1, (0, 255, 0), 3) ret, temp = cap.read() tm = 0 while cap.isOpened(): key = cv2.waitKey(1) if key == ord("q"): break if key == ord('s'): cv2.imwrite(id_generator() + '.jpg', frame2) # Capture frame-by-frame ret, frame = cap.read() m = mse(cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY), cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)) print('mse', m, '----\n') if abs(m - tm) < 2: # 静止画面,不用重复计算 continue else: temp = frame.copy() tm = m # # print(margin,frame.shape[0] - margin, margin,frame.shape[1] - margin)#40 680 40 1240 frame2 = frame[margin:frame.shape[0] - margin, margin:frame.shape[1] - margin] # .copy() # cv2.imshow('frame2', frame2) gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) # edges = cv2.Canny(gray, 50, 150, apertureSize=3) # HoughCircles(image, method, dp, minDist, circles=None, param1=None, param2=None, minRadius=None, maxRadius=None)
def compute_score_train(data, model, batch_size, FRAME, img_rows, img_cols): preimage = predictImage(data, FRAME, model, batch_size, img_rows, img_cols) / 255 mse_s = mse(data[:, 5:, :, :, 0], preimage[:, 5:]) ssim_s = compute_mssim(data[:, 5:, :, :, 0], preimage[:, 5:]) return mse_s, ssim_s, preimage[0, 5]
def mean_square_error(window_orig, window_warped, weights=1): # MSE = np.mean(weights*(window_orig - window_warped) ** 2) MSE = mse(window_orig, window_warped) return MSE
title='camera compare' plt.ion() # cap.read() # cap.read() # cap.read() # cap.read() ret, frame = cap.read() temp = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #TODO 前10帧 while cap.isOpened(): ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # m = mse(temp, gray) s = ssim(temp, gray) print("MSE: %.2f, SSIM: %.2f" % (m, s)) # temp = gray.copy() continue # # setup the figure # fig = plt.figure(title) # plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s)) # # # show first image # ax = fig.add_subplot(1, 2, 1) # plt.imshow(temp, cmap=plt.cm.gray) # plt.axis("off") #