import tensorflow as tf
import numpy as np
import os
from random import shuffle
import cv2 as cv
name_dict = {"BF":0,"BK":1,"BL":2,"BR":3,"CF":4,"CL":5,'CV':6,'CXK':7,'S':8,'XF':9}
data_root_path = "color_part_data/"
process_path = 'color_part_data_processing/'
test_file_path = data_root_path +"test.txt" #测试集数据集文件
trainer_file_path = data_root_path +"trainer.txt" #训练集数据集文件
name_data_list = {} #记录每类图片有多少训练图片、测试图片
trainer_list = []
test_list = []
#将图片完整路径存入字典
def save_train_test_file(path,name):
if name not in name_data_list:
img_list =[]
img_list.append(path)
name_data_list[name] = img_list
else:
name_data_list[name].append(path)
#遍历数据集目录,提取出图片路径,分训练集、测试集
dirs = os.listdir(data_root_path)
for d in dirs:
full_path = data_root_path + d
if os.path.isdir(full_path):
imgs = os.listdir(full_path) #列出子目录中所有图片
for img in imgs:
save_train_test_file(full_path+ "/" + img, d)
#返回图像的所有路径
def AllPicPath(data_root_path):
IMG = []
dirs = os.listdir(data_root_path)
for d in dirs:
full_path = data_root_path + d
if os.path.isdir(full_path):
imgs = os.listdir(full_path)
for img in imgs:
IMG.append(full_path+'/'+img)
return IMG
#计算所有图像最大的长和宽
def MAXLenWid(img):
leng , wid = 0 , 0
for imgpath in img:
image = cv.imread(imgpath)
L = image.shape[0]
W = image.shape[1]
if leng <= L :
leng = L
if wid <= W:
wid = W
return leng , wid
# maxwid , maxleng = MAXLenWid(AllPicPath(data_root_path))
# print(maxwid,maxleng,3)
print("---------------------------------------生成最大长宽图像-------------------------------------")
# image_fill = np.zeros((maxleng,maxwid,3))
#定义创建目录函数
def mkdir(path):
isExists = os.path.exists(path)
if not isExists:
os.mkdir(path)
else:
print("目录已存在")
allpicpath = AllPicPath(process_path)
print(allpicpath)
for picpath in allpicpath:
img = cv.imread(picpath)
img_LAB = cv.cvtColor(img, cv.COLOR_BGR2LAB)
img_HSV = cv.cvtColor(img,cv.COLOR_BGR2HSV)
picpath1 = picpath.split('/')
sub_picpath = picpath1[1]
final_save_path_LAB = 'color_part_data_LAB/'+sub_picpath +'/'+ picpath1[2]
final_save_path_HSV = 'color_part_data_HSV/'+sub_picpath +'/'+ picpath1[2]
mkdir('color_part_data_HSV/'+sub_picpath)
mkdir('color_part_data_LAB/'+sub_picpath)
cv.imwrite(final_save_path_LAB,img_LAB,[cv.IMWRITE_PNG_COMPRESSION,0])
cv.imwrite(final_save_path_HSV,img_HSV,[cv.IMWRITE_PNG_COMPRESSION,0])
# cv.imshow('img',img)
# cv.waitKey(1)
把RGB转换为LAB或HSV颜色空间并保存
于 2023-03-13 10:33:26 首次发布