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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from PIL import Image, ImageEnhance,ImageColor
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
with gr.Blocks() as interface:
|
| 7 |
+
|
| 8 |
+
with gr.Accordion("请选择一张图片"):
|
| 9 |
+
# gr.Markdown("Look at me...")
|
| 10 |
+
img_input = gr.Image(label='请选择一张待加工图片')
|
| 11 |
+
|
| 12 |
+
with gr.Accordion("每次调整参数后,点击【加工图片】按钮,得到图片的勾边"):
|
| 13 |
+
with gr.Row():
|
| 14 |
+
enhance = gr.Slider(0, 1, 0.8, step=0.1, label="图片彩色度")
|
| 15 |
+
blend = gr.Slider(0, 1, 0.4, step=0.1, label="颜色填充度")
|
| 16 |
+
color = gr.ColorPicker(label="勾边颜色")
|
| 17 |
+
|
| 18 |
+
section_btn = gr.Button("加工图片")
|
| 19 |
+
|
| 20 |
+
with gr.Accordion("提供4种勾边效果,均可下载本地"):
|
| 21 |
+
with gr.Row():
|
| 22 |
+
closed_output0 = gr.Image(label='自选颜色勾边')
|
| 23 |
+
img_param_output0 = gr.Image(label='极简勾边')
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
closed_output1 = gr.Image(label='自选颜色勾边')
|
| 27 |
+
img_param_output1 = gr.Image(label='简单勾边')
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
closed_output2 = gr.Image(label='自选颜色勾边')
|
| 31 |
+
img_param_output2 = gr.Image(label='细致勾边')
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
closed_output3 = gr.Image(label='彩色勾边')
|
| 35 |
+
img_param_output3 = gr.Image(label='图片+勾边合成图')
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# 调整模型结果参数
|
| 39 |
+
def turn_arguments(img,enhance,blend,color):
|
| 40 |
+
imageX = Image.fromarray(img)
|
| 41 |
+
contrast = ImageEnhance.Contrast(imageX)
|
| 42 |
+
imageX = contrast.enhance(1.5)
|
| 43 |
+
sharpness = ImageEnhance.Sharpness(imageX)
|
| 44 |
+
imageX = sharpness.enhance(1.5)
|
| 45 |
+
img = np.asarray(imageX)
|
| 46 |
+
|
| 47 |
+
#####################################
|
| 48 |
+
# 极简勾边-自选颜色 #
|
| 49 |
+
#####################################
|
| 50 |
+
gaussian_blur_0 = 13
|
| 51 |
+
structuring_element_0 = 3
|
| 52 |
+
canny_start_0 = 65
|
| 53 |
+
canny_end_0 = 100
|
| 54 |
+
thresh_val_0 = 205
|
| 55 |
+
maxval_0 = 330
|
| 56 |
+
gray0 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 57 |
+
# 对灰度图像进行高斯滤波,以去除噪声
|
| 58 |
+
gray0 = cv2.GaussianBlur(gray0, (gaussian_blur_0,gaussian_blur_0), 0)
|
| 59 |
+
# 使用Canny算子进行边缘检测
|
| 60 |
+
edges0 = cv2.Canny(gray0, canny_start_0, canny_end_0)
|
| 61 |
+
# 将边缘图像转换为二值图像
|
| 62 |
+
_, thresh0 = cv2.threshold(edges0, thresh_val_0, maxval_0, cv2.THRESH_BINARY)
|
| 63 |
+
# 对二值图像进行形态学操作,以去除小的噪点
|
| 64 |
+
kernel0 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (structuring_element_0, structuring_element_0))
|
| 65 |
+
closed0 = cv2.morphologyEx(thresh0, cv2.MORPH_CLOSE, kernel0)
|
| 66 |
+
closed0 = closed0.astype(img.dtype)
|
| 67 |
+
result0 = cv2.bitwise_and(img, img, mask=closed0)
|
| 68 |
+
result0[closed0==0] = (255,255,255)
|
| 69 |
+
line_color0 = ImageColor.getcolor(color, "RGB")
|
| 70 |
+
result0[closed0!=0] = (line_color0)
|
| 71 |
+
close00 = Image.fromarray(result0).convert('RGB')
|
| 72 |
+
|
| 73 |
+
# 颜色空间转换
|
| 74 |
+
image0 = Image.fromarray(img)
|
| 75 |
+
enhancer0 = ImageEnhance.Color(image=image0)
|
| 76 |
+
# 增强颜色
|
| 77 |
+
img0 = enhancer0.enhance(enhance).convert('RGB')
|
| 78 |
+
union_img0 = np.asarray(Image.blend(close00, img0, blend))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
#####################################
|
| 82 |
+
# 简单勾边-自选颜色 #
|
| 83 |
+
#####################################
|
| 84 |
+
gaussian_blur_1 = 13
|
| 85 |
+
structuring_element_1 = 3
|
| 86 |
+
canny_start_1 = 25
|
| 87 |
+
canny_end_1 = 45
|
| 88 |
+
thresh_val_1 = 205
|
| 89 |
+
maxval_1 = 330
|
| 90 |
+
gray1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 91 |
+
# 对灰度图像进行高斯滤波,以去除噪声
|
| 92 |
+
gray1 = cv2.GaussianBlur(gray1, (gaussian_blur_1,gaussian_blur_1), 0)
|
| 93 |
+
# 使用Canny算子进行边缘检测
|
| 94 |
+
edges1 = cv2.Canny(gray1, canny_start_1, canny_end_1)
|
| 95 |
+
# 将边缘图像转换为二值图像
|
| 96 |
+
_, thresh1 = cv2.threshold(edges1, thresh_val_1, maxval_1, cv2.THRESH_BINARY)
|
| 97 |
+
# 对二值图像进行形态学操作,以去除小的噪点
|
| 98 |
+
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (structuring_element_1, structuring_element_1))
|
| 99 |
+
closed1 = cv2.morphologyEx(thresh1, cv2.MORPH_CLOSE, kernel1)
|
| 100 |
+
closed1 = closed1.astype(img.dtype)
|
| 101 |
+
result1 = cv2.bitwise_and(img, img, mask=closed1)
|
| 102 |
+
result1[closed1==0] = (255,255,255)
|
| 103 |
+
line_color1 = ImageColor.getcolor(color, "RGB")
|
| 104 |
+
result1[closed1!=0] = (line_color1)
|
| 105 |
+
close01 = Image.fromarray(result1).convert('RGB')
|
| 106 |
+
|
| 107 |
+
# 颜色空间转换
|
| 108 |
+
image1 = Image.fromarray(img)
|
| 109 |
+
enhancer1 = ImageEnhance.Color(image=image1)
|
| 110 |
+
# 增强颜色
|
| 111 |
+
img1 = enhancer1.enhance(enhance).convert('RGB')
|
| 112 |
+
union_img1 = np.asarray(Image.blend(close01, img1, blend))
|
| 113 |
+
|
| 114 |
+
#####################################
|
| 115 |
+
# 复杂勾边-自选颜色 #
|
| 116 |
+
#####################################
|
| 117 |
+
gaussian_blur_2 = 13
|
| 118 |
+
structuring_element_2 = 3
|
| 119 |
+
canny_start_2 = 10
|
| 120 |
+
canny_end_2 = 40
|
| 121 |
+
thresh_val_2 = 205
|
| 122 |
+
maxval_2 = 330
|
| 123 |
+
gray2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 124 |
+
# 对灰度图像进行高斯滤波,以去除噪声
|
| 125 |
+
gray2 = cv2.GaussianBlur(gray2, (gaussian_blur_2,gaussian_blur_2), 0)
|
| 126 |
+
# 使用Canny算子进行边缘检测
|
| 127 |
+
edges2 = cv2.Canny(gray2, canny_start_2, canny_end_2)
|
| 128 |
+
# 将边缘图像转换为二值图像
|
| 129 |
+
_, thresh2 = cv2.threshold(edges2, thresh_val_2, maxval_2, cv2.THRESH_BINARY)
|
| 130 |
+
# 对二值图像进行形态学操作,以去除小的噪点
|
| 131 |
+
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (structuring_element_2, structuring_element_2))
|
| 132 |
+
closed2 = cv2.morphologyEx(thresh2, cv2.MORPH_CLOSE, kernel2)
|
| 133 |
+
closed2 = closed2.astype(img.dtype)
|
| 134 |
+
result2 = cv2.bitwise_and(img, img, mask=closed2)
|
| 135 |
+
result2[closed2==0] = (255,255,255)
|
| 136 |
+
line_color2 = ImageColor.getcolor(color, "RGB")
|
| 137 |
+
result2[closed2!=0] = (line_color2)
|
| 138 |
+
close02 = Image.fromarray(result2).convert('RGB')
|
| 139 |
+
|
| 140 |
+
# 颜色空间转换
|
| 141 |
+
image2 = Image.fromarray(img)
|
| 142 |
+
enhancer2 = ImageEnhance.Color(image=image2)
|
| 143 |
+
# 增强颜色
|
| 144 |
+
img2 = enhancer2.enhance(enhance).convert('RGB')
|
| 145 |
+
union_img2 = np.asarray(Image.blend(close02, img2, blend))
|
| 146 |
+
|
| 147 |
+
#####################################
|
| 148 |
+
# 简单勾边-彩色勾边 #
|
| 149 |
+
#####################################
|
| 150 |
+
closed3 = closed1.astype(img.dtype)
|
| 151 |
+
result3 = cv2.bitwise_and(img, img, mask=closed3)
|
| 152 |
+
result3[closed3==0] = (255,255,255)
|
| 153 |
+
close03 = Image.fromarray(result3).convert('RGB')
|
| 154 |
+
|
| 155 |
+
# 颜色空间转换
|
| 156 |
+
image3 = Image.fromarray(img)
|
| 157 |
+
enhancer3 = ImageEnhance.Color(image=image3)
|
| 158 |
+
# 增强颜色
|
| 159 |
+
img3 = enhancer3.enhance(enhance).convert('RGB')
|
| 160 |
+
union_img3 = np.asarray(Image.blend(close03, img3, blend))
|
| 161 |
+
|
| 162 |
+
return result0,union_img0,result1,union_img1,result2,union_img2,result3,union_img3
|
| 163 |
+
|
| 164 |
+
section_btn.click(turn_arguments,inputs=[img_input,enhance,blend,color],
|
| 165 |
+
outputs = [closed_output0,img_param_output0,closed_output1,img_param_output1,closed_output2,img_param_output2,closed_output3,img_param_output3])
|
| 166 |
+
|
| 167 |
+
interface.launch(show_api=False)
|