def noise_estimation(im: np.ndarray): """ Given an image, this function determines the variance in its pixel values and displays a histogram of the image along with a fitted normal distribution. """ # Calculate histogram im_hist, bin_edges = np.histogram(im, 30) bin_centers = bin_edges[:-1] + (np.diff(bin_edges) / 2) # Normalize histogram to make a PDF im_hist = im_hist.astype(float) im_hist /= np.sum(im_hist) # Calculate the mean and variance values mean = np.sum(im_hist * bin_centers) var = np.sum(im_hist * (bin_centers**2)) - (mean**2) # Calculate the values on the normal distribution PDF norm_vals = np.exp(-((bin_centers - mean) ** 2) / (2 * var)) \ / np.sqrt(2 * np.pi * var) # Normalize the norm_vals norm_vals /= np.sum(norm_vals) # Rescale variance var /= (255**2) print('Variance: {}'.format(var)) dip.figure() dip.bar(bin_centers, im_hist) dip.plot(bin_centers, norm_vals, 'r') dip.legend(['Fitted Gaussian PDF', 'Histogram of image']) dip.xlabel('Pixel value') dip.ylabel('Occurrence') dip.show()
def hist25(image): dim = image.shape x = np.reshape(image, (dim[0] * dim[1], 1)) dip.hist(x) dip.grid() dip.title('Histogram') dip.xlabel('Pixel value') dip.ylabel('Pixel frequency')
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 hist(image, plot): dim = image.shape M = dim[0] N = dim[1] res = np.zeros(256) for i in range(0, M): for j in range(0, N): res[image[i, j]] += 1 if plot: dip.bar(np.arange(256), res, width=1) dip.grid() dip.title('Histogram') dip.xlabel('Pixel value') dip.ylabel('Pixel frequency') return res
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()
x_max = max(x_max, dim[1]) y_max = max(y_max, dim[0]) image_size_distribution = np.zeros((y_max + 100, x_max + 100)) for filename in os.listdir(directory): im = dip.im_read(directory + filename) dim = im.shape image_size_distribution[dim[0]][dim[1]] += 1 image_size_distribution = image_size_distribution / np.amax( image_size_distribution) dip.contour(dip.float_to_im(image_size_distribution, 8)) dip.grid() dip.xlabel('image width') dip.ylabel('image height') dip.title('Image size distribution') ########################### # CONVERTING TO GRAYSCALE # ########################### for directory in dirs: for filename in os.listdir(directory): out = Image.open(directory + filename).convert('L') filename_without_extension = os.path.splitext(filename)[0] if directory == dir_no: out.save("../grayscale_data/no/" + filename_without_extension + ".png") elif directory == dir_yes: out.save("../grayscale_data/yes/" + filename_without_extension +