or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

aruco.mdcamera-calibration.mdcomputational-photography.mdcontours-shapes.mdcore-operations.mddnn.mdfeature-detection.mdgui-drawing.mdimage-processing.mdimage-video-io.mdindex.mdmachine-learning.mdobject-detection.mdtask-log.mdvideo-analysis.md

image-processing.mddocs/

0

# Image Processing

1

2

OpenCV's image processing module (`imgproc`) provides comprehensive functionality for image filtering, geometric transformations, color space conversion, feature detection, segmentation, and more. All functions are accessed through the `cv2` namespace.

3

4

## Capabilities

5

6

### Filtering

7

8

Image filtering operations for smoothing, sharpening, edge detection, and noise reduction.

9

10

#### Smoothing Filters

11

12

```python { .api }

13

cv2.blur(src, ksize, dst=None, anchor=None, borderType=None) -> dst

14

```

15

16

Applies average blur filter (box filter normalized).

17

18

- **src**: Input image

19

- **ksize**: Kernel size (width, height) tuple

20

- **anchor**: Anchor point (default: kernel center)

21

- **borderType**: Border extrapolation method

22

- **Returns**: Blurred image

23

24

```python { .api }

25

cv2.GaussianBlur(src, ksize, sigmaX, dst=None, sigmaY=None, borderType=None) -> dst

26

```

27

28

Applies Gaussian blur filter using a Gaussian kernel.

29

30

- **src**: Input image

31

- **ksize**: Kernel size (width, height) - must be positive and odd

32

- **sigmaX**: Gaussian kernel standard deviation in X direction

33

- **sigmaY**: Gaussian kernel standard deviation in Y direction (default: sigmaX)

34

- **borderType**: Border extrapolation method

35

- **Returns**: Blurred image

36

37

```python { .api }

38

cv2.medianBlur(src, ksize, dst=None) -> dst

39

```

40

41

Applies median blur filter. Effective for salt-and-pepper noise removal.

42

43

- **src**: Input image

44

- **ksize**: Aperture linear size (must be odd and greater than 1)

45

- **Returns**: Blurred image

46

47

```python { .api }

48

cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace, dst=None, borderType=None) -> dst

49

```

50

51

Applies bilateral filter for edge-preserving smoothing.

52

53

- **src**: Input image (8-bit or floating-point, 1 or 3 channels)

54

- **d**: Diameter of pixel neighborhood

55

- **sigmaColor**: Filter sigma in color space

56

- **sigmaSpace**: Filter sigma in coordinate space

57

- **borderType**: Border extrapolation method

58

- **Returns**: Filtered image

59

60

```python { .api }

61

cv2.boxFilter(src, ddepth, ksize, dst=None, anchor=None, normalize=True, borderType=None) -> dst

62

```

63

64

Applies box filter (unnormalized if normalize=False).

65

66

- **src**: Input image

67

- **ddepth**: Output image depth (-1 uses source depth)

68

- **ksize**: Kernel size

69

- **normalize**: Whether to normalize the kernel

70

- **Returns**: Filtered image

71

72

```python { .api }

73

cv2.sqrBoxFilter(src, ddepth, ksize, dst=None, anchor=None, normalize=True, borderType=None) -> dst

74

```

75

76

Calculates normalized squared box filter (useful for local variance computation).

77

78

- **src**: Input image

79

- **ddepth**: Output image depth (-1 uses source depth)

80

- **ksize**: Kernel size

81

- **normalize**: Whether to normalize the kernel

82

- **Returns**: Filtered image

83

84

#### Custom Convolution

85

86

```python { .api }

87

cv2.filter2D(src, ddepth, kernel, dst=None, anchor=None, delta=0, borderType=None) -> dst

88

```

89

90

Convolves image with a custom kernel.

91

92

- **src**: Input image

93

- **ddepth**: Desired depth of destination image (-1 uses source depth)

94

- **kernel**: Convolution kernel (single-channel floating-point matrix)

95

- **anchor**: Anchor point within kernel (default: kernel center)

96

- **delta**: Value added to filtered results

97

- **borderType**: Border extrapolation method

98

- **Returns**: Filtered image

99

100

```python { .api }

101

cv2.sepFilter2D(src, ddepth, kernelX, kernelY, dst=None, anchor=None, delta=0, borderType=None) -> dst

102

```

103

104

Applies separable linear filter (more efficient for separable kernels).

105

106

- **src**: Input image

107

- **ddepth**: Output image depth

108

- **kernelX**: Coefficients for filtering rows

109

- **kernelY**: Coefficients for filtering columns

110

- **Returns**: Filtered image

111

112

#### Derivative Filters

113

114

```python { .api }

115

cv2.Sobel(src, ddepth, dx, dy, ksize=3, scale=1, delta=0, borderType=None) -> dst

116

```

117

118

Calculates image derivatives using Sobel operator.

119

120

- **src**: Input image

121

- **ddepth**: Output image depth

122

- **dx**: Order of derivative in x direction

123

- **dy**: Order of derivative in y direction

124

- **ksize**: Size of extended Sobel kernel (1, 3, 5, or 7)

125

- **scale**: Scale factor for computed derivative values

126

- **delta**: Value added to results

127

- **Returns**: Derivative image

128

129

```python { .api }

130

cv2.Scharr(src, ddepth, dx, dy, scale=1, delta=0, borderType=None) -> dst

131

```

132

133

Calculates image derivatives using Scharr operator (more accurate than 3x3 Sobel).

134

135

- **src**: Input image

136

- **ddepth**: Output image depth

137

- **dx**: Order of derivative in x (0 or 1)

138

- **dy**: Order of derivative in y (0 or 1)

139

- **scale**: Scale factor

140

- **delta**: Value added to results

141

- **Returns**: Derivative image

142

143

