Skip to content
TheChatArchiveThe Chat Archive
GPT 5.1 Codex Max· openaiHugging Face Frontier Reasoning Traces

Implement OCR with Tesseract using OpenCV preprocessing. Apply adaptive thresholding and…

  1. User

    Implement OCR with Tesseract using OpenCV preprocessing. Apply adaptive thresholding and deskewing to correct rotation before passing to Tesseract. Output results as HOCR preserving layout positions.

  2. Assistant

    Providing OCR with OpenCV preprocessing and HOCR output

    The implementation loads an image, converts it to grayscale, applies adaptive thresholding with denoising, deskews it by computing the rotation angle from the text region, and then uses Tesseract to output HOCR preserving layout positions. The code includes a deskew helper function using minAreaRect and calls pytesseract.image_to_pdf_or_hocr with proper config to generate HOCR output. Here is a minimal end‑to‑end example in Python that:

    • loads an input image with OpenCV,
    • converts to grayscale and denoises,
    • binarises with adaptive thresholding,
    • estimates and corrects the skew angle,
    • passes the corrected image to Tesseract, and
    • saves the HOCR output so you get the recognised text along with bounding boxes/layout information.
    import cv2
    import numpy as np
    import pytesseract
    
    # helper: estimate skew angle and rotate to deskew
    def deskew(binary_img):
        # find coordinates of all non-zero (white) pixels
        coords = np.column_stack(np.where(binary_img > 0))
        rect = cv2.minAreaRect(coords)
        angle = rect[-1]
        # the `minAreaRect` angle is in [-90, 0); adjust to deskew properly
        if angle < -45:
            angle = -(90 + angle)
        else:
            angle = -angle
        (h, w) = binary_img.shape[:2]
        center = (w // 2, h // 2)
        M = cv2.getRotationMatrix2D(center, angle, 1.0)
        rotated = cv2.warpAffine(binary_img, M, (w, h),
                                 flags=cv2.INTER_CUBIC,
                                 borderMode=cv2.BORDER_REPLICATE)
        return rotated
    
    # load and preprocess
    img = cv2.imread('input.png')  # or .jpg, etc.
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # optional: denoise
    gray = cv2.medianBlur(gray, 3)
    
    # adaptive thresholding to handle uneven illumination
    thresh = cv2.adaptiveThreshold(gray, 255,
                                   cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                   cv2.THRESH_BINARY_INV, 31, 15)
    
    # deskew the binarised image
    deskewed = deskew(thresh)
    
    # invert back if needed because we used THRESH_BINARY_INV
    deskewed = 255 - deskewed
    
    # run Tesseract to get HOCR output (layout preserved)
    # ensure tesseract is installed and pytesseract is configured to find it
    config = r'--oem 3 --psm 1'  # OEM 3 = default LSTM, PSM 1 = automatic page segmentation
    hocr_bytes = pytesseract.image_to_pdf_or_hocr(deskewed, extension='hocr', config=config)
    
    with open('output.hocr', 'wb') as f:
        f.write(hocr_bytes)
    
    print("HOCR saved to output.hocr")
    

    A few notes:

    • Adjust the adaptive thresholding parameters (blockSize and C in adaptiveThreshold) to suit your documents.
    • If your documents have dark text on a light background, THRESH_BINARY_INV followed by an inversion (255 - ...) is often useful because Tesseract prefers dark text on white.
    • The --psm value controls page segmentation; for single columns, receipts, etc., different values might yield better results.
    • The image_to_pdf_or_hocr call with extension='hocr' instructs Tesseract to output HOCR HTML with bounding boxes, which preserves layout positions.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.