Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 173 of 2547
How to compute the aspect ratio of an object in an image using OpenCV Python?
The aspect ratio of an object is computed as the ratio between the width and height of its bounding rectangle. To calculate this, we first find the bounding rectangle using OpenCV's cv2.boundingRect() function. Syntax x, y, w, h = cv2.boundingRect(contour) aspect_ratio = float(w) / h Here, contour is a numpy array containing the contour points of an object in the image. Step-by-Step Process Follow these steps to compute the aspect ratio of objects in an image: 1. Import Required Libraries import cv2 import numpy as np 2. Load ...
Read MoreHow to compute the extent of an object in image using OpenCV Python?
The extent of an object is computed as the ratio of contour area to its bounding rectangle area. This metric helps determine how well an object fills its bounding rectangle, with values closer to 1 indicating the object occupies most of the rectangle space. Syntax The extent can be computed as follows − area = cv2.contourArea(cnt) x, y, w, h = cv2.boundingRect(cnt) rect_area = w*h extent = float(area)/rect_area Here, cnt is a numpy array of the contour points of an object in the image. Step-by-Step Process Follow these steps to compute extent ...
Read MoreHow to fit the ellipse to an object in an image using OpenCV Python?
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 MoreHow to create a watermark on an image using OpenCV Python?
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 MoreHow to find the minimum enclosing circle of an object in OpenCV Python?
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 MoreHow to find and draw Convex Hull of an image contour in OpenCV Python?
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 MoreHow to compute Hu-Moments of an image in OpenCV Python?
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 MoreHow to detect a rectangle and square in an image using OpenCV Python?
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 MoreHow to draw filled ellipses in OpenCV using Python?
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 MoreHow to approximate a contour shape in an image using OpenCV Python?
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