-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencv.py
More file actions
140 lines (95 loc) · 4.34 KB
/
Copy pathopencv.py
File metadata and controls
140 lines (95 loc) · 4.34 KB
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
import telebot
from telebot import types
import os
import cv2 #opencv-python
import commands
import numpy as np
API_KEY = '5263831940:AAGxk7T_bP7eQGDmKeH6sopuVCH7dpY_8bo'
bot = telebot.TeleBot(API_KEY)
# Операции над изображениями
# -----------------------------------------------------------------------
def blur(image, matrix):
pic = cv2.imread(image) # Читаю исходное изображение
cv2.imwrite('output_image.jpg', cv2.blur(pic, matrix))
# -----------------------------------------------------------------------
def chb(image):
pic = cv2.imread(image) # Читаю исходное изображение
image_gray = cv2.cvtColor(pic, cv2.COLOR_BGR2GRAY)
cv2.imwrite('output_image.jpg', image_gray)
# -----------------------------------------------------------------------
def canny(image, matrix):
pic = cv2.imread(image) # Читаю исходное изображение
image = cv2.Canny(pic, matrix[0], matrix[1] )
cv2.imwrite('output_image.jpg', image)
# -----------------------------------------------------------------------
def mirror_y(image):
pic = cv2.imread(image)
img_flip = cv2.flip(pic, 1)
cv2.imwrite('output_image.jpg', img_flip)
def mirror_x(image):
pic = cv2.imread(image)
img_flip = cv2.flip(pic, 0)
cv2.imwrite('output_image.jpg', img_flip)
# -----------------------------------------------------------------------
def cartoon(img, k):
# https://projectgurukul.org/cartooning-image-opencv-python/
# Reading image
img = cv2.imread(img)
# Defining input data for clustering
data = np.float32(img).reshape((-1, 3))
# Defining criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 1.0)
# Applying cv2.kmeans function
_, label, center = cv2.kmeans(data, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
# Reshape the output data to the size of input image
result = center[label.flatten()]
result = result.reshape(img.shape)
# Convert the input image to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Perform adaptive threshold
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 9, 8)
# Smooth the result
blurred = cv2.medianBlur(result, 3)
# Combine the result and edges to get final cartoon effect
cartoon = cv2.bitwise_and(blurred, blurred, mask=edges)
cv2.imwrite('output_image.jpg', cartoon)
# -----------------------------------------------------------------------
# Next Step Handlers для записи матриц ( для обработки изображений)
# -----------------------------------------------------------------------
def blur_matrix_step(message):
try:
matrix = abs(int(message.text))
except Exception:
bot.send_message(message.chat.id, text="Матрицей должно быть целое число, попробуйте ещё раз.")
return
matrix = (matrix, matrix)
blur("input_image.jpg", matrix)
all_done_photo(message.chat.id)
# -----------------------------------------------------------------------
def canny_matrix_step(message):
try:
matrix = abs(int(message.text))
except Exception:
bot.send_message(message.chat.id, text="Матрицей должно быть целое число, попробуйте ещё раз.")
return
matrix = (matrix, matrix*2)
canny("input_image.jpg", matrix)
all_done_photo(message.chat.id)
# -----------------------------------------------------------------------
def cartoon_matrix_step(message):
matrix = int(message.text)
cartoon("input_image.jpg", matrix)
all_done_photo(message.chat.id)
# Отправляет фото
# -----------------------------------------------------------------------
def all_done_photo(chat_id):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
back = types.KeyboardButton(commands.dict["exit"])
markup.add(back)
bot.send_photo(chat_id,
open("output_image.jpg", "rb"),
caption="Фото после обработки", reply_markup=markup)
os.remove("input_image.jpg")
os.remove("output_image.jpg")
# -----------------------------------------------------------------------