```python { .api }

144

cv2.Laplacian(src, ddepth, ksize=1, scale=1, delta=0, borderType=None) -> dst

145

```

146

147

Calculates Laplacian of image (sum of second derivatives).

148

149

- **src**: Input image

150

- **ddepth**: Output image depth

151

- **ksize**: Aperture size (must be positive and odd)

152

- **scale**: Scale factor

153

- **delta**: Value added to results

154

- **Returns**: Laplacian image

155

156

#### Edge Detection

157

158

```python { .api }

159

cv2.Canny(image, threshold1, threshold2, edges=None, apertureSize=3, L2gradient=False) -> edges

160

```

161

162

Detects edges using the Canny algorithm.

163

164

- **image**: 8-bit input image

165

- **threshold1**: First threshold for hysteresis

166

- **threshold2**: Second threshold for hysteresis

167

- **apertureSize**: Aperture size for Sobel operator

168

- **L2gradient**: Use L2 norm for gradient magnitude (more accurate but slower)

169

- **Returns**: Binary edge map

170

171

#### Corner Detection

172

173

Corner detection algorithms for identifying salient points in images, useful for feature tracking and image matching.

174

175

```python { .api }

176

cv2.goodFeaturesToTrack(image, maxCorners, qualityLevel, minDistance, corners=None, mask=None, blockSize=3, useHarrisDetector=False, k=0.04) -> corners

177

```

178

179

Determines strong corners on an image using Shi-Tomasi corner detection method.

180

181

- **image**: Input 8-bit or floating-point 32-bit, single-channel image

182

- **maxCorners**: Maximum number of corners to return (if ≤ 0, no limit is set)

183

- **qualityLevel**: Parameter characterizing minimal accepted quality of image corners (multiplied by best corner quality measure)

184

- **minDistance**: Minimum possible Euclidean distance between returned corners

185

- **mask**: Optional region of interest (CV_8UC1, same size as image)

186

- **blockSize**: Size of average block for computing derivative covariation matrix

187

- **useHarrisDetector**: Whether to use Harris detector (true) or cornerMinEigenVal (false)

188

- **k**: Free parameter of Harris detector (used if useHarrisDetector=True)

189

- **Returns**: Array of detected corners

190

191

```python { .api }

192

cv2.cornerHarris(src, blockSize, ksize, k, dst=None, borderType=cv2.BORDER_DEFAULT) -> dst

193

```

194

195

Harris corner detector.

196

197

- **src**: Input single-channel 8-bit or floating-point image

198

- **blockSize**: Neighborhood size (see cornerEigenValsAndVecs)

199

- **ksize**: Aperture parameter for the Sobel operator

200

- **k**: Harris detector free parameter

201

- **borderType**: Pixel extrapolation method (BORDER_WRAP not supported)

202

- **Returns**: Image to store the Harris detector responses (type CV_32FC1, same size as src)

203

204

```python { .api }

205

cv2.cornerSubPix(image, corners, winSize, zeroZone, criteria) -> corners

206

```

207

208

Refines the corner locations to sub-pixel accuracy.

209

210

- **image**: Input single-channel, 8-bit or float image

211

- **corners**: Initial coordinates of input corners and refined coordinates provided for output

212

- **winSize**: Half of the side length of the search window (e.g., if winSize=Size(5,5), then an 11×11 search window is used)

213

- **zeroZone**: Half of size of the dead region in the middle of the search zone (value of (-1,-1) indicates no such size)

214

- **criteria**: Criteria for termination of the iterative corner refinement process (TermCriteria)

215

- **Returns**: Refined corner coordinates (modifies corners in-place)

216

217

#### Hough Transforms

218

219

Hough transforms detect lines, circles, and other shapes in images, typically applied after edge detection.

220

221

```python { .api }

222

cv2.HoughLines(image, rho, theta, threshold, lines=None, srn=0, stn=0, min_theta=0, max_theta=np.pi) -> lines

223

```

224

225

Detects lines using the standard Hough Line Transform.

226

227

- **image**: 8-bit, single-channel binary source image (typically edge map from Canny)

228

- **rho**: Distance resolution in pixels of the Hough accumulator

229

- **theta**: Angle resolution in radians of the Hough accumulator

230

- **threshold**: Accumulator threshold parameter (minimum votes to detect a line)

231

- **srn**: For multi-scale Hough transform (default 0 for classical)

232

- **stn**: For multi-scale Hough transform (default 0 for classical)

233

- **min_theta**: Minimum angle to check for lines (default 0)

234

- **max_theta**: Maximum angle to check for lines (default π)

235

- **Returns**: Array of lines in (rho, theta) representation, or None

236

237

```python { .api }

238

cv2.HoughLinesP(image, rho, theta, threshold, lines=None, minLineLength=0, maxLineGap=0) -> lines

239

```

240

241

Detects line segments using the Probabilistic Hough Line Transform.

242

243

- **image**: 8-bit, single-channel binary source image

244

- **rho**: Distance resolution in pixels of the Hough accumulator

245

- **theta**: Angle resolution in radians of the Hough accumulator

246

- **threshold**: Accumulator threshold parameter

247

- **minLineLength**: Minimum line length (shorter lines are rejected)

248

- **maxLineGap**: Maximum gap between points on the same line to link them

249

- **Returns**: Array of line segments as (x1, y1, x2, y2), or None

250

251

```python { .api }

252

cv2.HoughCircles(image, method, dp, minDist, circles=None, param1=100, param2=100, minRadius=0, maxRadius=0) -> circles

253

```

254

255

Detects circles using the Hough Circle Transform.

256

257

- **image**: 8-bit, single-channel grayscale input image

258

- **method**: Detection method (use cv2.HOUGH_GRADIENT or cv2.HOUGH_GRADIENT_ALT)

259

- **dp**: Inverse ratio of accumulator resolution to image resolution

