import os
from PIL import Image
import numpy as np
from tqdm import tqdm
img_path = r'D:/dataset/6/sy' #获取当前代码文件的路径
save_path = r'D:/dataset/normalization/sy' #获取存储路径
a = 0
b = 1
#定义为1,是因为如果为0最后代码中b值类型不会改变,
#数值类型不同无法进行归一化计算,可以更大一点以确保初始定义值不是最小值
i = 1
#找出全局像素最大值、最小值
for item in tqdm(os.listdir(img_path)):
arr = item.strip().split('*')
img_name = arr[0]
image_path = os.path.join(img_path, img_name)
img = Image.open(image_path)
c = np.max(img)
d = np.min(img)
if a < c:
a = c
else:
a = a
if b > d:
b = d
else:
b = b
if i % 20 == 0:
print('max:{}'.format(a))
print('min:{}'.format(b))
i += 1
#归一化操作
for item in tqdm(os.listdir(img_path)):
arr = item.strip().split('*')
img_name = arr[0]
image_path = os.path.join(img_path, img_name)
img = Image.open(image_path)
img = (img - b) / (a - b) #最大最小值归一化
img = 255 * img #考虑是否需要可视化,决定是否乘255
img = Image.fromarray(np.uint8(img))
img.save(save_path +'/'+img_name)
最大最小值归一化的相关知识相对基础,其他博客的解释也很详尽,我就不班门弄斧了。只是实现一个功能比较基础的代码。
69

被折叠的 条评论
为什么被折叠?



