-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAverageImages.py
86 lines (64 loc) · 2.93 KB
/
AverageImages.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
#import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
import time
from skimage.feature import ORB, match_descriptors
from skimage.measure import ransac
from skimage.transform import warp, downscale_local_mean, resize, SimilarityTransform
from skimage.io import imread, imsave
import time
from PIL import Image
########################################################################33
def matchFeatures(keypoints1, descriptors1, keypoints2, descriptors2):
matches12 = match_descriptors(descriptors1, descriptors2, cross_check=True)
# Select keypoints from the source (image to be registered) and target (reference image)
src = keypoints2[matches12[:, 1]][:, ::-1]
dst = keypoints1[matches12[:, 0]][:, ::-1]
model_robust, inliers = ransac((src, dst), SimilarityTransform,
min_samples=4, residual_threshold=1, max_trials=300)
return model_robust, inliers
###################################################################################
if __name__ == "__main__":
# resiz is set so to make feature detect faster
image_files = sorted(os.listdir("c:/temp/average"))
for img in image_files:
if img.split(".")[-1].lower() not in ["jpg", "jpeg", "png"]:
image_files.remove(img)
# Assuming all images are the same size, get dimensions of first image
w, h = Image.open("c:/temp/average/{}".format(image_files[0])).size
N = len(image_files)
# Create a numpy array of floats to store the average (assume RGB images)
arr = np.zeros((h, w, 3), np.float)
for im in image_files:
imarr = np.array(Image.open("c:/temp/average/{}".format(im)), dtype=np.float32)
arr = arr + imarr / N
# Round values in array and cast as 8-bit integer
arr = np.array(np.round(arr), dtype=np.uint8)
# Generate, save and preview final image
out = Image.fromarray(arr, mode="RGB")
out.save("c:/temp/Average.png")
out.show()
print ("That's All Folks!")
if False:
import os, numpy, PIL
from PIL import Image
# Access all PNG files in directory
allfiles = os.listdir(os.getcwd())
imlist = [filename for filename in allfiles if filename[-4:] in [".png", ".PNG"]]
# Assuming all images are the same size, get dimensions of first image
w, h = Image.open(imlist[0]).size
N = len(imlist)
# Create a numpy array of floats to store the average (assume RGB images)
arr = numpy.zeros((h, w, 3), numpy.float)
# Build up average pixel intensities, casting each image as an array of floats
for im in imlist:
imarr = numpy.array(Image.open("c:/temp/average/{}".format(im)), dtype=numpy.float)
arr = arr + imarr / N
# Round values in array and cast as 8-bit integer
arr = numpy.array(numpy.round(arr), dtype=numpy.uint8)
# Generate, save and preview final image
out = Image.fromarray(arr, mode="RGB")
out.save("Average.png")
out.show()