dreamteach
This commit is contained in:
1 parent
f107e44977
commit
a7575dddbf
3 files changed
+60
-47
No files matched your search
@@ -178,10 +178,30 @@ def photoshop_lab_color_range_optimized(bgr_img, target_lab, tolerance=59, anti_
|
||||
|
||||
return mask
|
||||
|
||||
def generate_curve_lut(x_points, y_points):
|
||||
|
||||
def generate_curve_lut(x_points, y_points, smooth_factor=20):
|
||||
"""
|
||||
输入采样点,生成 256 长度的查找表(LUT)
|
||||
"""
|
||||
if smooth_factor > 0:
|
||||
new_x, new_y = [], []
|
||||
for i in range(len(x_points) - 1):
|
||||
x0, x1 = x_points[i], x_points[i + 1]
|
||||
y0, y1 = y_points[i], y_points[i + 1]
|
||||
new_x.append(x0)
|
||||
new_y.append(y0)
|
||||
|
||||
# 在每对原始控制点之间插入平滑点
|
||||
steps = max(1, int(smooth_factor * (x1 - x0) / 256))
|
||||
for j in range(1, steps):
|
||||
alpha = j / steps
|
||||
new_x.append(int(x0 + alpha * (x1 - x0)))
|
||||
new_y.append(y0 + alpha * (y1 - y0))
|
||||
|
||||
# 添加最后一个点
|
||||
new_x.append(x_points[-1])
|
||||
new_y.append(y_points[-1])
|
||||
x_points, y_points = new_x, new_y
|
||||
cs = CubicSpline(x_points, y_points, bc_type='natural')
|
||||
x = np.arange(256)
|
||||
y = cs(x)
|
||||
@@ -206,41 +226,36 @@ def add_color_image(img):
|
||||
|
||||
return adjusted
|
||||
|
||||
def unsharp_mask(image, radius=5.0, amount=1.5, threshold=10):
|
||||
"""
|
||||
对图像应用 Unsharp Mask 锐化。
|
||||
|
||||
参数:
|
||||
- image: 输入图像,必须是3通道BGR格式
|
||||
- radius: 高斯模糊半径(标准差)
|
||||
- amount: 锐化强度
|
||||
- threshold: 差异阈值,仅大于该值的区域会被增强
|
||||
"""
|
||||
if len(image.shape) != 3 or image.shape[2] != 3:
|
||||
raise ValueError("输入必须是3通道BGR图像")
|
||||
if max(image.shape[:2]) > 20000:
|
||||
return unsharp_mask_blockwise(image, radius, amount, threshold)
|
||||
|
||||
img_float = image.astype(np.float32) if image.dtype != np.float32 else image
|
||||
blurred = cv2.GaussianBlur(img_float, (0, 0), radius)
|
||||
diff = img_float - blurred
|
||||
mask = np.abs(diff) > threshold
|
||||
sharpened = img_float.copy()
|
||||
sharpened[mask] = img_float[mask] + diff[mask] * amount
|
||||
return np.clip(sharpened, 0, 255).astype(np.uint8)
|
||||
def unsharp_mask(img_bgr,amount=0.47, radius=3, threshold=0):
|
||||
""""""
|
||||
|
||||
|
||||
def unsharp_mask_blockwise(image, radius=5.0, amount=1.5, threshold=10, block_size=1024):
|
||||
"""
|
||||
分块执行 Unsharp Mask,适用于超大图像,防止内存爆炸。
|
||||
"""
|
||||
h, w = image.shape[:2]
|
||||
output = np.zeros_like(image)
|
||||
for y in range(0, h, block_size):
|
||||
for x in range(0, w, block_size):
|
||||
block = image[y:y + block_size, x:x + block_size]
|
||||
output[y:y + block_size, x:x + block_size] = unsharp_mask(block, radius, amount, threshold)
|
||||
return output
|
||||
img = img_bgr.astype(np.float32) / 255.0
|
||||
img_ycc = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
|
||||
y, cr, cb = cv2.split(img_ycc)
|
||||
|
||||
# 模糊亮度通道
|
||||
blurred_y = cv2.GaussianBlur(y, (0, 0), sigmaX=radius, sigmaY=radius)
|
||||
|
||||
# 差值(高频图像)
|
||||
high_pass = y - blurred_y
|
||||
|
||||
# 阈值处理:模仿 PS 的 threshold(在 0~1 范围内)
|
||||
if threshold > 0:
|
||||
mask = np.abs(high_pass) >= (threshold / 255.0)
|
||||
high_pass = high_pass * mask
|
||||
|
||||
# 关键:亮边增强 + 暗边压制(模拟 PS 更自然的边缘)
|
||||
sharp_y = y + amount * high_pass
|
||||
sharp_y = np.clip(sharp_y, 0, 1)
|
||||
|
||||
# 合并回图像
|
||||
merged_ycc = cv2.merge([sharp_y, cr, cb])
|
||||
final_img = cv2.cvtColor(merged_ycc, cv2.COLOR_YCrCb2BGR)
|
||||
final_img = np.clip(final_img * 255.0, 0, 255).astype(np.uint8)
|
||||
|
||||
#cv2.imwrite(output_path, final_img)
|
||||
return final_img
|
||||
|
||||
|
||||
def add_shadow_image(img):
|
||||
@@ -347,7 +362,7 @@ def photoshop_actions_emulation(input_path, output_path):
|
||||
# output_white_path= output_color_path.replace(".jpg","white.jpg")
|
||||
# cv2.imwrite(output_white_path, result_white_image)
|
||||
#锐化
|
||||
result_usm = unsharp_mask(result_white_image, radius=2, amount=0.4, threshold=10)
|
||||
result_usm = unsharp_mask(result_white_image,amount=0.47, radius=3, threshold=0)
|
||||
|
||||
cv2.imwrite(output_path, result_usm)
|
||||
|
||||
@@ -363,6 +378,6 @@ if __name__ == '__main__':
|
||||
input_path = os.path.join(args.in_dir,args.image_name)
|
||||
output_path = os.path.join(args.out_dir,args.image_name_new)
|
||||
photoshop_actions_emulation(input_path, output_path)
|
||||
人种
|
||||
|
||||
|
||||
|
||||
@@ -439,7 +439,7 @@ def photoshop_add_white(original_img, ):
|
||||
#h, w = adjusted_img.shape[:2]
|
||||
#mask = cv2.resize(mask_uint8, (w, h))
|
||||
result = photoshop_feather_blend(adjusted_img, original_img, mask_uint8,
|
||||
feather_radius=15, brightness_factor=0.9999)
|
||||
feather_radius=150, brightness_factor=0.9999)
|
||||
#output_path="/data/datasets_20t/Downloads_google/correct_show_obj_dream_tech/290082/cache/290082Tex1_o_white.jpg"
|
||||
|
||||
return result
|
||||
@@ -469,7 +469,7 @@ def photoshop_add_white3(input_path, ):
|
||||
#h, w = adjusted_img.shape[:2]
|
||||
#mask = cv2.resize(mask_uint8, (w, h))
|
||||
result = photoshop_feather_blend(adjusted_img, original_img, mask_uint8,
|
||||
feather_radius=15, brightness_factor=0.99)
|
||||
feather_radius=150, brightness_factor=0.999999)
|
||||
output_path="/data/datasets_20t/Downloads_google/correct_show_obj_dream_tech/290082/cache/290082Tex1_o_white.jpg"
|
||||
cv2.imwrite(output_path, result)
|
||||
return result
|
||||
|
||||
@@ -221,10 +221,11 @@ def apply_curve_up_image(image_path,image_cache_dir):
|
||||
h, s, v = cv2.split(image_hsv)
|
||||
v_mean = np.mean(v)
|
||||
print(f"v_mean{v_mean}")
|
||||
if v_mean < 60:
|
||||
if v_mean < 50:
|
||||
adjusted = apply_curve(image_bgr, lut)
|
||||
adjusted2 = apply_curve(adjusted, lut)
|
||||
cv2.imwrite(result_path, adjusted2)
|
||||
adjusted3 = apply_curve(adjusted2, lut)
|
||||
cv2.imwrite(result_path, adjusted3)
|
||||
return result_path
|
||||
|
||||
else:
|
||||
@@ -248,7 +249,7 @@ def apply_curve_down_image(image_path,image_cache_dir):
|
||||
|
||||
v_mean = np.mean(v)
|
||||
print(f"v_mean{v_mean}")
|
||||
if v_mean > 110:
|
||||
if v_mean > 50:
|
||||
adjusted = apply_curve(image_bgr, lut)
|
||||
adjusted2 = apply_curve(adjusted, lut)
|
||||
cv2.imwrite(result_path, adjusted2)
|
||||
@@ -310,10 +311,7 @@ def correct_texture_image(input_path,image_result_dir,output_path):
|
||||
image_cache_dir= os.path.join(image_result_dir,"cache")
|
||||
os.makedirs(image_cache_dir, exist_ok=True)
|
||||
input_path_cure_up = apply_curve_up_image(input_path,image_cache_dir)
|
||||
|
||||
input_path_cure_down_result = apply_curve_down_image(input_path_cure_up,image_cache_dir)
|
||||
|
||||
|
||||
print("input_path_correct", input_path_cure_down_result)
|
||||
shadow_up_path = input_path_cure_down_result.replace(".jpg", "_shadow_shadow_add_color_white_unsharp.jpg")
|
||||
photoshop_actions_emulation(input_path_cure_down_result, shadow_up_path)
|
||||
@@ -328,13 +326,12 @@ def correct_texture_image(input_path,image_result_dir,output_path):
|
||||
return shadow_up_path
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
arg = argparse.ArgumentParser()
|
||||
arg.add_argument('-input_path', type=str, default=f"")
|
||||
arg.add_argument('-output_path', type=str, default=f"")
|
||||
args = arg.parse_args()
|
||||
|
||||
image_result_dir=os.path.dirname(args.output_path)
|
||||
os.makedirs(image_result_dir, exist_ok=True)
|
||||
|
||||
@@ -344,9 +341,10 @@ if __name__ == "__main__":
|
||||
total_time = round(end_time - start_time, 2)
|
||||
"""
|
||||
DreamTech,PS动作F7两次+Shift F7一次
|
||||
F7:::加暗*2
|
||||
F7:::加暗*2+1
|
||||
Shift F7 公仔*1
|
||||
公仔: 加暗,加暗,加饱和度上色,白位加白,锐化
|
||||
ps版本2020
|
||||
"""
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user