260

- **minDist**: Minimum distance between detected circle centers

261

- **param1**: For HOUGH_GRADIENT: higher threshold for Canny edge detector

262

- **param2**: For HOUGH_GRADIENT: accumulator threshold for circle centers

263

- **minRadius**: Minimum circle radius

264

- **maxRadius**: Maximum circle radius (0 = no limit)

265

- **Returns**: Array of detected circles as (x, y, radius), or None

266

267

**Usage Example:**

268

269

```python

270

import cv2

271

import numpy as np

272

273

# Detect lines

274

edges = cv2.Canny(image, 50, 150)

275

lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100, minLineLength=50, maxLineGap=10)

276

277

if lines is not None:

278

for line in lines:

279

x1, y1, x2, y2 = line[0]

280

cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)

281

282

# Detect circles

283

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

284

circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=1, minDist=20,

285

param1=50, param2=30, minRadius=10, maxRadius=50)

286

287

if circles is not None:

288

circles = np.uint16(np.around(circles))

289

for circle in circles[0, :]:

290

cv2.circle(image, (circle[0], circle[1]), circle[2], (0, 255, 0), 2)

291

```

292

293

#### Border Handling Constants

294

295

```python { .api }

296

cv2.BORDER_CONSTANT # Constant border (iiiiii|abcdefgh|iiiiiii)

297

cv2.BORDER_REPLICATE # Replicate border (aaaaaa|abcdefgh|hhhhhhh)

298

cv2.BORDER_REFLECT # Reflect border (fedcba|abcdefgh|hgfedcb)

299

cv2.BORDER_WRAP # Wrap border (cdefgh|abcdefgh|abcdefg)

300

cv2.BORDER_REFLECT_101 # Reflect 101 border (gfedcb|abcdefgh|gfedcba)

301

cv2.BORDER_DEFAULT # Same as BORDER_REFLECT_101

302

cv2.BORDER_ISOLATED # Do not extrapolate beyond image

303

```

304

305

### Morphological Operations

306

307

Mathematical morphology operations for shape processing and noise removal.

308

309

```python { .api }

310

cv2.erode(src, kernel, dst=None, anchor=None, iterations=1, borderType=None, borderValue=None) -> dst

311

```

312

313

Erodes image using specified structuring element.

314

315

- **src**: Input image

316

- **kernel**: Structuring element

317

- **anchor**: Anchor position within element

318

- **iterations**: Number of times erosion is applied

319

- **borderType**: Border extrapolation method

320

- **borderValue**: Border value for constant border

321

- **Returns**: Eroded image

322

323

```python { .api }

324

cv2.dilate(src, kernel, dst=None, anchor=None, iterations=1, borderType=None, borderValue=None) -> dst

325

```

326

327

Dilates image using specified structuring element.

328

329

- **src**: Input image

330

- **kernel**: Structuring element

331

- **anchor**: Anchor position within element

332

- **iterations**: Number of times dilation is applied

333

- **borderType**: Border extrapolation method

334

- **borderValue**: Border value for constant border

335

- **Returns**: Dilated image

336

337

```python { .api }

338

cv2.morphologyEx(src, op, kernel, dst=None, anchor=None, iterations=1, borderType=None, borderValue=None) -> dst

339

```

340

341

Performs advanced morphological transformations.

342

343

- **src**: Input image

344

- **op**: Type of morphological operation (see constants below)

345

- **kernel**: Structuring element

346

- **anchor**: Anchor position within element

347

- **iterations**: Number of times operation is applied

348

- **Returns**: Result image

349

350

```python { .api }

351

cv2.getStructuringElement(shape, ksize, anchor=None) -> retval

352

```

353

354

Creates structuring element for morphological operations.

355

356

- **shape**: Element shape (MORPH_RECT, MORPH_CROSS, MORPH_ELLIPSE)

357

- **ksize**: Size of structuring element

358

- **anchor**: Anchor position (default: center)

359

- **Returns**: Structuring element matrix

360

361

#### Morphological Operation Types

362

363

```python { .api }

364

cv2.MORPH_ERODE # Erosion

365

cv2.MORPH_DILATE # Dilation

366

cv2.MORPH_OPEN # Opening (erosion followed by dilation)

367

cv2.MORPH_CLOSE # Closing (dilation followed by erosion)

368

cv2.MORPH_GRADIENT # Morphological gradient (difference between dilation and erosion)

369

cv2.MORPH_TOPHAT # Top hat (difference between source and opening)

370

cv2.MORPH_BLACKHAT # Black hat (difference between closing and source)

371

cv2.MORPH_HITMISS # Hit-or-miss transform

372

```

373

374

#### Structuring Element Shapes

375

376

```python { .api }

377

cv2.MORPH_RECT # Rectangular structuring element

378

cv2.MORPH_CROSS # Cross-shaped structuring element

379

cv2.MORPH_ELLIPSE # Elliptical structuring element

380

```

381

382

### Geometric Transformations

383

384

Functions for resizing, rotating, warping, and remapping images.

385

386

#### Resizing and Rescaling

387

388

```python { .api }

389

cv2.resize(src, dsize, dst=None, fx=0, fy=0, interpolation=cv2.INTER_LINEAR) -> dst

390

```

391

392

Resizes image to specified size or by scale factors.

393

394

- **src**: Input image

395

- **dsize**: Output image size (width, height). If 0, computed from fx and fy

396

- **fx**: Scale factor along horizontal axis

397

- **fy**: Scale factor along vertical axis

398

- **interpolation**: Interpolation method (see constants below)

399

- **Returns**: Resized image

400

401

```python { .api }

402

cv2.pyrDown(src, dst=None, dstsize=None, borderType=None) -> dst

403

```

404

405

Downsamples image using Gaussian pyramid.

406

407

- **src**: Input image

