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
Articles by Shahid Akhtar Khan
Page 4 of 17
OpenCV Python – How to find and draw extreme points of an object on an image?
Finding and drawing extreme points of objects in an image is useful for shape analysis and object detection. Extreme points are the leftmost, rightmost, topmost, and bottommost coordinates of a contour. Algorithm Steps To find and draw extreme points of objects, follow these steps: Load and preprocess − Read the input image using cv2.imread() and convert it to grayscale. Apply thresholding − Create a binary image using cv2.threshold() for better contour detection. Find contours − Detect object boundaries using cv2.findContours(). Calculate extreme points − For each contour, find the leftmost, rightmost, topmost, and bottommost points. Draw ...
Read MoreOpenCV Python – How to draw circles using Mouse Events?
OpenCV provides various mouse events to interact with images, such as cv2.EVENT_LBUTTONDOWN for left button click, cv2.EVENT_RBUTTONDOWN for right button click, and cv2.EVENT_LBUTTONDBLCLK for double-click. These events return mouse coordinates (x, y) which we can use to draw shapes like circles through callback functions. Steps to Draw Circles with Mouse Events To draw circles using mouse events, follow these steps ? Import the required library OpenCV and NumPy. Create a black image or read an existing image using cv2.imread(). Define a mouse callback function that draws circles when mouse events occur. Create a window and bind ...
Read MoreHow to find the solidity and equivalent diameter of an object in an image using OpenCV Python?
Solidity is the ratio of contour area to its convex hull area, measuring how "solid" or compact an object is. Equivalent diameter is the diameter of a circle with the same area as the contour. Both properties help analyze object shape characteristics in computer vision. Understanding the Concepts To compute solidity and equivalent diameter, we need ? Contour area ? area enclosed by the object boundary Convex hull ? smallest convex polygon containing all contour points Hull area ? area of the convex hull ...
Read MoreHow 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 More