我正在尝试预处理稍微倾斜和褪色的收据图像。
我找到了调整照片位置的代码。但我不知道如何使文本清晰。
import cv2
import pytesseract
import numpy as np
img_path = "rec.jpg"
img = cv2.imread(img_path)
cv2.imshow("org",img)
img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
cv2.imshow("rgb",img_rgb)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = 255 - gray
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
# Compute rotated bounding box
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(90 + angle)
else:
angle = -angle
print(angle)
# Rotate image to deskew
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)[enter image description here]
rotated = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
cv2.imshow('thresh', thresh)
cv2.imshow('rotated', rotated)
bit_img = cv2.bitwise_not(rotated)
kernel = np.ones((2,2),np.uint8)
dilate_img = cv2.dilate(bit_img,kernel,iterations=1)
cv2.imshow("last",dilate_img)
cv2.waitKey(0)
尽管我用opencv尝试了各种代码,但准确率没有达到我想要的水平。
import cv2
import numpy as np
img_path = "rec.jpg"
img = cv2.imread(img_path)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Contrast enhancement using Adaptive Histogram Equalization
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
cl1 = clahe.apply(gray)
# Bilateral filtering
filtered = cv2.bilateralFilter(cl1, 9, 75, 75)
# Sharpening
kernel_sharpening = np.array([[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]])
sharpened = cv2.filter2D(filtered, -1, kernel_sharpening)
# Adaptive thresholding
thresh = cv2.adaptiveThreshold(sharpened, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)
cv2.imshow('Processed Image', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()