408

- **dstsize**: Size of output image (default: (src.width/2, src.height/2))

409

- **borderType**: Border extrapolation method

410

- **Returns**: Downsampled image

411

412

```python { .api }

413

cv2.pyrUp(src, dst=None, dstsize=None, borderType=None) -> dst

414

```

415

416

Upsamples image using Gaussian pyramid.

417

418

- **src**: Input image

419

- **dstsize**: Size of output image (default: (src.width*2, src.height*2))

420

- **borderType**: Border extrapolation method

421

- **Returns**: Upsampled image

422

423

```python { .api }

424

cv2.buildPyramid(src, maxlevel, dst=None, borderType=None) -> dst

425

```

426

427

Constructs Gaussian pyramid for an image.

428

429

- **src**: Input image

430

- **maxlevel**: Number of pyramid levels

431

- **borderType**: Border extrapolation method

432

- **Returns**: List of pyramid levels

433

434

#### Affine Transformations

435

436

```python { .api }

437

cv2.warpAffine(src, M, dsize, dst=None, flags=cv2.INTER_LINEAR, borderMode=None, borderValue=None) -> dst

438

```

439

440

Applies affine transformation to image.

441

442

- **src**: Input image

443

- **M**: 2x3 transformation matrix

444

- **dsize**: Size of output image

445

- **flags**: Combination of interpolation method and optional flags

446

- **borderMode**: Border extrapolation method

447

- **borderValue**: Value for constant border

448

- **Returns**: Transformed image

449

450

```python { .api }

451

cv2.getRotationMatrix2D(center, angle, scale) -> retval

452

```

453

454

Calculates 2D rotation matrix for rotating around a center point.

455

456

- **center**: Center of rotation (x, y)

457

- **angle**: Rotation angle in degrees (positive: counter-clockwise)

458

- **scale**: Isotropic scale factor

459

- **Returns**: 2x3 rotation matrix

460

461

```python { .api }

462

cv2.getAffineTransform(src, dst) -> retval

463

```

464

465

Calculates affine transform from three pairs of corresponding points.

466

467

- **src**: Coordinates of triangle vertices in source image (3x2 array)

468

- **dst**: Coordinates of corresponding triangle vertices in destination image

469

- **Returns**: 2x3 affine transformation matrix

470

471

```python { .api }

472

cv2.invertAffineTransform(M, iM=None) -> iM

473

```

474

475

Inverts affine transformation.

476

477

- **M**: Original 2x3 affine transformation matrix

478

- **Returns**: Inverse affine transformation matrix

479

480

#### Perspective Transformations

481

482

```python { .api }

483

cv2.warpPerspective(src, M, dsize, dst=None, flags=cv2.INTER_LINEAR, borderMode=None, borderValue=None) -> dst

484

```

485

486

Applies perspective transformation to image.

487

488

- **src**: Input image

489

- **M**: 3x3 transformation matrix

490

- **dsize**: Size of output image

491

- **flags**: Combination of interpolation method and optional flags

492

- **borderMode**: Border extrapolation method

493

- **borderValue**: Value for constant border

494

- **Returns**: Transformed image

495

496

```python { .api }

497

cv2.getPerspectiveTransform(src, dst, solveMethod=cv2.DECOMP_LU) -> retval

498

```

499

500

Calculates perspective transform from four pairs of corresponding points.

501

502

- **src**: Coordinates of quadrangle vertices in source image (4x2 array)

503

- **dst**: Coordinates of corresponding quadrangle vertices in destination image

504

- **solveMethod**: Method for solving the linear system

505

- **Returns**: 3x3 perspective transformation matrix

506

507

#### Generic Remapping

508

509

```python { .api }

510

cv2.remap(src, map1, map2, interpolation, dst=None, borderMode=None, borderValue=None) -> dst

511

```

512

513

Applies generic geometrical transformation using mapping arrays.

514

515

- **src**: Source image

516

- **map1**: First map (x coordinates or both xy as CV_32FC2)

517

- **map2**: Second map (y coordinates or empty if map1 is CV_32FC2)

518

- **interpolation**: Interpolation method

519

- **borderMode**: Border extrapolation method

520

- **borderValue**: Value for constant border

521

- **Returns**: Remapped image

522

523

```python { .api }

524

cv2.convertMaps(map1, map2, dstmap1type, dstmap1=None, dstmap2=None, nninterpolation=False) -> dstmap1, dstmap2

525

```

526

527

Converts image transformation maps from one representation to another.

528

529

- **map1**: First input map

530

- **map2**: Second input map

531

- **dstmap1type**: Type of first output map

532

- **nninterpolation**: Flag for nearest-neighbor interpolation

533

- **Returns**: Converted maps (dstmap1, dstmap2)

534

535

```python { .api }

536

cv2.getRectSubPix(image, patchSize, center, patch=None, patchType=-1) -> patch

537

```

538

539

Retrieves pixel rectangle from image with sub-pixel accuracy.

540

541

- **image**: Input image

542

- **patchSize**: Size of extracted patch

543

- **center**: Floating-point center coordinates of extracted rectangle

544

- **patchType**: Depth of extracted pixels (-1 uses source type)

545

- **Returns**: Extracted patch

546

547

#### Interpolation Methods

548

549

```python { .api }

550

cv2.INTER_NEAREST # Nearest-neighbor interpolation

551

cv2.INTER_LINEAR # Bilinear interpolation

552

cv2.INTER_CUBIC # Bicubic interpolation

553

cv2.INTER_AREA # Resampling using pixel area relation (best for decimation)

554

cv2.INTER_LANCZOS4 # Lanczos interpolation over 8x8 neighborhood

555

cv2.INTER_LINEAR_EXACT # Bit-exact bilinear interpolation

556

cv2.INTER_NEAREST_EXACT # Bit-exact nearest-neighbor interpolation

557

cv2.INTER_MAX # Mask for interpolation codes

558

cv2.WARP_FILL_OUTLIERS # Fill all pixels outside source image

559

cv2.WARP_INVERSE_MAP # Inverse transformation (dst->src instead of src->dst)

560

```

