Class ImagePdfUtils
Utility helpers for working with PDF pages as images and converting between
AWT BufferedImage and OpenCV Mat types. These functions are
used by lattice/OCR flows where pages are rasterized and processed with OpenCV.
Design
- Rendering is delegated to PDFBox's
PDFRenderer. - Conversion to
Matis implemented via a temporary PNG round‑trip for broad compatibility with image types; this keeps logic simple and avoids assumptions about pixel formats. - Binarization uses OpenCV adaptive thresholding tuned for line detection (inverted binary for morphological line ops).
This class is non‑instantiable.
- Since:
- 2025
- Author:
- Mehuli Mukherjee
-
Method Summary
Modifier and TypeMethodDescriptionstatic org.bytedeco.opencv.opencv_core.MatbinarizeForLines(org.bytedeco.opencv.opencv_core.Mat gray) Binarizes a grayscale image for line detection using adaptive thresholding.static org.bytedeco.opencv.opencv_core.MatConverts aBufferedImageto an OpenCVMat(grayscale) via a temporary PNG encode/decode round‑trip.static BufferedImagerenderPage(org.apache.pdfbox.pdmodel.PDDocument doc, int pageIndexZero, float dpi) Renders a single PDF page to aBufferedImageat the requested DPI.
-
Method Details
-
renderPage
public static BufferedImage renderPage(org.apache.pdfbox.pdmodel.PDDocument doc, int pageIndexZero, float dpi) throws IOException Renders a single PDF page to aBufferedImageat the requested DPI.The output is rendered as
ImageType.BINARY, which is generally suitable for downstream line/edge detection. If grayscale or RGB is needed, change theImageTypeaccordingly in the implementation.- Parameters:
doc- an openPDDocumentpageIndexZero- zero‑based page index (0 = first page)dpi- dots per inch to render at (e.g., 300f for scans)- Returns:
- a binary
BufferedImageof the page - Throws:
IOException- if rendering fails
-
bufferedToMat
Converts aBufferedImageto an OpenCVMat(grayscale) via a temporary PNG encode/decode round‑trip.This method favors robustness over raw speed; it handles arbitrary
BufferedImageformats without manual channel/buffer shuffling. The temporary file is deleted on success; on failure, aRuntimeExceptionis thrown wrapping theIOException.- Parameters:
img- source buffered image (any color model)- Returns:
- an 8‑bit single‑channel
Mat(CV_8UC1)
-
binarizeForLines
public static org.bytedeco.opencv.opencv_core.Mat binarizeForLines(org.bytedeco.opencv.opencv_core.Mat gray) Binarizes a grayscale image for line detection using adaptive thresholding.Applies
adaptiveThresholdwithADAPTIVE_THRESH_MEAN_CandTHRESH_BINARY_INVto emphasize lines/edges against background noise. The block size (15) and constant (10) are conservative defaults for document scans; adjust as needed for different lighting/contrast.- Parameters:
gray- input grayscale image (CV_8UC1)- Returns:
- a binary (inverted)
Matsuitable for morphological line ops
-