options¶
Option-handling and configuration definitions for page_dewarp.
This package provides:
- A global config instance (
cfg) containing default parameters. - The
Configclass, which defines the structure and types of these parameters.
core ¶
Core configuration structures for page-dewarp.
Defines:
- A helper function (
desc) that annotates msgspec.Struct fields with a description. - A global
Configclass specifying various parameters (camera, edge detection, etc.).
Config ¶
Bases:
Global configuration for page-dewarp.
Holds parameters controlling camera focal length, contour detection, output size, page margin, debug verbosity, etc.
Attributes:
| Name | Type | Description |
|---|---|---|
|
|
Maximum optimisation iterations. |
|
|
Name of the JAX/SciPy optimisation method to use. |
|
|
Normalized focal length of camera. |
|
|
Minimum reduced pixel width of detected text contour. |
|
|
Minimum reduced pixel height of detected text contour. |
|
|
Filter out text contours below this width/height ratio. |
|
|
Maximum reduced pixel thickness of detected text contour. |
|
|
Debug verbosity level (0 = none). |
|
|
Output mode for debug information ('file' by default). |
|
|
Maximum horizontal overlap of contours in a span. |
|
|
Maximum length of edges connecting contours. |
|
|
Cost of angles in edges (tradeoff vs length). |
|
|
Maximum allowed change in angle between contours. |
|
|
Viewing screen maximum width (for resizing to screen). |
|
|
Viewing screen maximum height (for resizing to screen). |
|
|
Pixels to ignore near left/right edge. |
|
|
Pixels to ignore near top/bottom edge. |
|
|
Window size for adaptive thresholding. |
|
|
Zoom factor for output relative to original image. |
|
|
Stated DPI of output PNG (does not affect appearance). |
|
|
Downscaling factor for remapping images. |
|
|
Disable output conversion to binary thresholded image. |
|
|
Penalty against camera tilt (shear distortion). |
|
|
Index slice of rotation vector in parameter vector. |
|
|
Index slice of translation vector in parameter vector. |
|
|
Index slice of cubic slopes in parameter vector. |
|
|
Minimum width of a span in reduced pixels. |
|
|
Pixel spacing for sampling along spans. |
OPT_MAX_ITER
class-attribute
instance-attribute
¶
Maximum optimisation iterations.
Tip
For a fast 'draft' preview, set this to a low value like 1 with -it 1.
Note
This value is passed as maxiter to JAX or
scipy.optimize.minimize,
which defaults to N*1000 where N is the number of parameter variables (in our case, 600).
OPT_METHOD
class-attribute
instance-attribute
¶
Name of the JAX/SciPy optimisation method to use.
JAX supports L-BFGS-B only (its default). It is typically several times faster than Powell's method (SciPy's default), and more accurate than SciPy's L-BFGS-B.
Tip
Install the jax Python package to use JAX reverse-mode autodifferentiation to
produce gradients for L-BFGS-B (recommended). It is much faster than Powell's
method with SciPy, typically with far fewer function evaluations and a better
result.
In SciPy, Powell's method is slower than methods like L-BFGS-B, but it avoids local minima better in high-dimensional parameter spaces because SciPy's gradients are lower quality so produce worse optimisations when used by gradient methods like L-BFGS-B.
Note
This name is passed as method to
scipy.optimize.minimize,
and defaults to "Powell" if unset.
All options:
- Nelder-Mead
- Powell
- CG
- BFGS
- Newton-CG
- L-BFGS-B
- TNC
- COBYLA
- COBYQA
- SLSQP
- trust-const
- dogleg
- trust-ncg
- trust-exact
- trust-krylov
FOCAL_LENGTH
class-attribute
instance-attribute
¶
Normalized focal length of camera.
TEXT_MIN_WIDTH
class-attribute
instance-attribute
¶
Min reduced px width of detected text contour.
Contours narrower than this are filtered out.
Tip
Decrease for small text, increase to filter out noise.
Question
#78 - Discussion of text detection robustness
TEXT_MIN_HEIGHT
class-attribute
instance-attribute
¶
Min reduced px height of detected text contour.
Contours shorter than this are filtered out.
Question
#78 - Discussion of text detection robustness
TEXT_MIN_ASPECT
class-attribute
instance-attribute
¶
Filter out text contours below this w/h ratio.
Note
Text is typically wider than tall, so this filters vertical artifacts. Decrease for languages with tall characters or rotated text.
Question
#78 - Discussion of text detection robustness
TEXT_MAX_THICKNESS
class-attribute
instance-attribute
¶
Max reduced px thickness of detected text contour.
Contours thicker than this are filtered out (likely not text).
Tip
For bold letters or close-up photos where letters are large, the morphological
smearing may not connect letters into word blobs effectively. Consider
adjusting this alongside TEXT_MIN_WIDTH.
Question
#78 - Discussion of text detection limitations with close-up photos.
EDGE_MAX_OVERLAP
class-attribute
instance-attribute
¶
Max reduced px horiz. overlap of contours in span.
EDGE_MAX_LENGTH
class-attribute
instance-attribute
¶
Max reduced px length of edge connecting contours.
EDGE_ANGLE_COST
class-attribute
instance-attribute
¶
Cost of angles in edges (tradeoff vs. length).
EDGE_MAX_ANGLE
class-attribute
instance-attribute
¶
Maximum change in angle allowed between contours.
SCREEN_MAX_W
class-attribute
instance-attribute
¶
Viewing screen max width (for resizing to screen).
SCREEN_MAX_H
class-attribute
instance-attribute
¶
Viewing screen max height (for resizing to screen).
PAGE_MARGIN_X
class-attribute
instance-attribute
¶
Reduced px to ignore near L/R edge.
Tip
Set to 0 when text extends to the page edges and you don't want
content cropped from the sides at all.
Question
#83: Dewarp failure example
using -x 0 -y 0
PAGE_MARGIN_Y
class-attribute
instance-attribute
¶
Reduced px to ignore near T/B edge.
Tip
Set to 0 when text extends to the top/bottom of the frame and you don't want
content cropped from either end.
Question
#83: Dewarp failure example
using -x 0 -y 0.
ADAPTIVE_WINSZ
class-attribute
instance-attribute
¶
Window size for adaptive threshold in reduced px.
Warning
Must be an odd number.
Tip
Increase this value when dealing with varying text sizes or when the default
threshold produces poor results. For example, -wz 105 resolved issues with
mixed text sizes in #48.
OUTPUT_ZOOM
class-attribute
instance-attribute
¶
How much to zoom output relative to original image.
Note
This controls output resolution, so 2.0 roughly (not exactly) doubles the size. For example in #19:
1=> 800 x 1248 px (default)2=> 1568 x 2480 px3=> 2352 x 3712 px
OUTPUT_DPI
class-attribute
instance-attribute
¶
Just affects stated DPI of PNG, not appearance.
REMAP_DECIMATE
class-attribute
instance-attribute
¶
Downscaling factor for remapping image.
NO_BINARY
class-attribute
instance-attribute
¶
Disable output conversion to binary thresholded image.
SHEAR_COST
class-attribute
instance-attribute
¶
Penalty against camera tilt (shear distortion).
Adds a penalty term to the optimization objective that discourages X-rotation, which manifests as sheared/slanted output.
Tip
Increase if output appears sheared (parallelogram instead of rectangle).
Note
The optimizer can mistake page curvature for camera tilt, producing sheared output even from flat scans. This penalty encourages modeling curvature via the cubic params instead of rotation.
Warning
Using this at all may overcorrect, causing non-parallel sides. If edges look worse after enabling (typically only mildly), reduce the value.
Question
#83: Discussion of shear distortion in flat document scans.
RVEC_IDX
class-attribute
instance-attribute
¶
Index of rvec in params vector (slice: pair of values).
TVEC_IDX
class-attribute
instance-attribute
¶
Index of tvec in params vector (slice: pair of values).
CUBIC_IDX
class-attribute
instance-attribute
¶
CUBIC_IDX: desc(tuple[int, int], 'Index of cubic slopes in params vector (slice: pair of values)') = (6, 8)
Index of cubic slopes in params vector (slice: pair of values).
Note
These parameters control the cubic spline dewarping model. The cubic slopes determine how the page curvature is estimated.
Todo
Document cubic param clamp control (#67)
SPAN_MIN_WIDTH
class-attribute
instance-attribute
¶
Minimum reduced px width for span.
SPAN_PX_PER_STEP
class-attribute
instance-attribute
¶
Reduced px spacing for sampling along spans.
desc ¶
Annotate a msgspec.Struct field with a description.
Returns an Annotated[typ, Meta(description=...)] for additional metadata.
k_opt ¶
Defines the default intrinsic camera matrix (K) using a configurable focal length.
This module exports a single function, K, which returns a 3x3 NumPy array
representing the camera's intrinsic matrix based on Config.FOCAL_LENGTH.
K ¶
Return the default intrinsic parameter matrix, derived from cfg.FOCAL_LENGTH.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
|
The configuration object, which includes |
required |
Returns:
| Type | Description |
|---|---|
|
A 3x3 NumPy array representing the intrinsic camera matrix. |