def _plot_psnr_against_var(self, image, deg_type, name="no_name"):
     fh = ImageFileHandler()
     im_degrader = ImageDegrader()
     im = fh.open_image_file_as_matrix(file)
     vars = np.linspace(0.01, 0.5, 10)
     restored_psnrs = []
     degraded_psnrs = []
     generic_psnrs = []
     count = 0
     comparison_func = lambda x, y: dip.PSNR(x, y)
     for var in vars:
         degraded_im = im_degrader.degrade(im,
                                           degradation_type=deg_type,
                                           severity_value=var)
         restored_im, _, h_params = self.multiplicative_clustering_restore(
             degraded_im)
         restored_generic_im = self.fast_multiplicative_restore(
             degraded_im,
             h_param=int(np.mean(h_params)),
             search_window_size=21)
         degraded_psnrs.append(comparison_func(im, degraded_im))
         restored_psnrs.append(comparison_func(im, restored_im))
         generic_psnrs.append(comparison_func(im, restored_generic_im))
         count += 1
     plt.plot(vars, restored_psnrs, "b", label="Clustering Restore")
     plt.plot(vars, degraded_psnrs, "r", label="Degraded Image")
     plt.plot(vars, generic_psnrs, "y", label="fastN1Means Restore")
     plt.legend()
     plt.xlabel("Variance level of noise")
     plt.ylabel("PSNR")
     plt.title("Effect of clustering on PSNR for image {}".format(name))
     plt.show()
Ejemplo n.º 2
0
def iqa_results(im1, im2):
    print('The MSE is: {:.2f}'.format(
        dip.MSE(cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY),
                cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY))))
    print('The PSNR is: {:.2f}'.format(
        dip.PSNR(cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY),
                 cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY),
                 np.amax(cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)))))
    def _test_restore_mode(self, file, deg_type, save_images=False, name=None):
        fh = ImageFileHandler()
        im_degrader = ImageDegrader()
        im = fh.open_image_file_as_matrix(file)
        degraded_im = im_degrader.degrade(im,
                                          degradation_type=deg_type,
                                          severity_value=.5)
        restored_im, clustered_im, h_params = self.multiplicative_clustering_restore(
            degraded_im)
        restored_im_2 = self.fast_multiplicative_restore(
            degraded_im, h_param=np.mean(h_params), search_window_size=21)
        if save_images:
            dip.im_write(dip.float_to_im(degraded_im),
                         "./" + name + "_degraded_image.jpg",
                         quality=95)
            dip.im_write(dip.float_to_im(restored_im),
                         "./" + name + "_restored_image.jpg",
                         quality=95)
            dip.im_write(dip.float_to_im(clustered_im),
                         "./" + name + "_clustered_image.jpg",
                         quality=95)

        dip.figure()

        dip.subplot(141)
        dip.imshow(im, cmap="gray")

        dip.subplot(142)
        dip.imshow(degraded_im, cmap="gray")
        dip.xlabel("PSNR: {0:.2f}, SSIM: {1:.2f}".format(
            dip.PSNR(im, degraded_im),
            dip.SSIM(im, degraded_im)[0]))

        dip.subplot(143)
        dip.imshow(restored_im, cmap="gray")
        dip.xlabel("PSNR: {0:.2f}, SSIM: {1:.2f}".format(
            dip.PSNR(im, restored_im),
            dip.SSIM(im, restored_im)[0]))

        dip.subplot(144)
        dip.imshow(restored_im_2, cmap="gray")
        dip.xlabel("PSNR: {0:.2f}, SSIM: {1:.2f}".format(
            dip.PSNR(im, restored_im_2),
            dip.SSIM(im, restored_im_2)[0]))

        dip.show()
Ejemplo n.º 4
0
 def _test_noise_mode(self, file, deg_type):
     fh = ImageFileHandler()
     im = fh.open_image_file_as_matrix(file)
     degraded_im = self.degrade(im, degradation_type=deg_type)
     dip.figure()
     dip.subplot(121)
     dip.imshow(im, cmap="gray")
     dip.subplot(122)
     dip.imshow(degraded_im, cmap="gray")
     dip.xlabel("PSNR: {0:.2f}".format(dip.PSNR(im, degraded_im)))
     dip.show()
 def param_search_multiplicative_restore(self, degraded_image,
                                         original_image):
     int_image = dip.float_to_im(degraded_image)
     psnr_max = None
     best_denoise = None
     for i in range(1, 25):
         cur_denoised = dip.im_to_float(
             cv2.fastNlMeansDenoising(int_image, h=i, searchWindowSize=31))
         cur_psnr = dip.PSNR(original_image, cur_denoised)
         if psnr_max is None or cur_psnr > psnr_max:
             best_denoise = cur_denoised
             psnr_max = cur_psnr
     return best_denoise
