Skip to content

Instantly share code, notes, and snippets.

View nimpy's full-sized avatar
💭
Akku fast leer

Nina nimpy

💭
Akku fast leer
View GitHub Profile
@nimpy
nimpy / calculate_ssd.py
Last active November 4, 2022 08:29
Computing the sum of squared differences (SSD) between two images.
import numpy as np
def calculate_ssd(img1, img2):
"""Computing the sum of squared differences (SSD) between two images."""
if img1.shape != img2.shape:
print("Images don't have the same shape.")
return
return np.sum((np.array(img1, dtype=np.float32) - np.array(img2, dtype=np.float32))**2)
@nimpy
nimpy / add_gaussian_noise.py
Created February 17, 2019 13:54
Add Gaussian noise to an image of type np.uint8.
import numpy as np
def add_gaussian_noise(image, mean=0, sigma=20):
"""Add Gaussian noise to an image of type np.uint8."""
gaussian_noise = np.random.normal(mean, sigma, image.shape)
gaussian_noise = gaussian_noise.reshape(image.shape)
noisy_image = image + gaussian_noise
noisy_image = np.clip(noisy_image, 0, 255)
noisy_image = noisy_image.astype(np.uint8)
return noisy_image
@nimpy
nimpy / calculate_psnr.py
Last active May 12, 2024 04:07
Calculating peak signal-to-noise ratio (PSNR) between two images.
import numpy as np
def calculate_psnr(img1, img2, max_value=255):
""""Calculating peak signal-to-noise ratio (PSNR) between two images."""
mse = np.mean((np.array(img1, dtype=np.float32) - np.array(img2, dtype=np.float32)) ** 2)
if mse == 0:
return 100
return 20 * np.log10(max_value / (np.sqrt(mse)))
@nimpy
nimpy / 1ch_2_3ch.py
Last active July 28, 2020 13:59
Making 3 channel image by stacking a 1 channel image three times
import numpy as np
image_3ch = np.repeat(image_1ch, 3, axis=1).reshape((image_1ch.shape[0], image_1ch.shape[1], 3))
@nimpy
nimpy / max_pool.py
Last active May 14, 2019 13:52
Fast implementation of max pooling
def max_pool(image, pool_size=8):
"""
Fast implementation of max pooling, given the following is true:
- the length of image shape is 3
- the image is channels-last
- pooling height, width, and stride are all equal (pool_size)
- height % pool_size == 0
- width % pool_size == 0
Code taken and adjusted from: max_pool_forward_reshape() in https://github.com/mratsim/Arraymancer/issues/174
@nimpy
nimpy / zimnica_template.py
Created December 12, 2019 15:35
Template for pickling and unpickling variables
import pickle
import datetime
def pickle_vars(var1, var2):
pickle_file_path = 'zimnica/pickled_vars_' + datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + '.pickle'
try:
pickle.dump((var1, var2), open(pickle_file_path, "wb"))
except Exception as e:
print("Problem while trying to pickle: ", str(e))
@nimpy
nimpy / read_file_line_by_line.py
Created April 28, 2020 09:20
Read a file line by line
file = open('path/to/file', 'r')
count = 0
while True:
line = file.readline()
if not line:
break
# do something here
@nimpy
nimpy / rmse.py
Created May 6, 2020 09:22
Compute Root MSE between two arrays
def rmse(a, b):
# Root MSE (Mean Squared Error)
return np.sqrt(np.mean(np.square(np.subtract(a, b, dtype=np.float32))))
@nimpy
nimpy / rgb_to_greyscale.py
Created July 27, 2020 14:32
Convert an RGB image to a greyscale image
import numpy as np
def rgb_to_greyscale(image):
return np.dot(image[...,:3], [0.2989, 0.5870, 0.1140])
@nimpy
nimpy / crop.py
Created June 29, 2021 14:10
A script that crops an image
#!/usr/bin/env python
import argparse
import imageio
parser = argparse.ArgumentParser()
parser.add_argument('input', default='image.png',
help="Path to the input image to be cropped")
parser.add_argument('--output',