JavaCPP bindings for Leptonica image processing library with cross-platform support
—
Rectangle and point operations for defining regions, measuring features, and spatial analysis with comprehensive support for geometric calculations and transformations.
Define and manipulate rectangular regions for clipping, analysis, and region-of-interest operations.
/**
* Rectangle structure for defining image regions
*/
class BOX extends Pointer {
// Rectangle properties
int x(); // left coordinate
int y(); // top coordinate
int w(); // width
int h(); // height
int refcount(); // reference count for memory management
// Setters
BOX x(int x);
BOX y(int y);
BOX w(int w);
BOX h(int h);
}
/**
* Create rectangle
* @param x - Left coordinate
* @param y - Top coordinate
* @param w - Width
* @param h - Height
* @return BOX rectangle or null on failure
*/
BOX boxCreate(int x, int y, int w, int h);
/**
* Copy rectangle
* @param box - Source rectangle
* @return BOX copy or null on failure
*/
BOX boxCopy(BOX box);
/**
* Get rectangle geometry
* @param box - Source rectangle
* @param px - Returns x coordinate (can be null)
* @param py - Returns y coordinate (can be null)
* @param pw - Returns width (can be null)
* @param ph - Returns height (can be null)
* @return 0 on success, 1 on failure
*/
int boxGetGeometry(BOX box, IntPointer px, IntPointer py, IntPointer pw, IntPointer ph);
/**
* Set rectangle geometry
* @param box - Target rectangle
* @param x - Left coordinate
* @param y - Top coordinate
* @param w - Width
* @param h - Height
* @return 0 on success, 1 on failure
*/
int boxSetGeometry(BOX box, int x, int y, int w, int h);
/**
* Rectangle intersection
* @param box1 - First rectangle
* @param box2 - Second rectangle
* @return BOX intersection or null if no overlap
*/
BOX boxIntersection(BOX box1, BOX box2);
/**
* Rectangle union (bounding box)
* @param box1 - First rectangle
* @param box2 - Second rectangle
* @return BOX union or null on failure
*/
BOX boxBoundingRegion(BOX box1, BOX box2);
/**
* Check if rectangles overlap
* @param box1 - First rectangle
* @param box2 - Second rectangle
* @param presult - Returns 1 if overlap, 0 otherwise
* @return 0 on success, 1 on failure
*/
int boxOverlapRegion(BOX box1, BOX box2, IntPointer presult);
/**
* Check if point is inside rectangle
* @param box - Rectangle
* @param x - Point x coordinate
* @param y - Point y coordinate
* @param presult - Returns 1 if inside, 0 otherwise
* @return 0 on success, 1 on failure
*/
int boxContainsPt(BOX box, float x, float y, IntPointer presult);Usage Examples:
import org.bytedeco.leptonica.*;
import static org.bytedeco.leptonica.global.leptonica.*;
// Create rectangle for region of interest
BOX roi = boxCreate(100, 50, 200, 150);
// Get rectangle properties
IntPointer x = new IntPointer(1);
IntPointer y = new IntPointer(1);
IntPointer w = new IntPointer(1);
IntPointer h = new IntPointer(1);
boxGetGeometry(roi, x, y, w, h);
System.out.println("ROI: " + x.get() + "," + y.get() + " " + w.get() + "x" + h.get());
// Check intersection
BOX region1 = boxCreate(0, 0, 100, 100);
BOX region2 = boxCreate(50, 50, 100, 100);
BOX overlap = boxIntersection(region1, region2);
if (overlap != null) {
System.out.println("Regions overlap");
}
// Check if point is inside rectangle
IntPointer inside = new IntPointer(1);
boxContainsPt(roi, 150, 100, inside);
if (inside.get() == 1) {
System.out.println("Point is inside ROI");
}
// Extract image region
PIX clipped = pixClipRectangle(sourcePix, roi, null);Manage collections of rectangles for multi-region operations and analysis.
/**
* Array of BOX rectangles
*/
class BOXA extends Pointer {
int n(); // number of boxes
int nalloc(); // allocated array size
}
/**
* Create rectangle array
* @param n - Initial capacity
* @return BOXA array or null on failure
*/
BOXA boxaCreate(int n);
/**
* Add rectangle to array
* @param boxa - Target array
* @param box - Rectangle to add
* @param copyflag - L_INSERT or L_COPY
* @return 0 on success, 1 on failure
*/
int boxaAddBox(BOXA boxa, BOX box, int copyflag);
/**
* Get rectangle from array
* @param boxa - Source array
* @param index - Rectangle index
* @param accessflag - L_COPY or L_CLONE
* @return BOX rectangle or null on failure
*/
BOX boxaGetBox(BOXA boxa, int index, int accessflag);
/**
* Get number of rectangles
* @param boxa - Source array
* @return Number of rectangles
*/
int boxaGetCount(BOXA boxa);
/**
* Get bounding box of all rectangles
* @param boxa - Source array
* @return BOX bounding box or null on failure
*/
BOX boxaBinSort(BOXA boxa, int sorttype, int sortorder, NUMA naindex);
/**
* Sort rectangles by various criteria
* @param boxas - Source array
* @param sorttype - Sort criteria (L_SORT_BY_*)
* @param sortorder - L_SORT_INCREASING or L_SORT_DECREASING
* @param pnaindex - Returns sort indices (can be null)
* @return Sorted BOXA or null on failure
*/
BOXA boxaSort(BOXA boxas, int sorttype, int sortorder, NUMA naindex);Usage Examples:
// Create array for multiple regions
BOXA regions = boxaCreate(10);
// Add rectangles
boxaAddBox(regions, boxCreate(0, 0, 100, 100), L_COPY);
boxaAddBox(regions, boxCreate(200, 150, 150, 200), L_COPY);
boxaAddBox(regions, boxCreate(400, 50, 120, 180), L_COPY);
// Process each region
int count = boxaGetCount(regions);
for (int i = 0; i < count; i++) {
BOX box = boxaGetBox(regions, i, L_CLONE);
PIX region = pixClipRectangle(sourcePix, box, null);
// Process region...
System.out.println("Processing region " + i);
}
// Sort rectangles by area
BOXA sorted = boxaSort(regions, L_SORT_BY_AREA, L_SORT_DECREASING, null);Manage arrays of 2D points for geometric calculations, curve fitting, and spatial analysis.
/**
* Array of 2D points
*/
class PTA extends Pointer {
int n(); // number of points
int nalloc(); // allocated array size
}
/**
* Create point array
* @param n - Initial capacity
* @return PTA array or null on failure
*/
PTA ptaCreate(int n);
/**
* Add point to array
* @param pta - Target array
* @param x - X coordinate
* @param y - Y coordinate
* @return 0 on success, 1 on failure
*/
int ptaAddPt(PTA pta, float x, float y);
/**
* Get point from array
* @param pta - Source array
* @param index - Point index
* @param px - Returns x coordinate
* @param py - Returns y coordinate
* @return 0 on success, 1 on failure
*/
int ptaGetPt(PTA pta, int index, FloatPointer px, FloatPointer py);
/**
* Get number of points
* @param pta - Source array
* @return Number of points
*/
int ptaGetCount(PTA pta);
/**
* Get bounding box of all points
* @param pta - Source array
* @param pminx - Returns minimum x (can be null)
* @param pmaxx - Returns maximum x (can be null)
* @param pminy - Returns minimum y (can be null)
* @param pmaxy - Returns maximum y (can be null)
* @return 0 on success, 1 on failure
*/
int ptaGetRange(PTA pta, FloatPointer pminx, FloatPointer pmaxx, FloatPointer pminy, FloatPointer pmaxy);
/**
* Linear least squares fit to points
* @param pta - Source points
* @param pa - Returns slope coefficient
* @param pb - Returns intercept coefficient
* @param pnafit - Returns fitted points (can be null)
* @param ppixplot - Returns plot image (can be null)
* @return 0 on success, 1 on failure
*/
int ptaGetLinearLSF(PTA pta, FloatPointer pa, FloatPointer pb, NUMA nafit, PIX pixplot);
/**
* Quadratic least squares fit
* @param pta - Source points
* @param pa - Returns x² coefficient
* @param pb - Returns x coefficient
* @param pc - Returns constant coefficient
* @param pnafit - Returns fitted points (can be null)
* @param ppixplot - Returns plot image (can be null)
* @return 0 on success, 1 on failure
*/
int ptaGetQuadraticLSF(PTA pta, FloatPointer pa, FloatPointer pb, FloatPointer pc, NUMA nafit, PIX pixplot);Usage Examples:
// Create point array for curve fitting
PTA points = ptaCreate(20);
// Add sample points (e.g., from edge detection)
ptaAddPt(points, 10.0f, 15.2f);
ptaAddPt(points, 20.0f, 25.1f);
ptaAddPt(points, 30.0f, 35.8f);
ptaAddPt(points, 40.0f, 44.9f);
// Fit linear model: y = ax + b
FloatPointer slope = new FloatPointer(1);
FloatPointer intercept = new FloatPointer(1);
int result = ptaGetLinearLSF(points, slope, intercept, null, null);
if (result == 0) {
System.out.println("Line: y = " + slope.get() + "x + " + intercept.get());
}
// Get point range for normalization
FloatPointer minx = new FloatPointer(1);
FloatPointer maxx = new FloatPointer(1);
FloatPointer miny = new FloatPointer(1);
FloatPointer maxy = new FloatPointer(1);
ptaGetRange(points, minx, maxx, miny, maxy);
// Access individual points
int count = ptaGetCount(points);
for (int i = 0; i < count; i++) {
FloatPointer x = new FloatPointer(1);
FloatPointer y = new FloatPointer(1);
ptaGetPt(points, i, x, y);
System.out.println("Point " + i + ": (" + x.get() + ", " + y.get() + ")");
}Utility functions for geometric calculations and spatial analysis.
/**
* Calculate distance between two points
* @param x1 - First point x coordinate
* @param y1 - First point y coordinate
* @param x2 - Second point x coordinate
* @param y2 - Second point y coordinate
* @return Distance between points
*/
float l_distancePointToPoint(float x1, float y1, float x2, float y2);
/**
* Calculate distance from point to line
* @param x - Point x coordinate
* @param y - Point y coordinate
* @param a - Line slope coefficient
* @param b - Line intercept coefficient
* @return Distance from point to line
*/
float l_distancePointToLine(float x, float y, float a, float b);
/**
* Check if point is on line within tolerance
* @param x - Point x coordinate
* @param y - Point y coordinate
* @param a - Line slope coefficient
* @param b - Line intercept coefficient
* @param dist - Distance tolerance
* @param presult - Returns 1 if on line, 0 otherwise
* @return 0 on success, 1 on failure
*/
int l_isPointOnLine(float x, float y, float a, float b, float dist, IntPointer presult);
/**
* Find intersection of two lines
* @param a1 - First line slope
* @param b1 - First line intercept
* @param a2 - Second line slope
* @param b2 - Second line intercept
* @param px - Returns intersection x coordinate
* @param py - Returns intersection y coordinate
* @return 0 on success, 1 if lines are parallel
*/
int l_getIntersectionOfLines(float a1, float b1, float a2, float b2, FloatPointer px, FloatPointer py);Usage Examples:
// Calculate distance between two detected features
float distance = l_distancePointToPoint(100.0f, 200.0f, 300.0f, 150.0f);
System.out.println("Feature distance: " + distance + " pixels");
// Check if point lies on fitted line
FloatPointer onLine = new IntPointer(1);
l_isPointOnLine(150.0f, 175.0f, 1.2f, 25.0f, 5.0f, onLine);
if (onLine.get() == 1) {
System.out.println("Point is on the line");
}
// Find intersection of two lines
FloatPointer intersectX = new FloatPointer(1);
FloatPointer intersectY = new FloatPointer(1);
int intersects = l_getIntersectionOfLines(1.0f, 0.0f, -1.0f, 200.0f, intersectX, intersectY);
if (intersects == 0) {
System.out.println("Lines intersect at: (" + intersectX.get() + ", " + intersectY.get() + ")");
}// Copy/access flags
static final int L_COPY = 0;
static final int L_CLONE = 1;
static final int L_INSERT = 2;
// Sort criteria for boxes
static final int L_SORT_BY_X = 1;
static final int L_SORT_BY_Y = 2;
static final int L_SORT_BY_WIDTH = 3;
static final int L_SORT_BY_HEIGHT = 4;
static final int L_SORT_BY_AREA = 5;
static final int L_SORT_BY_PERIMETER = 6;
// Sort orders
static final int L_SORT_INCREASING = 1;
static final int L_SORT_DECREASING = 2;// Define ROI and extract for processing
BOX roi = boxCreate(x, y, width, height);
PIX region = pixClipRectangle(image, roi, null);
PIX processed = pixGaussianBlur(region, 2.0f, 2.0f);// Process multiple detected regions
BOXA detections = detectObjects(image); // hypothetical function
int count = boxaGetCount(detections);
for (int i = 0; i < count; i++) {
BOX box = boxaGetBox(detections, i, L_CLONE);
PIX object = pixClipRectangle(image, box, null);
// Analyze object...
}// Fit curve to edge points
PTA edgePoints = extractEdgePoints(image); // hypothetical function
FloatPointer a = new FloatPointer(1);
FloatPointer b = new FloatPointer(1);
FloatPointer c = new FloatPointer(1);
ptaGetQuadraticLSF(edgePoints, a, b, c, null, null);Install with Tessl CLI
npx tessl i tessl/maven-org-bytedeco--leptonica-platform