The function cv2.approxPolyDP() approximates a contour shape to another shape with fewer vertices. This is useful for simplifying complex shapes or detecting specific geometric forms like rectangles, triangles, or polygons. Syntax approx = cv2.approxPolyDP(contour, epsilon, closed) Parameters contour − The array of contour points to be approximated epsilon − Maximum distance from contour to approximated contour. Usually calculated as a percentage of the contour perimeter closed − Boolean flag indicating whether the contour is closed (True) or open (False) Complete Example Here's a complete example that creates a sample image ... Read More
The function cv2.isContourConvex() is used to check whether a curve (contour) is convex or not. A contour of an object in the image is a curve joining all the continuous points along the boundary, having the same color or intensity. Contours are used for shape analysis and object detection and recognition. Syntax The syntax for cv2.isContourConvex() is − cv2.isContourConvex(cnt) Where, "cnt" is a numpy array of the contour points of an object in the image. It returns True if the contour cnt is convex, else False. Understanding Convex vs Non-Convex Shapes A ... Read More
OpenCV provides powerful functions to compute the area and perimeter of image contours. A contour is a curve joining all continuous points along a boundary with the same color or intensity, making them essential for shape analysis and object detection. To compute area and perimeter, we first detect contours using cv2.findContours(), then apply cv2.contourArea() and cv2.arcLength() functions respectively. Syntax The functions use the following syntax: area = cv2.contourArea(cnt) perimeter = cv2.arcLength(cnt, True) Where cnt is a NumPy array containing the contour points of an object. Steps to Compute Area and Perimeter ... Read More
To compare two images, we use the Mean Square Error (MSE) of the pixel values of the two images. Similar images will have less mean square error value. Using this method, we can compare two images having the same height, width and number of channels. What is Mean Square Error? Mean Square Error (MSE) measures the average squared differences between corresponding pixels of two images. Lower MSE values indicate greater similarity between images. Steps to Compare Images You can use the following steps to compare two images using OpenCV − Step 1: Import the required ... Read More
Image moments are statistical measures that describe the shape and size characteristics of objects in an image. They are essential for computing features like center of mass, area, and orientation of objects. In OpenCV, image moments are calculated using contours of detected objects. Syntax The basic syntax for computing image moments is ? cv2.moments(contour) Where contour is a NumPy array containing the contour points of an object. Understanding Image Moments Image moments provide valuable information about objects ? m00 ? Area of the object m10, m01 ? First-order moments used ... Read More
We use cv2.matchShapes() function to match two image shapes in OpenCV. This function returns a metric showing the similarity between the image shapes using Hu-Moments to calculate the metric value. Lower the metric value, higher the similarity between the image shapes. In the following examples, we will match the shapes from different images and also shapes from a single image. Syntax We use the following syntax to match two image shapes − ret = cv2.matchShapes(cnt1, cnt2, method, parameter) Where, cnt1 − The contour points of the first image ... Read More
We can form Laplacian Pyramids from Gaussian Pyramids in OpenCV. While OpenCV doesn't provide a direct function to construct Laplacian Pyramids, we can create them by computing differences between Gaussian pyramid levels. In a Laplacian pyramid, images appear as edge-like representations and are commonly used in image compression and image enhancement applications. How Laplacian Pyramids Work A level in the Laplacian Pyramid is formed by the difference between that level in the Gaussian Pyramid and the expanded version of its upper level. The process involves: Creating a Gaussian pyramid using cv2.pyrDown() Expanding higher levels using ... Read More
Image gradients are essential in computer vision for detecting edges and boundaries. OpenCV provides Sobel and Laplacian operators to compute these gradients. The Sobel operator uses first-order derivatives to find gradients in horizontal and vertical directions, while the Laplacian operator uses second-order derivatives. Syntax The following syntaxes are used to compute image gradients ? cv2.Sobel(img, ddepth, dx, dy, ksize) cv2.Laplacian(img, ddepth) Parameters img − The input grayscale image. ddepth − Output image depth. Use cv2.CV_64F for 64-bit floating-point precision. dx − Order of derivative in X-direction (horizontal). Set dx=1, dy=0 for horizontal ... Read More
In this tutorial, we will learn how to apply custom filters to images using OpenCV Python. We'll explore two different low-pass filters: filter2D and boxFilter. These filters perform 2D convolution operations to smooth images and remove noise. Applying 2D filters to images is also known as the "2D Convolution operation". These filters are commonly referred to as averaging filters. The main disadvantage of these filters is that they also smooth the edges in the image. If you don't want to smooth the edges, you can apply a "bilateral filter" that preserves edges. Syntax Following are the syntaxes ... Read More
In Perspective Transformation, straight lines remain straight even after the transformation. To apply a perspective transformation, we need a 3×3 perspective transformation matrix and four points on both the input and output images. Key Functions We use cv2.getPerspectiveTransform() to find the transformation matrix − M = cv2.getPerspectiveTransform(pts1, pts2) Where: pts1 − An array of four points on the input image pts2 − An array of corresponding four points on the output image To apply the transformation, we use cv2.warpPerspective() − dst = cv2.warpPerspective(img, M, (width, height)) Where: ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance