Spaces:
Sleeping
Sleeping
File size: 2,586 Bytes
7d30f01 2ed1ac5 7d30f01 |
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 |
import cv2
import numpy as np
def preprocess_image_simple(image_file):
img = cv2.imdecode(np.fromstring(image_file.read(), np.uint8), 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return gray
def preprocess_image(image_file):
img = cv2.imdecode(np.fromstring(image_file.read(), np.uint8), 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
blur = cv2.GaussianBlur(thresh, (3,3), 0)
thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 4)
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(90 + angle)
else:
angle = -angle
(h, w) = thresh.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(thresh, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return rotated
def preprocess_image_high(image_file):
img = cv2.imread(image_file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Adaptive thresholding
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# Morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
opened = cv2.morphologyEx(closed, cv2.MORPH_OPEN, kernel)
# Connected Component Analysis (CCA)
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(opened)
filtered_labels = []
for i in range(1, num_labels):
# Filter out components based on their size, aspect ratio, and position
x, y, w, h, area = stats[i]
aspect_ratio = float(w) / h
if area > 100 and aspect_ratio < 5 and aspect_ratio > 0.2 and x > 10 and y > 10:
filtered_labels.append(i)
filtered = np.zeros_like(labels)
for i, label in enumerate(filtered_labels):
filtered[labels == label] = i + 1
# Skew correction
coords = np.column_stack(np.where(filtered > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(90 + angle)
else:
angle = -angle
(h, w) = filtered.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(filtered, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return rotated
|