Update pictureDeal.py
Browse files- pictureDeal.py +71 -4
pictureDeal.py
CHANGED
@@ -1,7 +1,74 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
from PIL import Image, ImageEnhance
|
3 |
import gradio as gr
|
4 |
+
from sklearn.cluster import KMeans
|
5 |
+
import numpy as np
|
6 |
|
7 |
+
with gr.Blocks() as interface:
|
|
|
8 |
|
9 |
+
with gr.Row():
|
10 |
+
n_colors = gr.Slider(2, 32, 12, step=1, label="图片要加工的目标颜色数量")
|
11 |
+
|
12 |
+
with gr.Row():
|
13 |
+
img_input = gr.Image()
|
14 |
+
img_output = gr.Image()
|
15 |
+
|
16 |
+
section_btn1 = gr.Button("合并色彩")
|
17 |
+
|
18 |
+
# 图片模型训练
|
19 |
+
def img_fit_predict(img,n_colors):
|
20 |
+
data = img.reshape(-1,3)
|
21 |
+
# 把原始图片压缩成n_colors个颜色
|
22 |
+
kmeans = KMeans(n_clusters=n_colors)
|
23 |
+
y_ = kmeans.fit_predict(data)
|
24 |
+
# 模型合并颜色
|
25 |
+
colors = kmeans.cluster_centers_/255
|
26 |
+
output_temp = colors[y_].reshape(img.shape)
|
27 |
+
return output_temp
|
28 |
+
|
29 |
+
section_btn1.click(img_fit_predict, inputs=[img_input,n_colors], outputs=img_output)
|
30 |
+
|
31 |
+
with gr.Row():
|
32 |
+
gaussian_blur = gr.Slider(1, 13, 13, step=2, label="整体降噪参数调整")
|
33 |
+
structuring_element = gr.Slider(1, 13, 3, step=2, label="去除小噪声")
|
34 |
+
canny_start = gr.Slider(1, 200, 4, step=1, label="边缘检测-开始参数")
|
35 |
+
canny_end = gr.Slider(1, 200, 10, step=1, label="边缘检测-结束参数")
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
thresh_val = gr.Slider(50, 500, 205, step=1, label="二值图像-thresh")
|
39 |
+
maxval = gr.Slider(50, 500, 330, step=1, label="二值图像-maxval")
|
40 |
+
enhance = gr.Slider(0, 1, 0.8, step=0.1, label="增强颜色-enhance")
|
41 |
+
blend = gr.Slider(0, 1, 0.4, step=0.1, label="增强颜色-blend")
|
42 |
+
|
43 |
+
section_btn2 = gr.Button("调整图片")
|
44 |
+
with gr.Row():
|
45 |
+
closed_output = gr.Image()
|
46 |
+
img_param_output = gr.Image()
|
47 |
+
|
48 |
+
# 调整模型结果参数
|
49 |
+
def turn_arguments(img,img_output,gaussian_blur,structuring_element,canny_start,canny_end,thresh_val,maxval,enhance,blend):
|
50 |
+
gray = cv2.cvtColor(img_output, cv2.COLOR_BGR2GRAY)
|
51 |
+
# 对灰度图像进行高斯滤波,以去除噪声
|
52 |
+
gray = cv2.GaussianBlur(gray, (gaussian_blur,gaussian_blur), 0)
|
53 |
+
# 使用Canny算子进行边缘检测
|
54 |
+
edges = cv2.Canny(gray, canny_start, canny_end)
|
55 |
+
# 将边缘图像转换为二值图像
|
56 |
+
_, thresh = cv2.threshold(edges, thresh_val, maxval, cv2.THRESH_BINARY)
|
57 |
+
# 对二值图像进行形态学操作,以去除小的噪点
|
58 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (structuring_element, structuring_element))
|
59 |
+
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
|
60 |
+
image = Image.fromarray(img_output)
|
61 |
+
closed = closed.astype(img.dtype)
|
62 |
+
# 颜色空间转换
|
63 |
+
enhancer = ImageEnhance.Color(image=image)
|
64 |
+
# 增强颜色
|
65 |
+
img1 = enhancer.enhance(enhance).convert('RGB')
|
66 |
+
img2 = Image.fromarray(closed).convert('RGB')
|
67 |
+
union_img = np.asarray(Image.blend(img2, img1, blend))
|
68 |
+
return closed,union_img
|
69 |
+
|
70 |
+
section_btn2.click(turn_arguments,inputs=[img_input, img_output,gaussian_blur,
|
71 |
+
structuring_element,canny_start,canny_end,thresh_val,maxval,enhance,blend ],
|
72 |
+
outputs = [closed_output,img_param_output])
|
73 |
+
|
74 |
+
interface.launch()
|