Python Articles

Page 195 of 855

How to fit the ellipse to an object in an image using OpenCV Python?

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

We can fit an ellipse to an object using the function cv2.fitEllipse(). The ellipse is inscribed in a rotated rectangle, which is a bounding rectangle with minimum area enclosing the object. Syntax The syntax for fitting an ellipse to a contour is − ellipse = cv2.fitEllipse(cnt) Where cnt is the contour points represented as an array of contour points. Output − It returns a tuple in ((x, y), (majorAxis, minorAxis), angle) format where: (x, y) is the coordinates of the center (majorAxis, minorAxis) are the lengths of major and minor axes angle ...

Read More

How to create a watermark on an image using OpenCV Python?

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

To add a watermark to an image, we use the cv2.addWeighted() function from OpenCV. This technique blends two images together by applying different opacity levels to create a semi-transparent watermark effect. Step-by-Step Process Here's the complete process to create a watermark on an image − Step 1: Import Required Libraries Import OpenCV library for image processing ? import cv2 import numpy as np Step 2: Read Images Load the main image and watermark image ? # Read the main image img = cv2.imread("main_image.jpg") # Read the watermark image ...

Read More

How to find the minimum enclosing circle of an object in OpenCV Python?

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

A minimum enclosing circle (circumcircle) of an object is a circle which completely covers the object with minimum area. We can find the minimum enclosing circle of an object using the function cv2.minEnclosingCircle(). Syntax The syntax of this function is ? (x, y), radius = cv2.minEnclosingCircle(cnt) Where cnt are the contour points represented as an array of contour points. Output ? It returns coordinate of center (x, y) and radius of minimum enclosing circle. Both (x, y) and radius are of float dtype. So, to draw a circle on the image, we convert ...

Read More

How to find and draw Convex Hull of an image contour in OpenCV Python?

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

A Convex Hull is a convex curve that wraps around an object, similar to stretching a rubber band around the shape. Unlike contour approximation, a convex hull is always bulged outward or flat, never curved inward. It finds and corrects convexity defects in the original contour. Syntax To find the convex hull, we use the following function: hull = cv2.convexHull(cnt, hull, clockwise, returnPoints) Parameters cnt − The input contour points as an array hull − Output parameter, normally omitted clockwise − Orientation flag. ...

Read More

How to compute Hu-Moments of an image in OpenCV Python?

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

The Hu-Moments can be computed using the cv2.HuMoments() function in OpenCV. It returns seven moments invariant to translation, rotation, and scale, with the seventh moment being skew-invariant. To compute Hu-Moments, we first need to find the contours of objects in the image. Image moments are calculated for objects using their contours, so we detect contours and apply cv2.moments() to compute the moments. Syntax The following syntax is used for this function — M = cv2.moments(cnt) hu_moments = cv2.HuMoments(M) Parameters cnt — A numpy array of the contour points of an object ...

Read More

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
Showing 1941–1950 of 8,549 articles
« Prev 1 193 194 195 196 197 855 Next »
Advertisements