def run_gibbs_with_external_field(image): #image = images_processing.reduce_channels_for_sampler(image) image = images_processing.convert_image_to_ising_model(image) iterations = 2 initial_beta = 0.3 beta_difference = 0.1 beta_range = arithmetic_progression_series(initial_beta, beta_difference, 10) rows = range(image.shape[0]) columns = range(image.shape[1]) beta = 1.3 all_spins_energy = sum_of_all_spins(image) for t in range(iterations): for i in rows: for j in columns: site = (i, j) s = neighbors_energy(image, site) # a sum of markov blanket values #odds = np.exp(2*beta*s + 0.5*all_spins_energy) p = 1 / (1 + np.exp(-2 * s)) u = random.random() if u < p.any(): image[i, j] = 1 else: image[i, j] = -1 sampled_image = images_processing.convert_from_ising_to_image(image) #sampled_image = images_processing.restore_channels(sampled_image, 3) # restored image images_processing.save_image( 'Denoised images/testing_no_channel_reduction/metropolis_noise_ising_chest_b={0}_noise={1}' .format(beta, NOISE_LEVEL), 'jpeg', sampled_image)
def run_gibbs_sampler(image): """Run the Gibbs sampler for the given noised image.""" beta = 0.8 external_strength = 2 image = images_processing.reduce_channels_for_sampler( image) # convert a 3-channel image to 1-channel one image = images_processing.convert_image_to_ising_model( image) # initial image # initial image sampled_image = image temperature = range(0, 100) width, height = image.shape[0], image.shape[1] # dimensions for t in temperature: for i in range(width): # rows for j in range(height): # columns prob = probability(beta, external_strength, image, (i, j)) flipped_value = -image[i, j] random_number = random.random() if np.log(random_number) < prob.any(): sampled_image[i][j] = flipped_value sampled_image = images_processing.convert_from_ising_to_image( sampled_image) sampled_image = images_processing.restore_channels(sampled_image, 3) print(sampled_image.shape) cv2.imwrite('gibbs_sampler_im_iter={}.png'.format(len(temperature)), sampled_image)
def run_metropolis_with_noise(image): """ Runs the Metropolis sampler with a strategy to choose the next pixel to update non-randomly. :param image: an image of size m x n :return: saves the result of denoising to the given folder """ image = images_processing.reduce_channels_for_sampler(image) image = images_processing.convert_image_to_ising_model(image) iterations = ITERATIONS_NUMBER initial_beta = 0.3 beta_difference = 0.1 # beta_range = arithmetic_progression_series(initial_beta, beta_difference, 12) beta_range = [0.8] rows = range(image.shape[0]) columns = range(image.shape[1]) for beta in beta_range: for t in range(iterations): for i in rows: for j in columns: site = (i, j) flipped_value = - image[site] d = beta * metropolis_sampler.potentials(image, site) + noise(NOISE_LEVEL) d_stroke = beta * metropolis_sampler.potentials(image, site, flipped_pixel_value=True) + noise( NOISE_LEVEL) posterior = np.exp(min(d_stroke - d, 0)) u = random.random() if u < posterior: image[site] = flipped_value sampled_image = images_processing.convert_from_ising_to_image(image) sampled_image = images_processing.restore_channels(sampled_image, 3) # restored image images_processing.save_image( 'metropolis_noise_ising_beta={0}_iter={1}'.format(beta, iterations), 'jpg', sampled_image, 'Denoised images/metropolis_sampler_noise_ising_model/noise={0}/brain_tissue_im/not_random'.format( NOISE_LEVEL))
def run_gibbs_without_noise(image): image = images_processing.reduce_channels_for_sampler(image) image = images_processing.convert_image_to_ising_model(image) iterations = 100 initial_beta = 0.3 beta_difference = 0.1 beta_range = arithmetic_progression_series(initial_beta, beta_difference, 10) beta = 0.8 # noise_prob = 0.05 # this parameter is taken from the knowledge of noise level in the image rows = range(image.shape[0]) columns = range(image.shape[1]) for t in range(iterations): for i in rows: for j in columns: site = (i, j) number_of_black_neighbors = energy_gibbs(image, site, 'black') number_of_white_neighbors = energy_gibbs(image, site, 'white') Z_normalizing_constant = np.exp( number_of_black_neighbors) + np.exp( number_of_white_neighbors) posterior = np.exp( beta * number_of_black_neighbors) / Z_normalizing_constant u = random.random() if u < posterior: image[site] = 1 else: image[site] = -1 sampled_image = images_processing.convert_from_ising_to_image(image) sampled_image = images_processing.restore_channels(sampled_image, 3) # restored image images_processing.save_image( 'Denoised images/gibbs_sampler_no_noise/metropolis_noise_ising_chest_b={0}_noise={1}' .format(beta, NOISE_LEVEL), 'jpeg', sampled_image)
def run_metropolis_sampler(image): """Run the Metropolis sampler for the given noised image.""" # arbitrary beta and pi TODO: How beta, pi and gamma can be interpreted? beta = 0.8 # gamma = 1.0 pi = 0.05 # print("Before:") # print(image.shape) image = images_processing.reduce_channels_for_sampler( image) # convert a 3-channel image to 1-channel one image = images_processing.convert_image_to_ising_model( image) # initial image init_image = image current_image = init_image width, height = image.shape[0], image.shape[1] # dimensions iterations = range(width * height) for t in iterations: i, j = random.randint(0, width - 1), random.randint(0, height - 1) flipped_value = -image[i, j] pixel_position = (i, j) alpha = acceptance_probability(beta, pi, init_image, current_image, pixel_position) random_number = random.random() if np.log(random_number) < alpha.any(): current_image[i][j] = flipped_value sampled_image = images_processing.convert_from_ising_to_image( current_image) sampled_image = images_processing.restore_channels(sampled_image, 3) print(sampled_image.shape) cv2.imwrite('testingiter={}.jpg'.format(len(iterations)), sampled_image) # should be a separate function
def run_metropolis_without_noise_for_beta_range(image_name, initial_beta, beta_step, iterations, neighbors_number): original_image = cv2.imread(image_name) original_image = images_processing.reduce_channels_for_sampler(original_image) original_image = images_processing.convert_image_to_ising_model(original_image) rows = range(original_image.shape[0]) columns = range(original_image.shape[1]) beta_range = arithmetic_progression_series(initial_beta, beta_step, 10) for beta in beta_range: sampled_image = original_image # for different values of beta, reset the sampled image to the original one for t in range(iterations): for i in rows: for j in columns: i = random.randint(0, rows - 1) j = random.randint(0, columns - 1) current_site = (i, j) flipped_value = - sampled_image[current_site] d = beta * metropolis_sampler.potentials(sampled_image, current_site, flipped_pixel_value=False, neighbors_number=neighbors_number) d_flipped = beta * metropolis_sampler.potentials(sampled_image, current_site, flipped_pixel_value=True) posterior = np.exp(min(d_flipped - d, 0)) u = random.random() if u < posterior: sampled_image[current_site] = flipped_value sampled_image = images_processing.convert_from_ising_to_image(sampled_image) sampled_image = images_processing.restore_channels(sampled_image, 3) format = what(image_name) images_processing.save_image( 'result_beta={0}_iter={1}_neighbors={2}'.format(beta, iterations, neighbors_number), format, sampled_image, directory='Results')
def run_metropolis_without_noise(image_name, beta, iterations, neighbors_number): original_image = cv2.imread(image_name) original_image = images_processing.reduce_channels_for_sampler(original_image) original_image = images_processing.convert_image_to_ising_model(original_image) sampled_image = original_image rows = range(original_image.shape[0]) columns = range(original_image.shape[1]) for t in range(iterations): for i in rows: for j in columns: i = random.randint(0, rows - 1) j = random.randint(0, columns - 1) current_site = (i, j) flipped_value = - sampled_image[current_site] d = beta * metropolis_sampler.potentials(sampled_image, current_site, flipped_pixel_value=False, neighbors_number=neighbors_number) # the conditional probability for the opposite to the random pixel's value d_flipped = beta * metropolis_sampler.potentials(sampled_image, current_site, flipped_pixel_value=True) posterior = np.exp(min(d_flipped - d, 0)) u = random.random() if u < posterior: sampled_image[current_site] = flipped_value sampled_image = images_processing.convert_from_ising_to_image(sampled_image) sampled_image = images_processing.restore_channels(sampled_image, 3) format = what(image_name) images_processing.save_image( 'result_beta={0}_iter={1}_neighbors={2}'.format(beta, iterations, neighbors_number), format, sampled_image, directory='Results') print('success')
def denoising_pipeline(image_name, beta, iterations, noise_probability, neighbors_number): """ Performs denoising of a given image using the Metropolis sampler. :param image_name: a name of the image located in the current directory (from which the script is called) :param beta: an essential parameter of the sampler, an inverse to the temperature (1/T) :param iterations: the number of iterations :param noise_probability: the assumed probability of the flip noise in the image :param neighbors_number: the size of the neighborhood structure. it's used when calculating the energy of the neighborhood. the default is 8. can be switched to 4 :return: saves a result of denoising to the folder 'Results' (it's created if doesn't exist) """ # read the image original_image = cv2.imread(image_name) # reduce channels original_image = images_processing.reduce_channels_for_sampler(original_image) # convert to Ising original_image = images_processing.convert_image_to_ising_model(original_image) # create a copy of the image to which the changes will be applied sampled_image = original_image # accept beta as an argument # accept iterations number as an argument # accept noise probability as an argument # accept the neighbourhood structure's size # get image dimensions rows = original_image.shape[0] # height columns = original_image.shape[1] # width for t in range(iterations): # generate a random pixel position i = random.randint(0, rows - 1) j = random.randint(0, columns - 1) current_site = (i, j) # find the opposite value of the current pixel flipped_value = - sampled_image[current_site] # the conditional probability for the random pixel's value d = beta * metropolis_sampler.potentials(sampled_image, current_site, flipped_pixel_value=False, neighbors_number=neighbors_number) + noise(noise_probability, sampled_image[current_site], original_image[ current_site]) # the conditional probability for the opposite to the random pixel's value d_flipped = beta * metropolis_sampler.potentials(sampled_image, current_site, flipped_pixel_value=True) + noise( noise_probability, - sampled_image[current_site], original_image[current_site]) posterior = np.exp(min(d_flipped - d, 0)) u = random.random() if u < posterior: sampled_image[current_site] = flipped_value # convert back from Ising sampled_image = images_processing.convert_from_ising_to_image(sampled_image) # restore channels sampled_image = images_processing.restore_channels(sampled_image, 3) # restored image # create a separate folder to save the result with parameters specified format = what(image_name) images_processing.save_image( 'result_beta={0}_noise_p={1}_iter={2}_neighbors={3}'.format(beta, noise_probability, iterations, neighbors_number), format, sampled_image, directory='Results')
def denoising_pipeline(image_name, beta, iterations, noise_probability, neighbors_number): # read the image original_image = cv2.imread(image_name) # reduce channels original_image = images_processing.reduce_channels_for_sampler(original_image) # convert to Ising original_image = images_processing.convert_image_to_ising_model(original_image) # create a copy of the image to which the changes will be applied sampled_image = original_image # accept beta as an argument # accept iterations number as an argument # accept noise probability as an argument # accept the neighbourhood structure's size # get image dimensions rows = original_image.shape[0] # height columns = original_image.shape[1] # width for t in range(iterations): # generate a random pixel position i = random.randint(0, rows - 1) j = random.randint(0, columns - 1) current_site = (i, j) # find the opposite value of the current pixel flipped_value = - sampled_image[current_site] # the conditional probability for the random pixel's value d = beta * metropolis_sampler.potentials(sampled_image, current_site, flipped_pixel_value=False, neighbors_number=neighbors_number) + noise(noise_probability, sampled_image[current_site], original_image[ current_site]) # the conditional probability for the opposite to the random pixel's value d_flipped = beta * metropolis_sampler.potentials(sampled_image, current_site, flipped_pixel_value=True) + noise( noise_probability, - sampled_image[current_site], original_image[current_site]) posterior = np.exp(min(d_flipped - d, 0)) u = random.random() if u < posterior: sampled_image[current_site] = flipped_value # convert back from Ising sampled_image = images_processing.convert_from_ising_to_image(sampled_image) # restore channels sampled_image = images_processing.restore_channels(sampled_image, 3) # restored image # create a separate folder to save the result with parameters specified format = imghdr.what(image_name) print('successfully denoised') images_processing.save_image('result_beta={}_noise_p={}_iter={}_neighbors={}', format, sampled_image, directory='Results')