-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
35 lines (28 loc) · 1.06 KB
/
Copy pathpreprocessing.py
File metadata and controls
35 lines (28 loc) · 1.06 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 numpy as np
class Preprocessing:
def __init__(self, image_path):
self.image_path = image_path
def image_prep_text_extrcation(self):
image = cv2.imread(self.image_path)
# 1. Coverting t o gray Scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#Contrast Stretching
gray = cv2.normalize(gray, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
#Removing noise
denoised = cv2.medianBlur(gray, 3)
# Adaptive Thresholding
binary = cv2.adaptiveThreshold(
denoised,
255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
31,
10
)
#Small Noise Removal
kernel = np.ones((1, 1), np.uint8)
cleaned = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
preprocessed_path = self.image_path.replace(".png", "_preprocessed.png")
cv2.imwrite(preprocessed_path, cleaned)
return preprocessed_path