-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverage_image.py
More file actions
51 lines (38 loc) · 1.64 KB
/
average_image.py
File metadata and controls
51 lines (38 loc) · 1.64 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
##############################################################
def average_image(paths):
first_image = cv2.imread(paths[0])
height, width, layers = first_image.shape
total_image = np.zeros((height, width, layers), np.float)
for path in paths[1:]:
current_image = cv2.imread(path)
total_image = cv2.add(total_image, current_image.astype(np.float))
average_image = total_image / len(paths)
plt.title('average_image')
plt.imshow(average_image)
plt.axis('off')
plt.show()
cv2.imwrite(os.path.join(image_folder, "average_image.jpg"), average_image)
average_image(paths)
##############################################################
import cv2
import numpy as np
import os
def average_images(image_folder):
# 画像が保存されているフォルダから画像のリストを取得
images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]
# 画像のサイズを取得
first_image = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = first_image.shape
# 画像の合計を初期化
total_image = np.zeros((height, width, layers), np.float)
# 各画像を合計に追加
for img in images:
img_path = os.path.join(image_folder, img)
current_image = cv2.imread(img_path)
total_image = cv2.add(total_image, current_image.astype(np.float))
# 画像の枚数で割って平均を計算
average_image = total_image / len(images)
# 平均画像を保存
cv2.imwrite(os.path.join(image_folder, "average_image.jpg"), average_image)
image_folder = "path/to/your/image/folder"
average_images(image_folder)