or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

collections.mdconnected-components.mdcore-images.mdgeometry.mdimage-io.mdimage-processing.mdindex.mdmorphology.mdtext-recognition.mdutilities.md

connected-components.mddocs/

0

# Connected Components and Contour Analysis

1

2

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

3

4

## Capabilities

5

6

### Connected Component Border Representation

7

8

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

9

10

```java { .api }

11

/**

12

* Connected component border representation

13

* Represents a single connected component through its border pixels

14

*/

15

class CCBORD extends Pointer {

16

// Component properties

17

PIX pix(); // minimally-clipped bitmap of component

18

BOXA boxa(); // boxes for primary component and holes

19

PTA start(); // initial border pixel locations

20

PTAA local(); // chain code for border (relative coords)

21

PTAA global(); // global pixel locations of border

22

23

// Reference counting

24

int refcount(); // internal reference count

25

26

// Manual cleanup

27

void destroy();

28

}

29

30

/**

31

* Array of connected component borders

32

*/

33

class CCBORDA extends Pointer {

34

int n(); // number of ccbords

35

36

// Access methods

37

CCBORD getCcbord(int index);

38

39

// Manual cleanup

40

void destroy();

41

}

42

43

// Connected component border functions

44

CCBORD ccbordCreate(PIX pixs, int connectivity);

45

CCBORDA ccbordaCreate(PIX pixs, int connectivity);

46

int ccbordDestroy(CCBORD pccbord);

47

int ccbordaDestroy(CCBORDA pccborda);

48

49

// Border extraction and rendering

50

PIX ccbordDisplayBorder(CCBORD ccbord);

51

PIX ccbordDisplaySP(CCBORD ccbord);

52

PIX ccbordaDisplayBorders(CCBORDA ccborda);

53

54

// Chain code generation

55

int ccbordStepChainsToPixCoords(CCBORD ccbord, int coordtype);

56

int ccbordSaveChainCode(CCBORD ccbord, String filename);

57

```

58

59

**Usage Examples:**

60

61

```java

62

import org.bytedeco.leptonica.*;

63

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

64

65

// Create connected component borders from binary image

66

PIX pixBinary = pixRead("binary_shapes.png");

67

CCBORDA ccborda = ccbordaCreate(pixBinary, 8); // 8-connectivity

68

69

// Process each connected component

70

int numComponents = ccborda.n();

71

for (int i = 0; i < numComponents; i++) {

72

CCBORD ccbord = ccborda.getCcbord(i);

73

74

// Get the component's bounding boxes

75

BOXA boxes = ccbord.boxa();

76

77

// Get border pixel coordinates

78

PTAA globalBorder = ccbord.global();

79

80

// Render the border for visualization

81

PIX borderPix = ccbordDisplayBorder(ccbord);

82

pixWrite("component_" + i + "_border.png", borderPix, IFF_PNG);

83

}

84

```

85

86

### Standard Connected Components

87

88

Basic connected component labeling and analysis functions.

89

90

```java { .api }

91

// Connected component labeling

92

PIX pixConnComp(PIX pixs, PIXA ppixa, int connectivity);

93

PIX pixConnCompPixa(PIX pixs, PIXA ppixa, int connectivity);

94

PIX pixConnCompBB(PIX pixs, BOXA pboxa, int connectivity);

95

96

// Component counting and analysis

97

int pixCountConnComp(PIX pixs, int connectivity, IntPointer pcount);

98

PIX pixLabelByReduction(PIX pixs, int connectivity, IntPointer bg, IntPointer fg);

99

100

// Size-based filtering

101

PIXA pixaSelectBySize(PIXA pixas, int width, int height, int type, int relation, IntPointer pchanged);

102

NUMA pixaFindWidthHeightRatio(PIXA pixa);

103

NUMA pixaFindAreaPerimRatio(PIXA pixa);

104

105

// Component reconstruction

106

PIX pixaDisplayRandomCmap(PIXA pixa, int w, int h);

107

PIX pixaDisplayOnLattice(PIXA pixa, int cellw, int cellh, IntPointer pncols, BOXA pboxa);

108

```

109

110

**Usage Examples:**

