-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_util.py
More file actions
56 lines (41 loc) · 1.36 KB
/
image_util.py
File metadata and controls
56 lines (41 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from scipy.linalg import norm
from scipy import sum
import numpy as np
from PIL import Image
import matplotlib.image as mpimg
# Module for working with images (rgb/grayscale numpy arrays)
# Funktioniert nicht.
# def load_image(infilename):
# img = Image.open(infilename)
# img.load()
# data = np.asarray(img, dtype="int32")
# return data # as numpy array
def load_image(infilename):
return mpimg.imread(infilename) # as numpy array
def save_image(npdata, outfilename):
img = Image.fromarray(np.asarray(
np.clip(npdata, 0, 255), dtype="uint8"), "L")
img.save(outfilename)
# Grauwert Spreizung
def normalize(arr):
rng = arr.max()-arr.min()
amin = arr.min()
return (arr-amin)*255/rng
def rgb_to_gray(rgb_numpyarr):
return np.dot(rgb_numpyarr[..., :3], [0.2989, 0.5870, 0.1140])
def compare_images_manhatten(img1, img2):
# normalize may be unnecessary
img1 = normalize(img1)
img2 = normalize(img2)
# calculate the difference elementwise for numpy arrays
diff = img1 - img2
m_norm = sum(abs(diff)) # Manhattan norm
return m_norm
def compare_images_zero(img1, img2):
# normalize may be unnecessary
img1 = normalize(img1)
img2 = normalize(img2)
# calculate the difference elementwise for numpy arrays
diff = img1 - img2
z_norm = norm(diff.ravel(), 0) # Zero norm
return z_norm