Shahid Akhtar Khan

Shahid Akhtar Khan

169 Articles Published

Articles by Shahid Akhtar Khan

Page 5 of 17

How to detect a rectangle and square in an image using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 45K+ Views

To detect rectangles and squares in an image using OpenCV Python, we analyze contours and calculate aspect ratios. A square has an aspect ratio close to 1.0, while rectangles have ratios significantly different from 1.0. Algorithm Overview The detection process follows these key steps: for cnt in contours: approx = cv2.approxPolyDP(cnt, epsilon, True) if len(approx) == 4: x, y, w, h = cv2.boundingRect(cnt) ratio = float(w)/h if ratio >= 0.9 and ratio

Read More

How to draw filled ellipses in OpenCV using Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 3K+ Views

To draw a filled ellipse on an image, we use the cv2.ellipse() method. This method accepts different arguments to draw different types of ellipses with various shapes, sizes, and orientations. Syntax cv2.ellipse(img, center, axes, angle, start_angle, end_angle, color, thickness) Parameters img − The input image on which the ellipse is to be drawn. center − The center coordinate of the ellipse as (x, y). axes − A tuple in (major axis length, minor axis length) format. angle − The rotation angle of an ellipse in degrees. start_angle − The starting angle of the ...

Read More

How to approximate a contour shape in an image using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 8K+ Views

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

How to check if an image contour is convex or not in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 2K+ Views

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

How to compute the area and perimeter of an image contour using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 16K+ Views

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

How to compare two images in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 77K+ Views

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

How to Compute Image Moments in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 6K+ Views

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

How to match image shapes in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 8K+ Views

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

How to find Laplassian pyramids for an image using OpenCV in Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 2K+ Views

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

How to find the image gradients using Sobel and Laplacian derivatives in OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 2K+ Views

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
Showing 41–50 of 169 articles
« Prev 1 3 4 5 6 7 17 Next »
Advertisements