How to preprocess black text on a cream background for Tesseract using OpenCV?

77 Views Asked by At

I am looking to extract text from this image: screenshot

Specifically the row under "Kills". However I cannot seem to get accurate results.

I tried to convert the image to gray and apply a threshhold:

import { createWorker, OEM, PSM } from "tesseract.js";
import cv from "@u4/opencv4nodejs";
import fs from "node:fs/promises";

const worker = await createWorker("eng", OEM.TESSERACT_LSTM_COMBINED);

await options.worker.setParameters({
  tessedit_char_whitelist: "0123456789",
  tessedit_pageseg_mode: PSM.SINGLE_BLOCK,
});

const image = await cv.imdecodeAsync(
  await fs.readFile("input.png"),
  cv.COLOR_BGR2GRAY
);

const threshHoldedImage =
  await image.thresholdAsync(
    150,
    255,
    cv.THRESH_BINARY
  );

const blurredImage = await cv.imencodeAsync(".png", threshHoldedImage);

const {
  data: { text: tierKillsText },
} = await options.worker.recognize(blurredImage, {
  rectangle: {
    top: 265,
    left: 552,
    width: 87,
    height: 138,
  },
});

console.log(tierKillsText);
// Received: 3228387
// Expected: 3328387

I have also tried to apply a gaussian blur without success:

const sigma = 0.75;
const blurred = threshHoldedImage.gaussianBlur(new cv.Size(0, 0), sigma);
2

There are 2 best solutions below

0
Daniell On BEST ANSWER

I fixed it by reading out each line individually which seems to lead to more accurate results

1
Saradamani On

Will you try this and see please? Worked for me.

import cv2
import numpy as np
from pytesseract import pytesseract

# Since I can't access the filesystem, I'll be using the uploaded image
image_path = 'C:/Users/Chirantan.Gupta/Downloads/image.png'

# Read the image
image = cv2.imread(image_path)

# Make sure the image has been read properly
if image is None:
    raise ValueError(f"The image at path {image_path} could not be loaded. Please check the file path.")

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Gaussian blur
blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# Use adaptive thresholding
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                               cv2.THRESH_BINARY_INV, 11, 2)

# Dilate the text to make it more solid
kernel = np.ones((2, 2), np.uint8)
dilated = cv2.dilate(thresh, kernel, iterations=1)


# text = pytesseract.image_to_string(dilated, config=config)## Set config accordingly

# For demonstration purposes, let's save the processed image to check the preprocessing steps
cv2.imwrite('/mnt/data/processed_image.png', dilated)

'C:/Users/Chirantan.Gupta/Downloads/processed_image.png'

Result