111

112

```java

113

// Basic connected component analysis

114

PIX pixBinary = pixRead("objects.png");

115

PIXA components = new PIXA(null);

116

BOXA boxes = new BOXA(null);

117

118

// Extract components with bounding boxes

119

PIX labeled = pixConnCompBB(pixBinary, boxes, 8);

120

121

// Count components

122

IntPointer count = new IntPointer(1);

123

pixCountConnComp(pixBinary, 8, count);

124

System.out.println("Found " + count.get() + " components");

125

126

// Filter by size (keep components larger than 100x100 pixels)

127

PIXA largeComponents = pixaSelectBySize(components, 100, 100, L_SELECT_WIDTH, L_SELECT_IF_GTE, null);

128

129

// Display results

130

PIX result = pixaDisplayRandomCmap(largeComponents, 800, 600);

131

pixWrite("components_filtered.png", result, IFF_PNG);

132

```

133

134

### Contour and Shape Analysis

135

136

Advanced contour processing and shape analysis capabilities.

137

138

```java { .api }

139

// Contour smoothing and processing

140

PTA ptaGetBoundaryPixels(PIX pixs, int type);

141

PTA ptaRemoveShortSegments(PTA ptas, float minlength);

142

PTA ptaSubsample(PTA ptas, int subfactor);

143

144

// Shape analysis

145

float ptaGetLinearLSF(PTA pta, FloatPointer pa, FloatPointer pb, NUMA pnafit);

146

int ptaGetArrays(PTA pta, NUMA pnax, NUMA pnay);

147

PTA ptaReplicatePattern(PTA ptas, PIX pixp, PTA ptap, int cx, int cy, int w, int h);

148

149

// Convex hull

150

PTA ptaGetConvexHull(PTA ptas);

151

int ptaContainsPt(PTA pta, float x, float y);

152

int ptaTestIntersection(PTA pta1, PTA pta2);

153

154

// Distance and similarity measures

155

float ptaGetMinDistance(PTA pta1, PTA pta2);

156

int ptaJoin(PTA ptad, PTA ptas, int istart, int iend);

157

```

158

159

**Usage Examples:**

160

161

```java

162

// Extract and analyze object contours

163

PIX pixBinary = pixRead("shape.png");

164

165

// Get boundary pixels as point array

166

PTA boundary = ptaGetBoundaryPixels(pixBinary, L_BOUNDARY_FG);

167

168

// Smooth the contour by subsampling

169

PTA smoothed = ptaSubsample(boundary, 3);

170

171

// Remove short segments

172

PTA cleaned = ptaRemoveShortSegments(smoothed, 10.0f);

173

174

// Compute convex hull

175

PTA hull = ptaGetConvexHull(cleaned);

176

177

// Test if a point is inside the shape

178

boolean inside = ptaContainsPt(hull, 150.0f, 200.0f) == 1;

179

180

// Measure distances between contours

181

PTA otherShape = ptaGetBoundaryPixels(otherPix, L_BOUNDARY_FG);

182

float distance = ptaGetMinDistance(cleaned, otherShape);

183

184

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

185

```

186

187

## Types

188

189

```java { .api }

190

// Connectivity constants

191

static final int L_CONNECTIVITY_4 = 4;

192

static final int L_CONNECTIVITY_8 = 8;

193

194

// Boundary extraction types

195

static final int L_BOUNDARY_FG = 1; // foreground boundary

196

static final int L_BOUNDARY_BG = 2; // background boundary

197

198

// Selection criteria for size filtering

199

static final int L_SELECT_WIDTH = 1;

200

static final int L_SELECT_HEIGHT = 2;

201

static final int L_SELECT_MAX_DIMENSION = 3;

202

static final int L_SELECT_AREA = 4;

203

static final int L_SELECT_PERIMETER = 5;

204

205

// Selection relations

206

static final int L_SELECT_IF_LT = 1; // if less than

207

static final int L_SELECT_IF_GT = 2; // if greater than

208

static final int L_SELECT_IF_LTE = 3; // if less than or equal

209

static final int L_SELECT_IF_GTE = 4; // if greater than or equal

210

```