import cv2
import numpy as np
import os
import shutil
import time
import random
# 定义光源位置和强度
light_position = [(400, 600),
(400, 400)]
light_intensity = 1.0
hsv_value = [0, 30]
saturation_value = [100] # 饱和度值
brightness_value = [50, 100]
dark_value = [0.0001, 0.0025]
all_input_folder = 'D:/dataset_0513/new_sv_data/' # 替换为实际的原始图片文件夹路径
for one_input_folder in os.listdir(all_input_folder):
input_folder = os.path.join(all_input_folder, one_input_folder) + '/'
for filename in os.listdir(input_folder):
start_time = time.time()
if filename.endswith('jpg') or filename.endswith('jpeg') or filename.endswith('.png'):
image_path = filename.split('.')[0]
filename_all = input_folder + filename
image = cv2.imread(filename_all)
height, width, _ = image.shape
folder_path = os.path.join(input_folder, image_path)
os.makedirs(folder_path, exist_ok=True)
for location in light_position:
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
for vs in saturation_value:
for vl in hsv_value:
# if vl == -1:
# continue
if vl != -1:
hsv_image[:, :, 0] = vl # 色调值
# hsv_image[:, :, 1] += vs # 饱和度值
# hsv_image[:, :, 1] = (hsv_image[:, :, 1] + vs).astype(np.uint8)
hsv_image[:, :, 1] = np.clip(hsv_image[:, :, 1] + vs, 0, 100).astype(np.uint8) # 饱和度值
#hsv_image[:, :, 2] = np.clip(hsv_image[:, :, 2].astype(np.int32)+50, 0, 255).astype(np.uint8) # 亮度值
image2 = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR) # 转换回BGR空间
for brightness in brightness_value:
image3 = image2.astype(float)
image3 += brightness
image3 = np.clip(image3, 0, 255)
image3 = image3 / 255.0
light_image = np.zeros_like(image3)
x, y = np.meshgrid(np.arange(width), np.arange(height))
distance_map = np.sqrt((x - location[0]) ** 2 + (y - location[1]) ** 2)
for dark in dark_value:
# 根据距离计算光照强度,离光源越远亮度越低
intensity_map = light_intensity / (1 + dark * distance_map)
# 扩展光照强度到三通道
intensity_map = np.expand_dims(intensity_map, axis=-1)
intensity_map = np.tile(intensity_map, (1, 1, 3))
# 添加光照效果
image4 = image3 * intensity_map
# 将图像范围映射回 [0, 255]
image4 = (image4 * 255).astype(np.uint8)
name = '/' + str(location) + '_' + str(vl) + '_' + str(vs) +'_'+ str(brightness) + '_' + str(dark) + '.jpg'
# nameList = ['/(200, 400)_-1_0_0.005.jpg',
# '/(400, 400)_0_20_0.0025.jpg',
# '/(200, 200)_0_40_0.005.jpg',
# '/(200, 400)_10_20_0.0025.jpg']
# if name in nameList:
cv2.imwrite(folder_path + name, image4)
print("finish: " + folder_path + name)
end_time = time.time()
elapsed_time = end_time - start_time
print('代码执行时间为:', elapsed_time, '秒')
模拟灯光效果,色相,饱和度,亮度等
最新推荐文章于 2023-10-16 11:41:17 发布