调用opencv的normalize函数完成图片数据归一化到0-1的值,最后再乘以255.
版本:python3.6
import os
import io
import math
import sys
import cv2
import shutil
import random
import numpy as np
from collections import namedtuple, OrderedDict
label_names = ['person','car','bus','truck','motorcycle','chemical']
def get_files(dir, suffix):
res = []
for root, directory, files in os.walk(dir):
for filename in files:
name, suf = os.path.splitext(filename)
if suf in suffix:
#res.append(filename)
res.append(os.path.join(root, filename))
return res
def uniform_image_size(list_path,width_size,height_size):
image_list = get_files(list_path, ['.jpg'])
total_len = len(image_list)
print('total_label_len', total_len)
for i in range(0, total_len):
image_file = image_list[i]
img = cv2.imread(image_file)
cv2.imshow("input", img)
cv2.waitKey(1)
result = np.zeros(img.shape, dtype=np.float32)
cv2.normalize(img, result, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
print(result)
cv2.imshow("norm", np.uint8(result*255.0))
cv2.waitKey(1)
os.remove(image_file)
cv2.imwrite(image_file, np.uint8(result*255.0))
cv.destroyAllWindows()
def main():
list_path = r'E:\projection\fair\yunfuyanhuo5'
width_size = 1280
height_size = 720
uniform_image_size(list_path,width_size,height_size)
if __name__ == '__main__':
main()