CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-bytedeco--leptonica-platform

JavaCPP bindings for Leptonica image processing library with cross-platform support

Pending
Overview
Eval results
Files

connected-components.mddocs/

Connected Components and Contour Analysis

Comprehensive connected component analysis, contour processing, and border representations for object detection and shape analysis.

Capabilities

Connected Component Border Representation

Advanced structure for representing connected components through their borders, supporting holes and complex shapes.

/**
 * Connected component border representation
 * Represents a single connected component through its border pixels
 */
class CCBORD extends Pointer {
    // Component properties
    PIX pix();           // minimally-clipped bitmap of component
    BOXA boxa();         // boxes for primary component and holes
    PTA start();         // initial border pixel locations
    PTAA local();        // chain code for border (relative coords)
    PTAA global();       // global pixel locations of border
    
    // Reference counting
    int refcount();      // internal reference count
    
    // Manual cleanup
    void destroy();
}

/**
 * Array of connected component borders
 */
class CCBORDA extends Pointer {
    int n();             // number of ccbords
    
    // Access methods
    CCBORD getCcbord(int index);
    
    // Manual cleanup  
    void destroy();
}

// Connected component border functions
CCBORD ccbordCreate(PIX pixs, int connectivity);
CCBORDA ccbordaCreate(PIX pixs, int connectivity);
int ccbordDestroy(CCBORD pccbord);
int ccbordaDestroy(CCBORDA pccborda);

// Border extraction and rendering
PIX ccbordDisplayBorder(CCBORD ccbord);
PIX ccbordDisplaySP(CCBORD ccbord);
PIX ccbordaDisplayBorders(CCBORDA ccborda);

// Chain code generation
int ccbordStepChainsToPixCoords(CCBORD ccbord, int coordtype);
int ccbordSaveChainCode(CCBORD ccbord, String filename);

Usage Examples:

import org.bytedeco.leptonica.*;
import static org.bytedeco.leptonica.global.leptonica.*;

// Create connected component borders from binary image
PIX pixBinary = pixRead("binary_shapes.png");
CCBORDA ccborda = ccbordaCreate(pixBinary, 8); // 8-connectivity

// Process each connected component
int numComponents = ccborda.n();
for (int i = 0; i < numComponents; i++) {
    CCBORD ccbord = ccborda.getCcbord(i);
    
    // Get the component's bounding boxes
    BOXA boxes = ccbord.boxa();
    
    // Get border pixel coordinates
    PTAA globalBorder = ccbord.global();
    
    // Render the border for visualization
    PIX borderPix = ccbordDisplayBorder(ccbord);
    pixWrite("component_" + i + "_border.png", borderPix, IFF_PNG);
}

Standard Connected Components

Basic connected component labeling and analysis functions.

// Connected component labeling
PIX pixConnComp(PIX pixs, PIXA ppixa, int connectivity);
PIX pixConnCompPixa(PIX pixs, PIXA ppixa, int connectivity);
PIX pixConnCompBB(PIX pixs, BOXA pboxa, int connectivity);

// Component counting and analysis
int pixCountConnComp(PIX pixs, int connectivity, IntPointer pcount);
PIX pixLabelByReduction(PIX pixs, int connectivity, IntPointer bg, IntPointer fg);

// Size-based filtering
PIXA pixaSelectBySize(PIXA pixas, int width, int height, int type, int relation, IntPointer pchanged);
NUMA pixaFindWidthHeightRatio(PIXA pixa);
NUMA pixaFindAreaPerimRatio(PIXA pixa);

// Component reconstruction
PIX pixaDisplayRandomCmap(PIXA pixa, int w, int h);
PIX pixaDisplayOnLattice(PIXA pixa, int cellw, int cellh, IntPointer pncols, BOXA pboxa);

Usage Examples:

// Basic connected component analysis
PIX pixBinary = pixRead("objects.png");
PIXA components = new PIXA(null);
BOXA boxes = new BOXA(null);

// Extract components with bounding boxes
PIX labeled = pixConnCompBB(pixBinary, boxes, 8);

// Count components
IntPointer count = new IntPointer(1);
pixCountConnComp(pixBinary, 8, count);
System.out.println("Found " + count.get() + " components");

// Filter by size (keep components larger than 100x100 pixels)
PIXA largeComponents = pixaSelectBySize(components, 100, 100, L_SELECT_WIDTH, L_SELECT_IF_GTE, null);

// Display results
PIX result = pixaDisplayRandomCmap(largeComponents, 800, 600);
pixWrite("components_filtered.png", result, IFF_PNG);

Contour and Shape Analysis

Advanced contour processing and shape analysis capabilities.

// Contour smoothing and processing
PTA ptaGetBoundaryPixels(PIX pixs, int type);
PTA ptaRemoveShortSegments(PTA ptas, float minlength);
PTA ptaSubsample(PTA ptas, int subfactor);

// Shape analysis
float ptaGetLinearLSF(PTA pta, FloatPointer pa, FloatPointer pb, NUMA pnafit);
int ptaGetArrays(PTA pta, NUMA pnax, NUMA pnay);
PTA ptaReplicatePattern(PTA ptas, PIX pixp, PTA ptap, int cx, int cy, int w, int h);

// Convex hull
PTA ptaGetConvexHull(PTA ptas);
int ptaContainsPt(PTA pta, float x, float y);
int ptaTestIntersection(PTA pta1, PTA pta2);

// Distance and similarity measures  
float ptaGetMinDistance(PTA pta1, PTA pta2);
int ptaJoin(PTA ptad, PTA ptas, int istart, int iend);

Usage Examples:

// Extract and analyze object contours
PIX pixBinary = pixRead("shape.png");

// Get boundary pixels as point array
PTA boundary = ptaGetBoundaryPixels(pixBinary, L_BOUNDARY_FG);

// Smooth the contour by subsampling
PTA smoothed = ptaSubsample(boundary, 3);

// Remove short segments
PTA cleaned = ptaRemoveShortSegments(smoothed, 10.0f);

// Compute convex hull
PTA hull = ptaGetConvexHull(cleaned);

// Test if a point is inside the shape
boolean inside = ptaContainsPt(hull, 150.0f, 200.0f) == 1;

// Measure distances between contours
PTA otherShape = ptaGetBoundaryPixels(otherPix, L_BOUNDARY_FG);
float distance = ptaGetMinDistance(cleaned, otherShape);

System.out.println("Minimum distance between shapes: " + distance);

Types

// Connectivity constants
static final int L_CONNECTIVITY_4 = 4;
static final int L_CONNECTIVITY_8 = 8;

// Boundary extraction types
static final int L_BOUNDARY_FG = 1;        // foreground boundary
static final int L_BOUNDARY_BG = 2;        // background boundary

// Selection criteria for size filtering
static final int L_SELECT_WIDTH = 1;
static final int L_SELECT_HEIGHT = 2;  
static final int L_SELECT_MAX_DIMENSION = 3;
static final int L_SELECT_AREA = 4;
static final int L_SELECT_PERIMETER = 5;

// Selection relations
static final int L_SELECT_IF_LT = 1;       // if less than
static final int L_SELECT_IF_GT = 2;       // if greater than
static final int L_SELECT_IF_LTE = 3;      // if less than or equal
static final int L_SELECT_IF_GTE = 4;      // if greater than or equal

Install with Tessl CLI

npx tessl i tessl/maven-org-bytedeco--leptonica-platform

docs

collections.md

connected-components.md

core-images.md

geometry.md

image-io.md

image-processing.md

index.md

morphology.md

text-recognition.md

utilities.md

tile.json