Spaces:
Running
Running
File size: 4,217 Bytes
f8cafb8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
"""
亮度、对比度、锐化、饱和度调整模块
"""
import cv2
import numpy as np
def adjust_brightness_contrast_sharpen_saturation(
image,
brightness_factor=0,
contrast_factor=0,
sharpen_strength=0,
saturation_factor=0,
):
"""
调整图像的亮度、对比度、锐度和饱和度。
参数:
image (numpy.ndarray): 输入的图像数组。
brightness_factor (float): 亮度调整因子。大于0增加亮度,小于0降低亮度。
contrast_factor (float): 对比度调整因子。大于0增加对比度,小于0降低对比度。
sharpen_strength (float): 锐化强度。
saturation_factor (float): 饱和度调整因子。大于0增加饱和度,小于0降低饱和度。
返回:
numpy.ndarray: 调整后的图像。
"""
if (
brightness_factor == 0
and contrast_factor == 0
and sharpen_strength == 0
and saturation_factor == 0
):
return image.copy()
adjusted_image = image.copy()
# 调整饱和度
if saturation_factor != 0:
adjusted_image = adjust_saturation(adjusted_image, saturation_factor)
# 调整亮度和对比度
alpha = 1.0 + (contrast_factor / 100.0)
beta = brightness_factor
adjusted_image = cv2.convertScaleAbs(adjusted_image, alpha=alpha, beta=beta)
# 增强锐化
adjusted_image = sharpen_image(adjusted_image, sharpen_strength)
return adjusted_image
def adjust_saturation(image, saturation_factor):
"""
调整图像的饱和度。
参数:
image (numpy.ndarray): 输入的图像数组。
saturation_factor (float): 饱和度调整因子。大于0增加饱和度,小于0降低饱和度。
返回:
numpy.ndarray: 调整后的图像。
"""
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
s = s.astype(np.float32)
s = s + s * (saturation_factor / 100.0)
s = np.clip(s, 0, 255).astype(np.uint8)
hsv = cv2.merge([h, s, v])
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
def sharpen_image(image, strength=0):
"""
对图像进行锐化处理。
参数:
image (numpy.ndarray): 输入的图像数组。
strength (float): 锐化强度,范围建议为0-5。0表示不进行锐化。
返回:
numpy.ndarray: 锐化后的图像。
"""
print(f"Sharpen strength: {strength}")
if strength == 0:
return image.copy()
strength = strength * 20
kernel_strength = 1 + (strength / 500)
kernel = (
np.array([[-0.5, -0.5, -0.5], [-0.5, 5, -0.5], [-0.5, -0.5, -0.5]])
* kernel_strength
)
sharpened = cv2.filter2D(image, -1, kernel)
sharpened = np.clip(sharpened, 0, 255).astype(np.uint8)
alpha = strength / 200
blended = cv2.addWeighted(image, 1 - alpha, sharpened, alpha, 0)
return blended
# Gradio接口
def base_adjustment(image, brightness, contrast, sharpen, saturation):
adjusted = adjust_brightness_contrast_sharpen_saturation(
image, brightness, contrast, sharpen, saturation
)
return adjusted
if __name__ == "__main__":
import gradio as gr
iface = gr.Interface(
fn=base_adjustment,
inputs=[
gr.Image(label="Input Image", height=400),
gr.Slider(
minimum=-20,
maximum=20,
value=0,
step=1,
label="Brightness",
),
gr.Slider(
minimum=-100,
maximum=100,
value=0,
step=1,
label="Contrast",
),
gr.Slider(
minimum=0,
maximum=5,
value=0,
step=1,
label="Sharpen",
),
gr.Slider(
minimum=-100,
maximum=100,
value=0,
step=1,
label="Saturation",
),
],
outputs=gr.Image(label="Adjusted Image"),
title="Image Adjustment",
description="Adjust the brightness, contrast, sharpness, and saturation of an image using sliders.",
)
iface.launch()
|