JavaCPP bindings for Leptonica image processing library with cross-platform support
—
Advanced morphological processing with custom structuring elements for shape analysis, feature extraction, and binary image operations.
Define and create structuring elements for morphological operations with various shapes and orientations.
/**
* Structuring element for morphological operations
*/
class SEL extends Pointer {
int sy(); // height of structuring element
int sx(); // width of structuring element
int cy(); // y center coordinate
int cx(); // x center coordinate
BytePointer name(); // optional name
}
/**
* Create rectangular structuring element
* @param h - Height
* @param w - Width
* @param cy - Center y coordinate
* @param cx - Center x coordinate
* @return SEL structuring element or null on failure
*/
SEL selCreateBrick(int h, int w, int cy, int cx);
/**
* Create structuring element from string pattern
* @param text - Text pattern (e.g., "ooo;oxo;ooo")
* @param h - Height
* @param w - Width
* @param name - Optional name
* @return SEL structuring element or null on failure
*/
SEL selCreateFromString(String text, int h, int w, String name);
/**
* Create circular structuring element
* @param r - Radius
* @return SEL structuring element or null on failure
*/
SEL selCreateCircular(int r);
/**
* Create plus-shaped structuring element
* @param r - Radius
* @return SEL structuring element or null on failure
*/
SEL selCreatePlus(int r);
/**
* Rotate structuring element
* @param sel - Source structuring element
* @param angle - Rotation angle in radians
* @return Rotated SEL or null on failure
*/
SEL selRotate(SEL sel, float angle);Core morphological operations: dilation, erosion, opening, and closing for shape analysis and noise removal.
/**
* Dilate binary image (expand foreground)
* @param pixd - Destination (can be null)
* @param pixs - Source binary image
* @param sel - Structuring element
* @return Dilated PIX or null on failure
*/
PIX pixDilate(PIX pixd, PIX pixs, SEL sel);
/**
* Erode binary image (shrink foreground)
* @param pixd - Destination (can be null)
* @param pixs - Source binary image
* @param sel - Structuring element
* @return Eroded PIX or null on failure
*/
PIX pixErode(PIX pixd, PIX pixs, SEL sel);
/**
* Opening operation (erosion followed by dilation)
* @param pixd - Destination (can be null)
* @param pixs - Source binary image
* @param sel - Structuring element
* @return Opened PIX or null on failure
*/
PIX pixOpen(PIX pixd, PIX pixs, SEL sel);
/**
* Closing operation (dilation followed by erosion)
* @param pixd - Destination (can be null)
* @param pixs - Source binary image
* @param sel - Structuring element
* @return Closed PIX or null on failure
*/
PIX pixClose(PIX pixd, PIX pixs, SEL sel);
/**
* Safe closing (handles boundary conditions)
* @param pixs - Source binary image
* @param size - Structuring element size
* @return Safely closed PIX or null on failure
*/
PIX pixCloseSafe(PIX pixs, int size);
/**
* Safe opening (handles boundary conditions)
* @param pixs - Source binary image
* @param size - Structuring element size
* @return Safely opened PIX or null on failure
*/
PIX pixOpenSafe(PIX pixs, int size);Sophisticated operations for gradient computation, feature extraction, and pattern matching.
/**
* Morphological gradient (dilation - erosion)
* @param pixs - Source binary image
* @param sel - Structuring element
* @param op - Operation type (L_MORPH_DILATE or L_MORPH_ERODE)
* @return Gradient PIX or null on failure
*/
PIX pixMorphGradient(PIX pixs, SEL sel, int op);
/**
* Top-hat transform (original - opening)
* @param pixs - Source binary image
* @param sel - Structuring element
* @param op - L_TOPHAT_WHITE or L_TOPHAT_BLACK
* @return Top-hat PIX or null on failure
*/
PIX pixTophat(PIX pixs, SEL sel, int op);
/**
* Hit-or-miss transform for pattern matching
* @param pixd - Destination (can be null)
* @param pixs - Source binary image
* @param sel - Structuring element with hit/miss pattern
* @return HMT result PIX or null on failure
*/
PIX pixHMT(PIX pixd, PIX pixs, SEL sel);
/**
* Thinning operation
* @param pixs - Source binary image
* @param connectivity - 4 or 8 connectivity
* @param maxiters - Maximum iterations (0 for convergence)
* @return Thinned PIX or null on failure
*/
PIX pixThin(PIX pixs, int connectivity, int maxiters);
/**
* Thickening operation
* @param pixs - Source binary image
* @param connectivity - 4 or 8 connectivity
* @param maxiters - Maximum iterations (0 for convergence)
* @return Thickened PIX or null on failure
*/
PIX pixThicken(PIX pixs, int connectivity, int maxiters);
/**
* Skeletonization (medial axis transform)
* @param pixs - Source binary image
* @param connectivity - 4 or 8 connectivity
* @return Skeleton PIX or null on failure
*/
PIX pixSkeleton(PIX pixs, int connectivity);Usage Examples:
import org.bytedeco.leptonica.*;
import static org.bytedeco.leptonica.global.leptonica.*;
// Create structuring elements
SEL brick = selCreateBrick(5, 5, 2, 2); // 5x5 rectangular
SEL circle = selCreateCircular(3); // radius 3 circular
SEL plus = selCreatePlus(2); // plus shape radius 2
// Basic morphological operations
PIX dilated = pixDilate(null, binaryImage, brick);
PIX eroded = pixErode(null, binaryImage, brick);
PIX opened = pixOpen(null, binaryImage, circle);
PIX closed = pixClose(null, binaryImage, circle);
// Remove small noise with opening
PIX denoised = pixOpenSafe(binaryImage, 3);
// Fill small gaps with closing
PIX filled = pixCloseSafe(binaryImage, 5);
// Extract boundaries with gradient
PIX boundaries = pixMorphGradient(binaryImage, brick, L_MORPH_DILATE);
// Extract fine features with top-hat
PIX features = pixTophat(binaryImage, circle, L_TOPHAT_WHITE);
// Skeletonize for shape analysis
PIX skeleton = pixSkeleton(binaryImage, 8);
// Thin lines to single pixel width
PIX thinned = pixThin(binaryImage, 8, 0);Morphological operations extended to grayscale images for texture analysis and filtering.
/**
* Grayscale dilation
* @param pixd - Destination (can be null)
* @param pixs - Source grayscale image
* @param sel - Structuring element
* @return Dilated PIX or null on failure
*/
PIX pixDilateGray(PIX pixd, PIX pixs, SEL sel);
/**
* Grayscale erosion
* @param pixd - Destination (can be null)
* @param pixs - Source grayscale image
* @param sel - Structuring element
* @return Eroded PIX or null on failure
*/
PIX pixErodeGray(PIX pixd, PIX pixs, SEL sel);
/**
* Grayscale opening
* @param pixs - Source grayscale image
* @param sel - Structuring element
* @return Opened PIX or null on failure
*/
PIX pixOpenGray(PIX pixs, SEL sel);
/**
* Grayscale closing
* @param pixs - Source grayscale image
* @param sel - Structuring element
* @return Closed PIX or null on failure
*/
PIX pixCloseGray(PIX pixs, SEL sel);
/**
* Grayscale top-hat transform
* @param pixs - Source grayscale image
* @param sel - Structuring element
* @param op - L_TOPHAT_WHITE or L_TOPHAT_BLACK
* @return Top-hat PIX or null on failure
*/
PIX pixTophatGray(PIX pixs, SEL sel, int op);Usage Examples:
// Grayscale morphological operations for texture processing
SEL disk = selCreateCircular(4);
// Enhance bright features
PIX brightFeatures = pixDilateGray(null, grayImage, disk);
// Suppress bright features
PIX darkFeatures = pixErodeGray(null, grayImage, disk);
// Smooth texture while preserving edges
PIX smoothed = pixOpenGray(grayImage, disk);
// Extract bright peaks
PIX peaks = pixTophatGray(grayImage, disk, L_TOPHAT_WHITE);
// Extract dark valleys
PIX valleys = pixTophatGray(grayImage, disk, L_TOPHAT_BLACK);Morphological operations on grayscale images for advanced image processing and analysis.
/**
* Grayscale erosion
* @param pixs - Source grayscale image
* @param hsize - Horizontal structuring element size
* @param vsize - Vertical structuring element size
* @return Eroded grayscale PIX or null on failure
*/
PIX pixErodeGray(PIX pixs, int hsize, int vsize);
/**
* Grayscale dilation
* @param pixs - Source grayscale image
* @param hsize - Horizontal structuring element size
* @param vsize - Vertical structuring element size
* @return Dilated grayscale PIX or null on failure
*/
PIX pixDilateGray(PIX pixs, int hsize, int vsize);
/**
* Grayscale opening
* @param pixs - Source grayscale image
* @param hsize - Horizontal size
* @param vsize - Vertical size
* @return Opened grayscale PIX or null on failure
*/
PIX pixOpenGray(PIX pixs, int hsize, int vsize);
/**
* Grayscale closing
* @param pixs - Source grayscale image
* @param hsize - Horizontal size
* @param vsize - Vertical size
* @return Closed grayscale PIX or null on failure
*/
PIX pixCloseGray(PIX pixs, int hsize, int vsize);Usage Examples:
// Grayscale morphological operations for texture analysis
PIX grayImage = pixRead("texture.jpg");
// Smooth bright features (erosion)
PIX eroded = pixErodeGray(grayImage, 5, 5);
// Enhance bright features (dilation)
PIX dilated = pixDilateGray(grayImage, 5, 5);
// Remove bright noise (opening)
PIX opened = pixOpenGray(grayImage, 3, 3);
// Fill dark gaps (closing)
PIX closed = pixCloseGray(grayImage, 3, 3);Specialized morphological operations for pattern detection and template matching.
/**
* Hit-or-Miss transform for pattern detection
* @param pixd - Destination (can be null)
* @param pixs - Source binary image
* @param sel - Structuring element with hit/miss pattern
* @return HMT result PIX or null on failure
*/
PIX pixHMT(PIX pixd, PIX pixs, SEL sel);
/**
* Top-hat transform (morphological gradient)
* @param pixs - Source image
* @param hsize - Horizontal size
* @param vsize - Vertical size
* @param type - L_TOPHAT_WHITE or L_TOPHAT_BLACK
* @return Top-hat PIX or null on failure
*/
PIX pixTophat(PIX pixs, int hsize, int vsize, int type);
/**
* Morphological gradient
* @param pixs - Source image
* @param hsize - Horizontal size
* @param vsize - Vertical size
* @param smoothing - Smoothing factor
* @return Gradient PIX or null on failure
*/
PIX pixMorphGradient(PIX pixs, int hsize, int vsize, int smoothing);Usage Examples:
// Pattern detection with Hit-or-Miss
SEL cornerSel = selCreateFromString("ox.;oxx;...", 3, 3, "corner");
PIX corners = pixHMT(null, binaryImage, cornerSel);
// White top-hat to detect bright features smaller than structuring element
PIX brightFeatures = pixTophat(grayImage, 10, 10, L_TOPHAT_WHITE);
// Black top-hat to detect dark features
PIX darkFeatures = pixTophat(grayImage, 10, 10, L_TOPHAT_BLACK);
// Morphological gradient for edge detection
PIX edges = pixMorphGradient(grayImage, 3, 3, 1);Advanced algorithms for shape analysis and region segmentation.
/**
* Distance transform (distance to nearest background pixel)
* @param pixs - Source binary image
* @param connectivity - 4 or 8 connectivity
* @return Distance transform PIX or null on failure
*/
PIX pixDistanceFunction(PIX pixs, int connectivity);
/**
* Watershed segmentation data structure
*/
class L_WSHED extends Pointer {
PIX pixs(); // source image
PIX pixm(); // marker image
PIX pixlab(); // labeled watershed regions
int nseeds(); // number of seed points
}
/**
* Create watershed segmentation structure
* @param pixs - Source grayscale image (typically gradient)
* @param pixm - Marker image (seed points)
* @return L_WSHED structure or null on failure
*/
L_WSHED wshedCreate(PIX pixs, PIX pixm);
/**
* Apply watershed algorithm
* @param wshed - Watershed structure
* @return 0 on success, 1 on failure
*/
int wshedApply(L_WSHED wshed);
/**
* Get watershed basins
* @param wshed - Watershed structure
* @return PIX with labeled regions or null on failure
*/
PIX wshedBasins(L_WSHED wshed);Usage Examples:
// Distance transform for shape analysis
PIX distance = pixDistanceFunction(binaryImage, 8);
// Watershed segmentation
PIX gradient = pixSobelEdgeFilter(grayImage, L_ALL_EDGES);
PIX markers = createMarkers(grayImage); // hypothetical function
L_WSHED watershed = wshedCreate(gradient, markers);
wshedApply(watershed);
PIX regions = wshedBasins(watershed);Reconstruction operations for connected component processing and feature extraction.
/**
* Morphological reconstruction by dilation
* @param pixs - Source image (mask)
* @param pixm - Marker image
* @param connectivity - 4 or 8 connectivity
* @return Reconstructed PIX or null on failure
*/
PIX pixMorphReconstructByDilation(PIX pixs, PIX pixm, int connectivity);
/**
* Morphological reconstruction by erosion
* @param pixs - Source image (mask)
* @param pixm - Marker image
* @param connectivity - 4 or 8 connectivity
* @return Reconstructed PIX or null on failure
*/
PIX pixMorphReconstructByErosion(PIX pixs, PIX pixm, int connectivity);
/**
* Hole filling using reconstruction
* @param pixs - Source binary image
* @param connectivity - 4 or 8 connectivity
* @return PIX with holes filled or null on failure
*/
PIX pixFillHoles(PIX pixs, int connectivity);
/**
* Remove border-connected components
* @param pixs - Source binary image
* @param connectivity - 4 or 8 connectivity
* @return PIX with border components removed or null on failure
*/
PIX pixRemoveBorderConnComps(PIX pixs, int connectivity);Usage Examples:
// Fill holes in binary image
PIX filled = pixFillHoles(binaryImage, 4);
// Remove components touching border
PIX interior = pixRemoveBorderConnComps(binaryImage, 8);
// Reconstruction-based filtering
PIX marker = pixErode(null, binaryImage, selCreateBrick(3, 3, 1, 1));
PIX reconstructed = pixMorphReconstructByDilation(binaryImage, marker, 8);// Operation types
static final int L_MORPH_DILATE = 1;
static final int L_MORPH_ERODE = 2;
static final int L_MORPH_OPEN = 3;
static final int L_MORPH_CLOSE = 4;
static final int L_MORPH_HMT = 5;
// Top-hat types
static final int L_TOPHAT_WHITE = 0;
static final int L_TOPHAT_BLACK = 1;
// Connectivity
static final int L_CONNECTIVITY_4 = 0;
static final int L_CONNECTIVITY_8 = 1;// Remove salt-and-pepper noise
PIX denoised = pixOpen(null, binaryImage, selCreateBrick(3, 3, 1, 1));
denoised = pixClose(denoised, selCreateBrick(3, 3, 1, 1));// Extract thin lines
PIX thinLines = pixTophat(binaryImage, selCreateBrick(1, 15, 0, 7), L_TOPHAT_WHITE);
// Extract small objects
PIX smallObjects = pixTophat(binaryImage, selCreateCircular(10), L_TOPHAT_WHITE);// Get object boundaries
PIX boundaries = pixMorphGradient(objects, selCreateBrick(3, 3, 1, 1), L_MORPH_DILATE);
// Skeletonize for shape matching
PIX skeleton = pixSkeleton(objects, 8);Install with Tessl CLI
npx tessl i tessl/maven-org-bytedeco--leptonica-platform