contrast without blur

This commit is contained in:
千住柱間 2025-04-21 21:29:58 -04:00
commit 89bad82c66
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -124,29 +124,16 @@ def adjust_gamma(image, gamma=1.0, alpha=1.0, beta=0):
return adjusted
def enhance_text(image_path):
# Improve contrast using CLAHE
gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
contrast = clahe.apply(gray)
# Apply adaptive thresholding to emphasize text
binarized = cv2.adaptiveThreshold(
contrast, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
blockSize=11,
C=2
)
return binarized
def preprocess_image(image_path):
# Load image in grayscale (1-channel)
gray = enhance_text(image_path)
gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
resampled = bicubic(gray)
# Convert grayscale to 3-channel RGB by duplicating the gray channel
img = contrast_stretch(adjust_gamma(cv2.cvtColor(resampled, cv2.COLOR_GRAY2RGB),gamma=1.6,alpha=1.75,beta=-15)) # perfect tunning
img1 = contrast_stretch(adjust_gamma(cv2.cvtColor(resampled, cv2.COLOR_GRAY2RGB),gamma=1,alpha=1.4,beta=-5)) # perfect tunning
img = cv2.fastNlMeansDenoising(img1, None, 20, 10, 20)
plt.imshow(img)
plt.axis("off")
plt.savefig("/tmp/output.png", bbox_inches="tight", pad_inches=0)