hsv 明度的范围_通过HSV转换的方式实现图片数据增强
生活随笔
收集整理的這篇文章主要介紹了
hsv 明度的范围_通过HSV转换的方式实现图片数据增强
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在我的上一篇文章中,我記錄了自己將MOT17-Det數據集轉換成VOC格式:
HUST小菜雞:將MOT17-Det數據集轉成VOC格式?zhuanlan.zhihu.com但是在后期的測試過程中,發現了一些小問題:
考慮到這些問題,今天對其進行一些對應的更新:
有關于HSV和RGB空間的轉換及相關特性參照我的這篇文章:
HUST小菜雞:HSV和RGB通道顏色的區別和轉換?zhuanlan.zhihu.com首先先明確H,S,V分別表示什么
H表示色調,取值范圍為(0,180)
S表示飽和度,取值范圍為(0,255)
V表示亮度,取值范圍為(0,255)
def gamma_transform_s(img, gamma):hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)illum = hsv[..., 1] / 255.illum = np.power(illum, gamma)v = illum * 255.v[v > 255] = 255v[v < 0] = 0hsv[..., 1] = v.astype(np.uint8)img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)return imgdef gamma_transform_v(img, gamma):hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)illum = hsv[..., 2] / 255.illum = np.power(illum, gamma)v = illum * 255.v[v > 255] = 255v[v < 0] = 0hsv[..., 2] = v.astype(np.uint8)img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)return imgdef gamma_transform_sv(img, gamma1,gamma2):hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)illum = hsv[..., 2] / 255.illum = np.power(illum, gamma1)v = illum * 255.v[v > 255] = 255v[v < 0] = 0hsv[..., 2] = v.astype(np.uint8)illum = hsv[..., 1] / 255.illum = np.power(illum, gamma2)v = illum * 255.v[v > 255] = 255v[v < 0] = 0hsv[..., 1] = v.astype(np.uint8)img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)return img這里定義了三個函數,一個是通過gamma變換進行飽和度變換,一個是通過gamma變換進行明度變換,一個是通過gamma變換進行飽和度和明度變換,這樣就會產生原數據四倍體量的數據,并且所有的gamma都是隨機生成的,使得數據集具有更多的普適性。
首先看一組測試的demo,看HSV轉換后生成的數據
原圖片文件明度通過gamma=0.5的變換后生成的圖片明度通過gamma=1.15的變換后生成的圖片飽和度通過gamma=0.5的變換后生成的圖片飽和度通過gamma=1.15的變換后生成的圖片通過修改后的代碼實現數據集的增強如下所示
import os import cv2 import codecs import time import numpy as npori_gt_lists = ['D:/MOT17-Det/train/MOT17-02/gt/gt.txt','D:/MOT17-Det/train/MOT17-04/gt/gt.txt','D:/MOT17-Det/train/MOT17-05/gt/gt.txt','D:/MOT17-Det/train/MOT17-09/gt/gt.txt','D:/MOT17-Det/train/MOT17-10/gt/gt.txt','D:/MOT17-Det/train/MOT17-11/gt/gt.txt','D:/MOT17-Det/train/MOT17-13/gt/gt.txt']img_dir = 'D:/MOT17-Det/voc/JPEGImages/' annotation_dir = 'D:/MOT17-Det/voc/Annotations/' root = 'D:/MOT17-Det/voc/ImageSets/Main/'fp_trainlist = open(root + 'train_list.txt','w')def replace_char(string,char,index):string = list(string)string[index] = charreturn ''.join(string)def gamma_transform_s(img, gamma):hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)illum = hsv[..., 1] / 255.illum = np.power(illum, gamma)v = illum * 255.v[v > 255] = 255v[v < 0] = 0hsv[..., 1] = v.astype(np.uint8)img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)return imgdef gamma_transform_v(img, gamma):hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)illum = hsv[..., 2] / 255.illum = np.power(illum, gamma)v = illum * 255.v[v > 255] = 255v[v < 0] = 0hsv[..., 2] = v.astype(np.uint8)img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)return imgdef gamma_transform_sv(img, gamma1,gamma2):hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)illum = hsv[..., 2] / 255.illum = np.power(illum, gamma1)v = illum * 255.v[v > 255] = 255v[v < 0] = 0hsv[..., 2] = v.astype(np.uint8)illum = hsv[..., 1] / 255.illum = np.power(illum, gamma2)v = illum * 255.v[v > 255] = 255v[v < 0] = 0hsv[..., 1] = v.astype(np.uint8)img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)return imgfor each_dir in ori_gt_lists:start_time = time.time()fp = open(each_dir, 'r')userlines = fp.readlines()fp.close()# 尋找gt中的對應的最大frame# max_indx = 0# for line in userlines:# e_fram = int(line.split(',')[0])# if e_fram > max_index:# max_index = e_fram# print(max_index)fram_list = []for line in userlines:e_fram = int(line.split(',')[0])fram_list.append(e_fram)max_index = max(fram_list)print(each_dir + 'max_index:', max_index)for i in range(1, max_index):clear_name = each_dir[-12:-10] + format(str(i), '0>6s')format_name = clear_name + '.jpg'detail_dir = img_dir + format_nameimg = cv2.imread(detail_dir)shape_img = img.shapeheight = shape_img[0]width = shape_img[1]depth = shape_img[2]gamma1 = np.random.uniform(0.5,1.5)gamma2 = np.random.uniform(0.5, 1.5)gamma3 = np.random.uniform(0.5, 1.5)gamma4 = np.random.uniform(0.5, 1.5)img1 = gamma_transform_s(img,gamma1)img2 = gamma_transform_v(img, gamma2)img3 = gamma_transform_sv(img, gamma3,gamma4)format_name1 = replace_char(format_name,'1',2)format_name2 = replace_char(format_name, '2', 2)format_name3 = replace_char(format_name, '3', 2)cv2.imwrite(img_dir+format_name1,img1)cv2.imwrite(img_dir + format_name2, img2)cv2.imwrite(img_dir + format_name3, img3)txt_name = format_name[:-4]txt_name1 = format_name1[:-4]txt_name2 = format_name2[:-4]txt_name3 = format_name3[:-4]# fp.writelines(txt_name + 'n')# fp.writelines(txt_name1 + 'n')# fp.writelines(txt_name2 + 'n')# fp.writelines(txt_name3 + 'n')xml_list = [txt_name,txt_name1,txt_name2,txt_name3]each_index = [num for num,x in enumerate(fram_list) if x == (i)]for xml_name in xml_list:fp_trainlist.writelines(xml_name + 'n')with codecs.open(annotation_dir + xml_name + '.xml', 'w') as xml:xml.write('<?xml version="1.0" encoding="UTF-8"?>n')xml.write('<annotation>n')xml.write('t<folder>' + 'voc' + '</folder>n')xml.write('t<filename>' + xml_name + '.jpg' + '</filename>n')# xml.write('t<path>' + path + "/" + info1 + '</path>n')xml.write('t<source>n')xml.write('tt<database> The MOT17-Det </database>n')xml.write('t</source>n')xml.write('t<size>n')xml.write('tt<width>' + str(width) + '</width>n')xml.write('tt<height>' + str(height) + '</height>n')xml.write('tt<depth>' + str(depth) + '</depth>n')xml.write('t</size>n')xml.write('tt<segmented>0</segmented>n')for j in range(len(each_index)):num = each_index[j]x1 = int(userlines[num].split(',')[2])y1 = int(userlines[num].split(',')[3])x2 = int(userlines[num].split(',')[4])y2 = int(userlines[num].split(',')[5])xml.write('t<object>n')xml.write('tt<name>person</name>n')xml.write('tt<pose>Unspecified</pose>n')xml.write('tt<truncated>0</truncated>n')xml.write('tt<difficult>0</difficult>n')xml.write('tt<bndbox>n')xml.write('ttt<xmin>' + str(x1) + '</xmin>n')xml.write('ttt<ymin>' + str(y1) + '</ymin>n')xml.write('ttt<xmax>' + str(x1 + x2) + '</xmax>n')xml.write('ttt<ymax>' + str(y1 + y2) + '</ymax>n')xml.write('tt</bndbox>n')xml.write('t</object>n')xml.write('</annotation>')end_time = time.time()print('process {} cost time:{}s'.format(each_dir,(end_time-start_time))) fp_trainlist.close() print('succeed in processing all gt files') 數據集體量變成了21243標注的數量和train.txt中的數量也一致實現了匹配,實現了數據增強的目的
總結
以上是生活随笔為你收集整理的hsv 明度的范围_通过HSV转换的方式实现图片数据增强的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 要推出智能手表?蔚来申请注册 NIOWA
- 下一篇: 为什么我们家里的 IP 都是 192.1