把RGB转换为LAB或HSV颜色空间并保存

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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值