561

562

### Color Space Conversion

563

564

Functions for converting between different color representations.

565

566

```python { .api }

567

cv2.cvtColor(src, code, dst=None, dstCn=0) -> dst

568

```

569

570

Converts image from one color space to another.

571

572

- **src**: Input image

573

- **code**: Color space conversion code (see constants below)

574

- **dstCn**: Number of channels in destination image (0 = automatic)

575

- **Returns**: Converted image

576

577

```python { .api }

578

cv2.cvtColorTwoPlane(src1, src2, code, dst=None) -> dst

579

```

580

581

Converts two-plane YUV format to RGB/BGR.

582

583

- **src1**: Y plane

584

- **src2**: UV plane

585

- **code**: Color conversion code

586

- **Returns**: Converted image

587

588

#### Color Conversion Codes

589

590

**RGB/BGR conversions:**

591

592

```python { .api }

593

cv2.COLOR_BGR2RGB

594

cv2.COLOR_RGB2BGR

595

cv2.COLOR_BGR2BGRA

596

cv2.COLOR_RGB2RGBA

597

cv2.COLOR_BGRA2BGR

598

cv2.COLOR_RGBA2RGB

599

cv2.COLOR_BGR2RGBA

600

cv2.COLOR_RGB2BGRA

601

cv2.COLOR_RGBA2BGR

602

cv2.COLOR_BGRA2RGB

603

```

604

605

**Grayscale conversions:**

606

607

```python { .api }

608

cv2.COLOR_BGR2GRAY

609

cv2.COLOR_RGB2GRAY

610

cv2.COLOR_GRAY2BGR

611

cv2.COLOR_GRAY2RGB

612

cv2.COLOR_GRAY2BGRA

613

cv2.COLOR_GRAY2RGBA

614

cv2.COLOR_BGRA2GRAY

615

cv2.COLOR_RGBA2GRAY

616

```

617

618

**HSV conversions:**

619

620

```python { .api }

621

cv2.COLOR_BGR2HSV

622

cv2.COLOR_RGB2HSV

623

cv2.COLOR_HSV2BGR

624

cv2.COLOR_HSV2RGB

625

cv2.COLOR_BGR2HSV_FULL

626

cv2.COLOR_RGB2HSV_FULL

627

cv2.COLOR_HSV2BGR_FULL

628

cv2.COLOR_HSV2RGB_FULL

629

```

630

631

**HLS conversions:**

632

633

```python { .api }

634

cv2.COLOR_BGR2HLS

635

cv2.COLOR_RGB2HLS

636

cv2.COLOR_HLS2BGR

637

cv2.COLOR_HLS2RGB

638

cv2.COLOR_BGR2HLS_FULL

639

cv2.COLOR_RGB2HLS_FULL

640

cv2.COLOR_HLS2BGR_FULL

641

cv2.COLOR_HLS2RGB_FULL

642

```

643

644

**Lab conversions:**

645

646

```python { .api }

647

cv2.COLOR_BGR2Lab

648

cv2.COLOR_RGB2Lab

649

cv2.COLOR_Lab2BGR

650

cv2.COLOR_Lab2RGB

651

cv2.COLOR_LBGR2Lab

652

cv2.COLOR_LRGB2Lab

653

cv2.COLOR_Lab2LBGR

654

cv2.COLOR_Lab2LRGB

655

```

656

657

**Luv conversions:**

658

659

```python { .api }

660

cv2.COLOR_BGR2Luv

661

cv2.COLOR_RGB2Luv

662

cv2.COLOR_Luv2BGR

663

cv2.COLOR_Luv2RGB

664

cv2.COLOR_LBGR2Luv

665

cv2.COLOR_LRGB2Luv

666

cv2.COLOR_Luv2LBGR

667

cv2.COLOR_Luv2LRGB

668

```

669

670

**YUV conversions:**

671

672

```python { .api }

673

cv2.COLOR_BGR2YUV

674

cv2.COLOR_RGB2YUV

675

cv2.COLOR_YUV2BGR

676

cv2.COLOR_YUV2RGB

677

cv2.COLOR_YUV2RGB_NV12

678

cv2.COLOR_YUV2BGR_NV12

679

cv2.COLOR_YUV2RGB_NV21

680

cv2.COLOR_YUV2BGR_NV21

681

cv2.COLOR_YUV2RGBA_NV12

682

cv2.COLOR_YUV2BGRA_NV12

683

cv2.COLOR_YUV2RGBA_NV21

684

cv2.COLOR_YUV2BGRA_NV21

685

cv2.COLOR_YUV2RGB_YV12

686

cv2.COLOR_YUV2BGR_YV12

687

cv2.COLOR_YUV2RGB_IYUV

688

cv2.COLOR_YUV2BGR_IYUV

689

cv2.COLOR_YUV2RGBA_YV12

690

cv2.COLOR_YUV2BGRA_YV12

691

cv2.COLOR_YUV2RGBA_IYUV

692

cv2.COLOR_YUV2BGRA_IYUV

693

cv2.COLOR_YUV2GRAY_420

694

cv2.COLOR_YUV2GRAY_NV21

695

cv2.COLOR_YUV2GRAY_NV12

696

cv2.COLOR_YUV2GRAY_YV12

697

cv2.COLOR_YUV2GRAY_IYUV

698

```

699

700

**YCrCb conversions:**

701

702

```python { .api }

703

cv2.COLOR_BGR2YCrCb

704

cv2.COLOR_RGB2YCrCb

705

cv2.COLOR_YCrCb2BGR

706

cv2.COLOR_YCrCb2RGB

707

```

