Spaces:
Runtime error
Runtime error
File size: 837 Bytes
ffa6aac |
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 |
import cv2
import numpy as np
def preprocess(image: np.ndarray) -> np.ndarray:
# Upscale, interpolation with nearest neighbor
image = cv2.resize(image, (0, 0), fx=3, fy=3, interpolation=cv2.INTER_NEAREST)
# Denoise gray-like pixels
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, (0, 70, 70), (255, 255, 255))
mask = cv2.bitwise_not(mask)
image[np.where(mask)] = 255
# Convert to binary
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU)
# Fix some holes
kernel = np.ones((3, 3), np.uint8)
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel, iterations=2)
# add padding
image = cv2.copyMakeBorder(
image, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=(255, 255, 255)
)
return image
|