def run_optical_flow(filepath_ind: int, OF_alg: int, param: int=100,
                     display: bool=True):

    frame_1 = dip.im_read(filepaths[filepath_ind] + 'frame1.png')[:, :, :3]
    frame_2 = dip.im_read(filepaths[filepath_ind] + 'frame2.png')[:, :, :3]
    residual = np.abs(frame_1.astype(float) - frame_2.astype(float)) \
            .astype(np.uint8)
    frame_1_gray = dip.rgb2gray(frame_1)
    frame_2_gray = dip.rgb2gray(frame_2)
    PSNR_val = dip.PSNR(frame_1_gray, frame_2_gray)
    if display:
        # Plot the initial images
        dip.figure()
        dip.subplot(1, 3, 1)
        dip.imshow(frame_1)
        dip.title('Frame 1', fontsize='x-small')
        dip.subplot(1, 3, 2)
        dip.imshow(frame_2)
        dip.title('Frame 2', fontsize='x-small')
        dip.subplot(1, 3, 3)
        dip.imshow(residual)
        dip.title('Residual - PSNR: {:.2f} dB'.format(PSNR_val), fontsize='x-small')

    # Convert to grayscale for analysis
    frame_1 = dip.im_to_float(frame_1_gray)
    frame_2 = dip.im_to_float(frame_2_gray)
    start_time = default_timer()

    # ============================ EDIT THIS PART =============================
    mask_x = np.array([[-1, 1], [-1,1]])
    mask_y = np.array([[-1, -1], [1,1]])
    mask_t_2 = np.array([[-1, -1], [-1,-1]])
    mask_t_1 = np.array([[1, 1], [1,1]])
    dIx = dip.convolve2d(frame_1, mask_x, mode='same', like_matlab=True)
    dIy = dip.convolve2d(frame_1, mask_y, mode='same', like_matlab=True)
    dIt = dip.convolve2d(frame_1, mask_t_1, mode='same', like_matlab=True) + dip.convolve2d(frame_2, mask_t_2, mode='same', like_matlab=True)
    
    # ==========!!!!! DO NOT EDIT ANYTHING BELOW THIS !!!!!====================

    # Instantiate blank u and v matrices
    u = np.zeros_like(frame_1)
    v = np.zeros_like(frame_1)

    if 0 == OF_alg:
        print('The optical flow is estimated using Horn-Schuck...')
        u, v = horn_schuck(u, v, dIx, dIy, dIt, param)
    elif 1 == OF_alg:
        print('The optical flow is estimated using Lucas-Kanade...')
        u, v = lucas_kanade(u, v, dIx, dIy, dIt, param)
    else:
        raise ValueError('OF_alg must be either 0 or 1')

    end_time = default_timer()

    # Determine run time
    duration = end_time - start_time
    clock = [int(duration // 60), int(duration % 60)]
    print('Flow estimation time was {} minutes and {} seconds'
            .format(*clock))

    # Downsample for better visuals
    stride = 10
    m, n = frame_1.shape
    x, y = np.meshgrid(range(n), range(m))
    x = x.astype('float64')
    y = y.astype('float64')

    # Downsampled u and v
    u_ds = u[::stride, ::stride]
    v_ds = v[::stride, ::stride]

    # Coords for downsampled u and v
    x_ds = x[::stride, ::stride]
    y_ds = y[::stride, ::stride]

    # Estimated flow
    estimated_flow = np.stack((u, v), axis=2)

    # Read file for ground truth flow
    ground_truth_flow = read_flow_file(filepaths[filepath_ind] + 'flow1_2.flo')
    u_gt_orig = ground_truth_flow[:, :, 0]
    v_gt_orig = ground_truth_flow[:, :, 1]
    u_gt = np.where(np.isnan(u_gt_orig), 0, u_gt_orig)
    v_gt = np.where(np.isnan(v_gt_orig), 0, v_gt_orig)


    # Downsampled u_gt and v_gt
    u_gt_ds = u_gt[::stride, ::stride]
    v_gt_ds = v_gt[::stride, ::stride]
    if display:
        # Plot the optical flow field
        dip.figure()
        dip.subplot(2, 2, 1)
        dip.imshow(frame_2, 'gray')
        dip.quiver(x_ds, y_ds, u_ds, v_ds, color='r')
        dip.title('Estimated', fontsize='x-small')
        dip.subplot(2, 2, 2)
        dip.imshow(frame_2, 'gray')
        dip.quiver(x_ds, y_ds, u_gt_ds, v_gt_ds, color='r')
        dip.title('Ground truth', fontsize='x-small')
        # Draw colored velocity flow maps
        dip.subplot(2, 2, 3)
        dip.imshow(flow_to_color(estimated_flow))
        dip.title('Estimated', fontsize='x-small')
        dip.subplot(2, 2, 4)
        dip.imshow(flow_to_color(ground_truth_flow))
        dip.title('Ground truth', fontsize='x-small')

    # Normalization for metric computations
    normalize = lambda im: (im - np.min(im)) / (np.max(im) - np.min(im))
    un = normalize(u)
    un_gt = normalize(u_gt)
    un_gt[np.isnan(u_gt_orig)] = 1
    vn = normalize(v)
    vn_gt = normalize(v_gt)
    vn_gt[np.isnan(v_gt_orig)] = 1

    # Error calculations and displays
    EPE = ((un - un_gt) ** 2 + (vn - vn_gt) ** 2) ** 0.5
    AE = np.arccos(((un * un_gt) + (vn * vn_gt) + 1) /
                   (((un + vn + 1) * (un_gt + vn_gt + 1)) ** 0.5))
    EPE_nan_ratio = np.sum(np.isnan(EPE)) / EPE.size
    AE_nan_ratio = np.sum(np.isnan(AE)) / AE.size
    EPE_inf_ratio = np.sum(np.isinf(EPE)) / EPE.size
    AE_inf_ratio = np.sum(np.isinf(AE)) / AE.size
    print('Error nan ratio: EPE={:.2f}, AE={:.2f}'
            .format(EPE_nan_ratio, AE_nan_ratio))
    print('Error inf ratio: EPE={:.2f}, AE={:.2f}'
            .format(EPE_inf_ratio, AE_inf_ratio))
    EPE_avg = np.mean(EPE[~np.isnan(EPE)])
    AE_avg = np.mean(AE[~np.isnan(AE)])
    print('EPE={:.2f}, AE={:.2f}'.format(EPE_avg, AE_avg))

    if display:
        dip.show()

    return clock, EPE_avg, AE_avg
Ejemplo n.º 7
0
img3 = dip.convolve2d(img2, Gaussian, mode='same')

Lap_kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
ELap_kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]])

