-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_process.py
More file actions
35 lines (30 loc) · 1.13 KB
/
Copy pathimage_process.py
File metadata and controls
35 lines (30 loc) · 1.13 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
import cv2
import os
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import numpy as np
current_directory = os.getcwd()
# for i in range(1, 2):
file_path = os.path.join(current_directory, 'captcha_image', f'{2}.jpg')
image = cv2.imread(file_path)
image = cv2.resize(image, (140, 48), interpolation=cv2.INTER_CUBIC)
dst = cv2.fastNlMeansDenoisingColored(image, None, 30, 30, 7, 21)
ret, thresh = cv2.threshold(dst, 127, 255, cv2.THRESH_BINARY_INV)
image = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
image_copy = image.copy()
width = image_copy.shape[1]
height = image_copy.shape[0]
image_copy[:, 15:width-5] = 0
axis_y, axis_x = np.where(image_copy == 255)
x1_s = np.array([axis_x])
y1_s = height - axis_y
poly_reg = PolynomialFeatures(degree=2)
x1_t = poly_reg.fit_transform(x1_s.T)
lr = LinearRegression()
lr.fit(x1_t, y1_s)
x2_s = np.array([[i for i in range(0, width)]])
x2_t = poly_reg.fit_transform(x2_s.T)
for x, y in np.column_stack([lr.predict(x2_t).round(0), x2_s[0]]):
pos = height - int(x)
image[pos-3:pos+3, int(y)] = 255 - image[pos-3:pos+3, int(y)]