Spaces:
Paused
Paused
File size: 913 Bytes
accd6a0 46a8b27 accd6a0 93695d8 3a9f1a9 f3a699a 3042cee 93695d8 bd622b0 93695d8 accd6a0 f3a699a 3a9f1a9 46a8b27 f3a699a accd6a0 46a8b27 accd6a0 f3a699a accd6a0 f3a699a accd6a0 74edb04 accd6a0 74edb04 bd622b0 f3a699a bd622b0 3a9f1a9 f3a699a | 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 | import cv2
import numpy as np
from config.settings import THRESHOLD
def preprocess_image(img):
if hasattr(img, "convert"):
img = np.array(img)
if len(img.shape) == 3:
gray = cv2.cvtColor(
img,
cv2.COLOR_RGB2GRAY
)
else:
gray = img.copy()
# Increase contrast
gray = cv2.equalizeHist(gray)
# Detect pencil lines (threshold now driven by config, not hardcoded)
_, binary = cv2.threshold(
gray,
THRESHOLD,
255,
cv2.THRESH_BINARY_INV
)
# Close tiny gaps
kernel = np.ones((3, 3), np.uint8)
binary = cv2.morphologyEx(
binary,
cv2.MORPH_CLOSE,
kernel,
iterations=2
)
return binary
def remove_noise(binary):
kernel = np.ones((2, 2), np.uint8)
return cv2.morphologyEx(
binary,
cv2.MORPH_OPEN,
kernel
) |