708

709

**XYZ conversions:**

710

711

```python { .api }

712

cv2.COLOR_BGR2XYZ

713

cv2.COLOR_RGB2XYZ

714

cv2.COLOR_XYZ2BGR

715

cv2.COLOR_XYZ2RGB

716

```

717

718

**Bayer pattern conversions:**

719

720

```python { .api }

721

cv2.COLOR_BayerBG2BGR

722

cv2.COLOR_BayerGB2BGR

723

cv2.COLOR_BayerRG2BGR

724

cv2.COLOR_BayerGR2BGR

725

cv2.COLOR_BayerBG2RGB

726

cv2.COLOR_BayerGB2RGB

727

cv2.COLOR_BayerRG2RGB

728

cv2.COLOR_BayerGR2RGB

729

cv2.COLOR_BayerBG2GRAY

730

cv2.COLOR_BayerGB2GRAY

731

cv2.COLOR_BayerRG2GRAY

732

cv2.COLOR_BayerGR2GRAY

733

```

734

735

### Histograms

736

737

Operations for computing and analyzing image histograms.

738

739

```python { .api }

740

cv2.calcHist(images, channels, mask, histSize, ranges, hist=None, accumulate=False) -> hist

741

```

742

743

Calculates histogram of image(s).

744

745

- **images**: Source images list (must be same depth, 8-bit or 32-bit)

746

- **channels**: List of channel indices to compute histogram

747

- **mask**: Optional mask (None to use whole image)

748

- **histSize**: Histogram size in each dimension (list)

749

- **ranges**: Array of histogram bin boundaries in each dimension

750

- **accumulate**: Accumulation flag (add to existing histogram if True)

751

- **Returns**: Output histogram

752

753

```python { .api }

754

cv2.calcBackProject(images, channels, hist, ranges, scale, dst=None) -> dst

755

```

756

757

Calculates back projection of histogram.

758

759

- **images**: Source images

760

- **channels**: List of channels used

761

- **hist**: Input histogram

762

- **ranges**: Array of histogram bin boundaries

763

- **scale**: Scale factor for output back projection

764

- **Returns**: Back projection image

765

766

```python { .api }

767

cv2.compareHist(H1, H2, method) -> retval

768

```

769

770

Compares two histograms.

771

772

- **H1**: First histogram

773

- **H2**: Second histogram

774

- **method**: Comparison method (see constants below)

775

- **Returns**: Comparison result (interpretation depends on method)

776

777

```python { .api }

778

cv2.equalizeHist(src, dst=None) -> dst

779

```

780

781

Equalizes histogram of grayscale image.

782

783

- **src**: Source 8-bit single-channel image

784

- **Returns**: Equalized image

785

786

```python { .api }

787

cv2.createCLAHE(clipLimit=40.0, tileGridSize=(8,8)) -> retval

788

```

789

790

Creates CLAHE (Contrast Limited Adaptive Histogram Equalization) object.

791

792

- **clipLimit**: Threshold for contrast limiting

793

- **tileGridSize**: Size of grid for histogram equalization

794

- **Returns**: CLAHE object with apply() method

795

796

#### Histogram Comparison Methods

797

798

```python { .api }

799

cv2.HISTCMP_CORREL # Correlation

800

cv2.HISTCMP_CHISQR # Chi-Square

801

cv2.HISTCMP_INTERSECT # Intersection

802

cv2.HISTCMP_BHATTACHARYYA # Bhattacharyya distance

803

cv2.HISTCMP_HELLINGER # Synonym for BHATTACHARYYA

804

cv2.HISTCMP_CHISQR_ALT # Alternative Chi-Square

805

cv2.HISTCMP_KL_DIV # Kullback-Leibler divergence

806

```

807

808

### Thresholding

809

810

Binary and adaptive thresholding operations.

811

812

```python { .api }

813

cv2.threshold(src, thresh, maxval, type, dst=None) -> retval, dst

814

```

815

816

Applies fixed-level threshold to image.

817

818

- **src**: Input array (8-bit or 32-bit)

819

- **thresh**: Threshold value

820

- **maxval**: Maximum value for THRESH_BINARY and THRESH_BINARY_INV

821

- **type**: Thresholding type (see constants below)

822

- **Returns**: Tuple of (threshold value used, thresholded image)

823

824

```python { .api }

825

cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C, dst=None) -> dst

826

```

827

828

Applies adaptive threshold (threshold varies across image).

829

830

- **src**: Source 8-bit single-channel image

831

- **maxValue**: Non-zero value assigned to pixels exceeding threshold

832

- **adaptiveMethod**: Adaptive method (ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C)

833

- **thresholdType**: Thresholding type (THRESH_BINARY or THRESH_BINARY_INV)

834

- **blockSize**: Size of pixel neighborhood (odd number)

835

- **C**: Constant subtracted from mean or weighted mean

836

- **Returns**: Thresholded image

837

838

#### Threshold Types

839

840

```python { .api }

841

cv2.THRESH_BINARY # dst = (src > thresh) ? maxval : 0

842

cv2.THRESH_BINARY_INV # dst = (src > thresh) ? 0 : maxval

843

cv2.THRESH_TRUNC # dst = (src > thresh) ? thresh : src

844

cv2.THRESH_TOZERO # dst = (src > thresh) ? src : 0

845

cv2.THRESH_TOZERO_INV # dst = (src > thresh) ? 0 : src

846

cv2.THRESH_MASK # Mask for threshold types

847

cv2.THRESH_OTSU # Use Otsu's algorithm (flag, combine with type)

848

cv2.THRESH_TRIANGLE # Use Triangle algorithm (flag, combine with type)

849

```

850

851

#### Adaptive Threshold Methods

852

853

