Geometric Transformations on Image
Image Transformation involves the transformation of images, such as scaling, rotating, cropping, etc for further usage.
Function |
Purpose |
---|---|
resize(image, width, height, interpolation_method) |
By using this method we can resize the image at hand for specific width and height. Some of the methods that can be used for interpolation are as below:
|
M = cv2.getRotationMatrix2D(center, angle, scale) warpAffine(image, M, (width, height)) |
Image rotation is a common image processing routine with applications in matching, alignment, and other image-based algorithms, in image rotation the image is rotated by a definite angle. We can also use cv2.rotate() function for image rotation. |
matrix = cv2.getPerspectiveTransform(src, dst) warpPerspective(image, matrix, dsize) | In Perspective Transformation, we can change the perspective of a given image or video for getting better insights into the required information. We need to provide the points on the image from which want to gather information by changing the perspective. We also need to provide the points inside which we want to display our image. |
Scaling
Image scaling is a process used to resize a digital image using the cv2.resize() method.
res = cv2.resize(img,(2width, 2height), interpolation)
The resize() method’s Interpolation attribute has the following types: (Scaling types)
Scaling Types |
Description |
---|---|
cv2.INTER_AREA | Used to shrink the image |
cv2.INTER_CUBIC | Bicubic interpolation |
cv2.INTER_LINEAR | Default interpolation technique used to zoom the image |
Translation
Translation refers to the rectilinear shift of an object i.e. an image from one location to another. If we know the amount of shift in the horizontal and vertical direction, then we can make a transformation matrix. Then, we can use the cv2.wrapAffine() function to implement these translations. This function requires a 2×3 array. The numpy array should be of float type.
T = np.float32([[1, 0, new_width], [0, 1, new_height]])
cv2.warpAffine(image, T, (original_width, original_height))
Rotation
Image rotation is a common image processing routine with applications in matching, alignment, and other image-based algorithms, in image rotation the image is rotated by a definite angle. We can also use cv2.rotate() function for image rotation.
M = cv2.getRotationMatrix2D(center, angle, scale)
cv2.warpAffine(image, M, (width, height))
Affline Transformation
In Affine transformation, all parallel lines in the original image will still be parallel in the output image. This can be done by using the cv2.getAfflineTransform() method.
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(img, M, (cols, rows))
Perspective Transformation
In Perspective Transformation, we can change the perspective of a given image or video to get better insights into the required information. We need to provide the points on the image from which want to gather information by changing the perspective. We also need to provide the points inside which we want to display our image.
matrix = cv2.getPerspectiveTransform(src, dst)
cv2.warpPerspective(image, matrix, dsize)
Python OpenCV Cheat Sheet
The Python OpenCV Cheat Sheet is your complete guide to mastering computer vision and image processing using Python. It’s designed to be your trusty companion, helping you quickly understand the important ideas, functions, and techniques in the OpenCV library. Whether you’re an experienced developer needing a quick reminder or a newcomer excited to start, this cheat sheet has got you covered.
In this article, we’ve gathered all the vital OpenCV concepts and explained them in simple terms. We’ve also provided practical examples to make things even clearer. You’ll learn everything from how to handle images to using advanced filters, spotting objects, and even exploring facial recognition. It’s all here to help you on your journey of discovering the amazing world of computer vision.
Table of Content
- Python OpenCV Cheat Sheet 2023
- Core Operations
- Drawing Shapes and Text on Images
- Arithmetic Operations on Images
- Morphological Operations on Images
- Geometric Transformations on Image
- Image Thresholding
- Edge/Line Detection (Features)
- Image Pyramids
- Changing the Colorspace of Images
- Smoothing Images
- Working With Videos
- Camera Calibration and 3D Reconstruction