Lap1 = dip.convolve2d(img1, Lap_kernel, mode='same')
Lap2 = dip.convolve2d(img2, Lap_kernel, mode='same')
Lap3 = dip.convolve2d(img3, Lap_kernel, mode='same')

ELap1 = dip.convolve2d(img1, ELap_kernel, mode='same')
ELap2 = dip.convolve2d(img2, ELap_kernel, mode='same')
ELap3 = dip.convolve2d(img3, ELap_kernel, mode='same')

sharpen1 = scale(img1 - Lap1)
print('sharpen1')
print(dip.PSNR(img, sharpen1))
sharpen2 = scale(img2 - Lap2)
print('sharpen2')
print(dip.PSNR(img, sharpen2))
sharpen3 = scale(img3 - Lap3)
print('sharpen3')
print(dip.PSNR(img, sharpen3))

Esharpen1 = scale(img1 - ELap1)
print('Esharpen1')
print(dip.PSNR(img, Esharpen1))
Esharpen2 = scale(img2 - ELap2)
print('Esharpen2')
print(dip.PSNR(img, Esharpen2))
Esharpen3 = scale(img3 - ELap3)
print('Esharpen3')
Ejemplo n.º 8
0
 def calc_score(self, reconstructed_image):
     return dip.PSNR(self.image, reconstructed_image, np.max(self.image))
Ejemplo n.º 9
0
        tmp2 = np.pad(tmp2, ((0, 0), (error_col, error_col), (0, 0)),
                      'constant',
                      constant_values=(0, 0))
    tmp3 = tmp2[int(tmp2.shape[0] / 2 - 1 - row / 2 +
                    1):int(tmp2.shape[0] / 2 + row / 2),
                int(tmp2.shape[1] / 2 - 1 - col / 2 +
                    1):int(tmp2.shape[1] / 2 + col / 2), :]
    tmp4 = np.clip(tmp3, 0, 1)
    return tmp4


#example
filename = 'images/airplane.jpg'
im = dip.im_read(filename)
#downSampling
down_IMG = zoomAndshrink(im, 2.5, 1.0 / 1.7, 27.5 / 180.0 * np.pi)
dip.figure(1)
dip.imshow(down_IMG)

#upSampling
up_IMG = zoomAndshrink_inv(down_IMG, 0.4, 1.7, -27.5 / 180.0 * np.pi, im.shape)
dip.figure(2)
dip.imshow(up_IMG)

#show difference
dip.figure(3)
dip.imshow(im - up_IMG)

#show PSNR
print(dip.PSNR(im, up_IMG))
dip.show()
Ejemplo n.º 10
0
im_DCT_reconstructed = dip.block_process(im_DCT_quantized_reconstructed,
                                         dequantize, block_size)

# STEP 13: Inverse DCT
# ============================ EDIT THIS PART =================================
im_reconstructed = dip.block_process(im_DCT_reconstructed, dip.idct_2d,
                                     block_size)

# STEP 14: Add 127 to every pixel
# ============================ EDIT THIS PART =================================
im_reconstructed = im_reconstructed + 127

# ===================!!!!! DO NOT EDIT THIS PART !!!!!=========================
dip.subplot(2, 2, 2)
dip.imshow(im_reconstructed, 'gray')
dip.title('Reconstructed image')

# ===================!!!!! DO NOT EDIT THIS PART !!!!!=========================
im = im + 127

# STEP 15: Calculating MSE and PSNR
# ============================ EDIT THIS PART =================================
MSE = dip.MSE(im, im_reconstructed)
PSNR = dip.PSNR(im, im_reconstructed, np.max(im))

# ===================!!!!! DO NOT EDIT THIS PART !!!!!=========================
print("MSE = {:.2f}".format(MSE))
print("PSNR = {:.2f} dB".format(PSNR))

dip.show()