|
|
|
|
@ -178,10 +178,30 @@ def photoshop_lab_color_range_optimized(bgr_img, target_lab, tolerance=59, anti_
@@ -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):
@@ -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 锐化。 |
|
|
|
|
def unsharp_mask(img_bgr,amount=0.47, radius=3, threshold=0): |
|
|
|
|
"""""" |
|
|
|
|
|
|
|
|
|
参数: |
|
|
|
|
- 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) |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
# 差值(高频图像) |
|
|
|
|
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):
@@ -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__':
@@ -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) |
|
|
|
|
人种 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|