This is part of “Make my first iPhone app in 16 weeks” series.

# Erosion and Dilation

- Erosion – Convolving the kernel over the image and replacing the pixel with the local maximum. It makes the brighter area dilated.
- Erosion – Convolving the kernel over the image and replacing the pixel with the local minimum. It makes the brighter area eroded (darker part gets dilated)

## Erosion

### Build Kernel

Mat kern = getStructuringElement(morph_type,
Size(2*erosion_size + 1, 2*erosion_size+1),
Point(erosion_size, erosion_size));

### Use erode() function

erode( src image, destination image, kern );

## Dilation

### Build Kernel

Mat kern = getStructuringElement(morph_type,
Size(2*dilation_size + 1, 2*dilation_size+1),
Point(dilation_size, dilation_size));

### Use dilate() function

dilate( src image, destination image, kern );

**** There are 3 morph types**

- Rectangular box: MORPH_RECT
- Cross: MORPH_CROSS
- Ellipse: MORPH_ELLIPSE

# morphologyEx

## Operations

- opening – erode and dilate
- closing – dilate and erode
- morphological gradient – dilation – erosion
- top hat – original image – opening
- black hat – closing – original image

### Use morphologyEx() function

morphologyEx( src, dst, operation, element );

# Image Pyramids

Resizing images – upsampling or downsampling

pyrUp( current image, destination image, Size( tmp.cols*2, tmp.rows*2 )

pyrDown( current image, destination image, Size( tmp.cols/2, tmp.rows/2 )

# Threshold

## Types of thresholding operations

- Binary – if the intensity of the pixel is
**higher** than threshold, it’s set to **max** value and if it’s **lower**, it’s set to **zero**
- Binary, Inverted – if the intensity of the pixel is
**higher** than threshold, it’s set to **zero** and if it’s **lower**, it’s set to **max** value
- Truncate – if the intensity of the pixel is
**higher** than threshold, it’s set to **threshold** and if it’s **lower**, **nothing** happens
- Threshold to Zero – if the intensity of the pixel is
**higher** than threshold, **nothing** happens and if it’s **lower**, it’s set to **zero**
- Threshold to Zero, Inverted – if the intensity of the pixel is
**higher** than threshold, it’s set to **zero**. If it’s **lower**, **nothing** happens.

threshold( grayscale original image, destination image, threshold_value, max_BINARY_value, type of thresholding operation );

# Linear Filters

### Initialize the arguments for the linear filter

anchor = Point( -1, -1 ); // anchor of kernel
delta = 0; // A value to be added to each pixel during the convolution.
ddepth = -1; // depth of destination

### Define kernel

kernel_size = 3 + 2*( ind%5 );
kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size);

### Use filter2D() function

filter2D(source image, destination image, ddepth , kernel, anchor, delta, BORDER_DEFAULT );

# Make Borders

### Border types

- BORDER_CONSTANT – a set color for the border
- BORDER_REPLICATE – copies over the row or column at the very edge of the original to the extra border.

### Define the thickness of top, bottom, left and right borders and the color of the border (if BORDER_CONSTANT)

### Use copyMakeBorder() function

copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );

# EDGE DETECTION

# Sobel and Scharr – Edge detection using first derivative

### Sobel

Sobel( grayscale source image,
destination x derivative image,
ddepth,
1,
0,
3,
scale,
delta,
BORDER_DEFAULT );
convertScaleAbs( grad_x,
abs_grad_x );
Sobel( grayscale source image,
destination y derivative image,
ddepth,
0,
1,
3,
scale,
delta,
BORDER_DEFAULT );
convertScaleAbs( grad_y,
abs_grad_y );

### Scharr – More accurate than Sobel

Scharr( grayscale source image,
destination x derivative image,
ddepth,
1,
0,
scale,
delta,
BORDER_DEFAULT ); //ddepth is CV_16S here
convertScaleAbs( destination x derivative image,
abs_grad_x ); // change back to CV_8U
Scharr( grayscale source image,
destination y derivative image,
ddepth,
0,
1,
scale,
delta,
BORDER_DEFAULT ); //ddepth is CV_16S here
convertScaleAbs( destination y derivative image,
abs_grad_y ); // change back to CV_8U

/// Approximate total gradient
addWeighted( abs_grad_x,
0.5,
abs_grad_y,
0.5,
0,
destination image );

# Laplace – Edge detection using second derivative

Laplacian( grayscale source image,
destination image,
ddepth, // CV_16S
kernel_size,
scale,
delta,
BORDER_DEFAULT );
convertScaleAbs( dst,
abs_dst );

# Canny – Optimal Edge detection using Hysteresis

Canny( blurred image, destination image with edges, lowThreshold, highThreshold, kernel_size );

# LINE & SHAPE DETECTION

# Standard Hough Line Transform – Straight line

### 1. First apply Canny

### 2. Use HoughLines()

vector lines;
HoughLines( resulting grayscale image from Canny,
lines,
rho, // The resolution of the parameter r in pixels. We use 1 pixel.
theta, // The resolution of the parameter \theta in radians. We use 1 degree (CV_PI/180)
threshold, // The minimum number of intersections to “detect” a line
srn, // Default is zero
stn // Default is zero
);

# Probabilistic Hough Line Transform – Straight line

### 1. First apply Canny

### 2. Use HoughLinesP()

vector lines;
HoughLinesP( resulting grayscale image from Canny,
lines,
rho, // The resolution of the parameter r in pixels. We use 1 pixel.
theta, // The resolution of the parameter \theta in radians. We use 1 degree (CV_PI/180)
threshold, // The minimum number of intersections to “detect” a line
minLineLength, // The minimum number of points that can form a line. Lines with less than this number of points are disregarded.
maxLineLength // The maximum gap between two points to be considered in the same line.
);

# Hough Circle Transform

Mostly the same as straight line detection.

HoughCircles( grayscale source image,
destination images,
CV_HOUGH_GRADIENT,
inverse ratio of resolution, // 1
min_dist between detected centers, // src_gray.rows/8
upper threshold for the internal Canny edge detector,
threshold for center detection,
minimum radius to be detected, // if unknown, put zero
maximum radius to be detected, // if unknown, put zero
);

Then draw detected circles.

# Remapping

### Use remap() function