```python { .api }

854

cv2.ADAPTIVE_THRESH_MEAN_C # Threshold = mean of neighborhood - C

855

cv2.ADAPTIVE_THRESH_GAUSSIAN_C # Threshold = weighted sum (Gaussian) - C

856

```

857

858

### Image Segmentation

859

860

Advanced segmentation algorithms for partitioning images into regions.

861

862

```python { .api }

863

cv2.watershed(image, markers) -> markers

864

```

865

866

Performs marker-based image segmentation using watershed algorithm.

867

868

- **image**: Input 8-bit 3-channel image

869

- **markers**: Input/output 32-bit single-channel marker image. Modified in-place

870

- **Returns**: None (markers modified in-place with segment labels)

871

872

```python { .api }

873

cv2.grabCut(img, mask, rect, bgdModel, fgdModel, iterCount, mode=cv2.GC_EVAL) -> mask, bgdModel, fgdModel

874

```

875

876

Segments foreground using GrabCut algorithm.

877

878

- **img**: Input 8-bit 3-channel image

879

- **mask**: Input/output 8-bit single-channel mask

880

- **rect**: ROI containing segmented object (used with GC_INIT_WITH_RECT)

881

- **bgdModel**: Temporary array for background model (13x5)

882

- **fgdModel**: Temporary array for foreground model (13x5)

883

- **iterCount**: Number of iterations

884

- **mode**: Operation mode (GC_INIT_WITH_RECT, GC_INIT_WITH_MASK, or GC_EVAL)

885

- **Returns**: Updated mask, bgdModel, fgdModel

886

887

```python { .api }

888

cv2.connectedComponents(image, labels=None, connectivity=8, ltype=cv2.CV_32S) -> retval, labels

889

```

890

891

Computes the connected components labeled image of boolean image.

892

893

- **image**: The 8-bit single-channel image to be labeled

894

- **labels**: Destination labeled image

895

- **connectivity**: 8 or 4 for 8-way or 4-way connectivity respectively

896

- **ltype**: Output image label type (currently CV_32S and CV_16U are supported)

897

- **Returns**: Tuple of (number of labels [0, N-1] where 0 is background, labels array)

898

899

```python { .api }

900

cv2.connectedComponentsWithStats(image, labels=None, stats=None, centroids=None, connectivity=8, ltype=cv2.CV_32S) -> retval, labels, stats, centroids

901

```

902

903

Computes the connected components labeled image and produces statistics.

904

905

- **image**: The 8-bit single-channel image to be labeled

906

- **labels**: Destination labeled image

907

- **stats**: Statistics output for each label including background (accessed via stats(label, COLUMN)). Data type is CV_32S

908

- **centroids**: Centroid output for each label including background. Centroids are accessed via centroids(label, 0) for x and centroids(label, 1) for y. Data type CV_64F

909

- **connectivity**: 8 or 4 for 8-way or 4-way connectivity respectively

910

- **ltype**: Output image label type (currently CV_32S and CV_16U are supported)

911

- **Returns**: Tuple of (number of labels [0, N-1] where 0 is background, labels array, stats array, centroids array)

912

913

```python { .api }

914

cv2.distanceTransform(src, distanceType, maskSize, dst=None, dstType=cv2.CV_32F) -> dst, labels

915

```

916

917

Calculates distance to nearest zero pixel for each pixel.

918

919

- **src**: 8-bit single-channel source image (binary)

920

- **distanceType**: Type of distance (DIST_L1, DIST_L2, DIST_C, etc.)

921

- **maskSize**: Size of distance transform mask (3 or 5, or DIST_MASK_PRECISE)

922

- **dstType**: Type of output image (CV_8U or CV_32F)

923

- **Returns**: Distance transform output and labels

924

925

```python { .api }

926

cv2.floodFill(image, mask, seedPoint, newVal, loDiff=None, upDiff=None, flags=None) -> retval, image, mask, rect

927

```

928

929

Fills connected component with specified color.

930

931

- **image**: Input/output 1- or 3-channel image (modified in-place)

932

- **mask**: Operation mask (should be 2 pixels wider and taller than image)

933

- **seedPoint**: Starting point for flood fill

934

- **newVal**: New value for repainted domain pixels

935

- **loDiff**: Maximal lower brightness/color difference

936

- **upDiff**: Maximal upper brightness/color difference

937

- **flags**: Operation flags (connectivity, mask fill value, etc.)

938

- **Returns**: Tuple of (area filled, image, mask, bounding rect)

939

940

#### GrabCut Constants

941

942

```python { .api }

943

cv2.GC_BGD # Background pixel (0)

944

cv2.GC_FGD # Foreground pixel (1)

945

cv2.GC_PR_BGD # Probably background pixel (2)

946

cv2.GC_PR_FGD # Probably foreground pixel (3)

947

cv2.GC_INIT_WITH_RECT # Initialize with rectangle

948

cv2.GC_INIT_WITH_MASK # Initialize with mask

949

cv2.GC_EVAL # Evaluate mode

950

```

951

952

#### Distance Types

953

954

```python { .api }

955

cv2.DIST_USER # User-defined distance

956

cv2.DIST_L1 # Distance = |x1-x2| + |y1-y2|

957

cv2.DIST_L2 # Euclidean distance

958

cv2.DIST_C # Distance = max(|x1-x2|, |y1-y2|)

959

cv2.DIST_L12 # L1-L2 metric

960

cv2.DIST_FAIR # Distance = c^2(|x|/c - log(1+|x|/c))

961

cv2.DIST_WELSCH # Distance = c^2/2(1-exp(-(x/c)^2))

962

cv2.DIST_HUBER # Distance = |x|<c ? x^2/2 : c(|x|-c/2)

963

cv2.DIST_MASK_3 # Mask size 3

964

cv2.DIST_MASK_5 # Mask size 5

965

cv2.DIST_MASK_PRECISE # Precise distance calculation

966

```

