# Image Manipulation OpenCV Python

OpenCV (tutorial) (Computer Vision) is widely used for Image Manipulation. Image manipulation OpenCV techniques help us to read different data from the image.

This blog post will provide you important image manipulation techniques in OpenCV Python.

## Image Manipulation OpenCV

1. Transformation, affine, and nonaffine
2. Translations
3. Rotations
4. Scaling, re-sizing, and Interpolations
5. Image Pyramids
6. Cropping
7. Arithmetic Operations
9. Convolution and Blurring
10. Sharpening
11. Thresholding and Binarization
12. Dilation, Erosion, Opening, and Closing
13. Edge Detection and Image Gradients
14. Perspective and Affine Transformation

Face Recognition

Lane Detection in self Driving Car

## Transformations OpenCV Python

Transformations are Geometric distortions enacted upon an image.

We use transformations to correct distortions or perspective issues arising from the point of view of an image that was captured.

• Affine
• Non-Affine

## Translations OpenCV Python

This is an affine transform that simply shifts the position of an image.

We Use cv2.wrapAffine() to implement these transformations.

## Rotation OpenCV Python

cv2.getRotationMatrix2D(rotation_center_x , rotation_center_y , angle of rotation, scale)

## OpenCV Python Re-Sizing, Scaling, and Interpolation

### What is OpenCV Interpolation?

Interpolation is a method of constructing new data points within the range of a discrete set of known data points.

• cv2.INTER_AREA (Good for shrinking or downsampling)
• cv2.INTER_NEAREST (Fastest)
• cv2.INTER_LINEAR (Good for Zooming or up Sampling) (Default)
• cv2.INTER_CUBIC (Better)
• cv2.INTER_LANCZOS4 (Best)

Let’s make Image 3/4th of its original size.

img_scaled = cv2.resize(image, None, fx=0.75, fy=0.75)

Let’s Double the size of our image.

img_scaled = cv2.resize(image, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC)

Let’s Re-size by setting Exact Dimensions.

img_scaled = cv2.resize(image, (900,400), interpolation = cv2.INTER_AREA)

## Pyramids OpenCV Python

Pyramiding image refers to either upscaling (enlarging) and downscaling (Shrinking images).

It’s simply a different way of re-sizing that allows us to easily and quickly scale images. Scaling down reduces the height and width of the new image by half.

This comes in use when making object detectors that scale images each time it looks for an object.

smaller = cv2.pyrDown(image)

larger = cv2.pyrUp(image)

## Cropping OpenCV Python

Cropping refers to extracting a segment of an Image.

height , width = image.shape[:2]

start_row, start_col = int( height * 0.25 ) , int(width * 0.25)

end_row, end_col = int( height * 0.75 ) , int(width * 0.75)

cropped_image = image[start_row : end_row , start_col : end_col]

## Convolution OpenCV Python

A Convolution is a mathematical operation performed on two functions producing a third function which is typically a modified version of one of the original functions.

Output Image = Image * Function (Kernel Size)

In Computer Vision, we use Kernel’s to specify the size over which we run our manipulation function over our image.

## Blurring OpenCV Python

Blurring is an Operation where we average the pixels within a region (Kernel).

Kernel = 1/25 [1’s metrix of 5 x 5]

We Multiply by 1/25 to normalize i.e sum to 1, otherwise, we would be increasing intensity.

Creating 3×3 kernel

kernel = np.ones((3,3), np.float32)/9

blurring

blurred_image = cv2.filter2D(image, -1, kernel)

### Types of Blurring

• cv2.blur —–> Averages value over a specified window
• cv2.GaussianBlur —-> similar, but uses a Gaussian window (more emphasis or weighting on points around the center)
• cv2.medianBlur —-> Uses median of all Elements in the window.
• cv2.bilateralFilter —-> Blur while keeping edges sharp. It also takes a Gaussian Filter in space, but one more Gaussian filter which is a function of a pixel different. The pixel different function makes sure only those pixels with similar intensity to the central pixel is considered for blurring. So it preserves the edges since pixel at edges will have large intensity variation.

## Sharpening OpenCV Python

Sharpening is the opposite of blurring, it strengthens or emphasizes edges in an image.

sharpened_image = cv2.filter2D(image, -1, kernel)

## Thresholding, Binarization & Adaptive Thresholding

Thresholding is the act of converting an image into a binary form.

cv2.threshold(image, threshold value, Max value, Type)

### Types of Threshold

• cv2.THRESH_BINARY (Most Common)
• cv2.THRESH_BINARY_INV
• cv2.THRESH_TRUNC
• cv2.THRESH_TOZERO
• cv2.THRESH_TOZERO_INV

Note: The image needs to be converted to grayscale before thresholding.

• Simple Thresholding method requires us to provide the threshold value
• Adaptive threshold methods take that uncertainty away.

cv2.adaptiveThreshold(img, max value, Adaptive type, threshold type, Block size, constant that is substracted from mean)

#### Types of Adaptive Thresholding OpenCV Python

• ADAPTIVE_THRESH_MEAN_C —> Based on the mean of the neighborhood of pixels.
• ADAPTIVE_THRESH_GAUSSIAN_C –> Weighted sum of neighborhood pixels under the Gaussian Window.
• THRESH_OTSU

## Image Manipulation – Dilation and Erosion in OpenCV

There are operations in the field of mathematical morphology.

• Dilation adds pixels to the boundaries of objects in an image.
• ErosionRemoves pixels at the boundaries of the object in an image.
• OpeningErosion followed by Dilation
• ClosingDilation followed by Erosion

## Edge Detection and Image Gradients

Learn how to make a live sketch using OpenCV in Python.

Edges can be defined as sudden changes (discontinuities) in an image as they can encode just as much information as pixels.

### OpenCV Edge Detection Algorithms

• Sobel – to emphasize vertical or horizontal edges
• Laplacian – Gets all orientations
• Canny – Optimal due to low error rate, well-defined edges and accurate detection.

#### OpenCV Canny Edge Detection

Developed by John F. Canny in 1986.

1. Applies Gaussian Blurring
2. Finds intensity Gradient of the image
3. Applied non-maximum suppressions (i.e removes pixels that are not edges)
4. Hysteresis – Applies thresholds (i.e If Pixel is within the upper and lower threshold, it is considered an edge)

## Image Manipulation OpenCV GitHub 