967

968

### Template Matching

969

970

Template matching for finding pattern locations in images.

971

972

```python { .api }

973

cv2.matchTemplate(image, templ, method, result=None, mask=None) -> result

974

```

975

976

Compares template against overlapping image regions.

977

978

- **image**: Image where search is running (8-bit or 32-bit floating-point)

979

- **templ**: Searched template (same type as image, not larger than image)

980

- **method**: Comparison method (see constants below)

981

- **mask**: Optional mask of searched template (same size as templ, 8-bit)

982

- **Returns**: Map of comparison results (single-channel 32-bit floating-point)

983

984

#### Template Matching Methods

985

986

```python { .api }

987

cv2.TM_SQDIFF # Sum of squared differences (minimum is best)

988

cv2.TM_SQDIFF_NORMED # Normalized SQDIFF (minimum is best)

989

cv2.TM_CCORR # Cross-correlation (maximum is best)

990

cv2.TM_CCORR_NORMED # Normalized cross-correlation (maximum is best)

991

cv2.TM_CCOEFF # Correlation coefficient (maximum is best)

992

cv2.TM_CCOEFF_NORMED # Normalized correlation coefficient (maximum is best)

993

```

994

995

### Image Pyramids

996

997

Multi-scale image representation using Gaussian pyramids.

998

999

```python { .api }

1000

cv2.pyrDown(src, dst=None, dstsize=None, borderType=None) -> dst

1001

```

1002

1003

Blurs and downsamples image (builds next pyramid level down).

1004

1005

- **src**: Input image

1006

- **dstsize**: Size of output image (default: ((src.cols+1)/2, (src.rows+1)/2))

1007

- **borderType**: Border extrapolation method

1008

- **Returns**: Downsampled image

1009

1010

```python { .api }

1011

cv2.pyrUp(src, dst=None, dstsize=None, borderType=None) -> dst

1012

```

1013

1014

Upsamples and blurs image (builds next pyramid level up).

1015

1016

- **src**: Input image

1017

- **dstsize**: Size of output image (default: (src.cols*2, src.rows*2))

1018

- **borderType**: Border extrapolation method

1019

- **Returns**: Upsampled image

1020

1021

```python { .api }

1022

cv2.buildPyramid(src, maxlevel, dst=None, borderType=None) -> dst

1023

```

1024

1025

Constructs Gaussian pyramid.

1026

1027

- **src**: Source image

1028

- **maxlevel**: Number of pyramid levels (0-based)

1029

- **borderType**: Border extrapolation method

1030

- **Returns**: List containing pyramid levels

1031

1032

### Other Image Transformations

1033

1034

Additional transformation and accumulation operations.

1035

1036

```python { .api }

1037

cv2.integral(src, sum=None, sdepth=-1) -> sum

1038

cv2.integral2(src, sum=None, sqsum=None, sdepth=-1, sqdepth=-1) -> sum, sqsum

1039

cv2.integral3(src, sum=None, sqsum=None, tilted=None, sdepth=-1, sqdepth=-1) -> sum, sqsum, tilted

1040

```

1041

1042

Calculates integral image(s) for fast area sum computation.

1043

1044

- **src**: Input image

1045

- **sdepth**: Desired depth of integral image (-1 = automatic)

1046

- **sqdepth**: Desired depth of squared integral image

1047

- **Returns**: Integral image(s)

1048

1049

```python { .api }

1050

cv2.accumulate(src, dst, mask=None) -> dst

1051

```

1052

1053

Adds image to accumulator.

1054

1055

- **src**: Input image (1- or 3-channel, 8-bit or 32-bit)

1056

- **dst**: Accumulator image (same channels as src, 32-bit or 64-bit)

1057

- **mask**: Optional operation mask

1058

- **Returns**: None (dst modified in-place)

1059

1060

```python { .api }

1061

cv2.accumulateSquare(src, dst, mask=None) -> dst

1062

```

1063

1064

Adds square of source image to accumulator.

1065

1066

- **src**: Input image

1067

- **dst**: Accumulator image

1068

- **mask**: Optional operation mask

1069

- **Returns**: None (dst modified in-place)

1070

1071

```python { .api }

1072

cv2.accumulateProduct(src1, src2, dst, mask=None) -> dst

1073

```

1074

1075

Adds product of two images to accumulator.

1076

1077

- **src1**: First input image

1078

- **src2**: Second input image

1079

- **dst**: Accumulator image

1080

- **mask**: Optional operation mask

1081

- **Returns**: None (dst modified in-place)

1082

1083

```python { .api }

1084

cv2.accumulateWeighted(src, dst, alpha, mask=None) -> dst

1085

```

1086

1087

Updates running average (exponentially weighted).

1088

1089

- **src**: Input image

1090

- **dst**: Accumulator image (same channels and depth as src)

1091

- **alpha**: Weight of input image (dst = (1-alpha)*dst + alpha*src)

1092

- **mask**: Optional operation mask

1093

- **Returns**: None (dst modified in-place)

1094

1095

```python { .api }

1096

cv2.createHanningWindow(winSize, type) -> dst

1097

```

1098

1099

Creates Hanning window (used for DFT-based trackers).

1100

1101

- **winSize**: Window size

1102

- **type**: Created matrix type

1103

- **Returns**: Hanning window

1104

1105

```python { .api }

1106

cv2.phaseCorrelate(src1, src2, window=None) -> retval, response

1107

```

1108

1109

Detects translational shift between two images using phase correlation.

1110

1111

- **src1**: First source floating-point array (grayscale or single color channel)

1112

- **src2**: Second source array (same size and type as src1)

1113

- **window**: Optional Hanning window

1114

- **Returns**: Tuple of (detected phase shift, peak response value)

1115