Shahid Akhtar Khan

Shahid Akhtar Khan

169 Articles Published

Articles by Shahid Akhtar Khan

Page 5 of 17

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 16K+ Views

The contours of the objects in an image are very helpful to compute the area and perimeter of the image. A contour of an 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 etc. To compute the area and perimeter of an object, we first detect the contour of the object and then apply cv2.contourArea() and cv2.arcLength() functions respectively. Syntax The following syntax are used for the functions − area = cv2.contourArea(cnt) perimeter = cv2.arcLength(cnt, True) Where, "cnt" is ...

Read More

How to find the solidity and equivalent diameter of an object in an image using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 1K+ Views

The solidity of an object is computed as the ratio of contour area to its convex hull area. So to compute the solidity, we first have to find the contour area and convex hull area. The contour area of an object can be found using cv2.contourArea() function. Equivalent Diameter is the diameter of the circle whose area is the same as the contour area. The solidity and equivalent diameter can be computed as below − Syntax area = cv2.contourArea(cnt) hull = cv2.convexHull(cnt) hull_area = cv2.contourArea(hull) solidity = float(area)/hull_area equi_diameter = np.sqrt(4*area/np.pi) Where, cnt is a numpy array of the ...

Read More

How to compute the extent of an object in image using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 1K+ Views

The extent of an object is computed as the ratio of contour area to its bounding rectangle area. So, to compute the extent, we first have to find the contour area and bounding rectangle area. The contour area of an object can be found using cv2.contourArea() function. 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. Steps You can use the following steps to compute extent of an ...

Read More

How to compute the aspect ratio of an object in an image using OpenCV Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 5K+ Views

The aspect ratio of an object is computed as the ratio between the width and height of the bounding rectangle of the object. So, to compute the aspect ratio, we first have to find the bounding rectangle of the object. Bounding rectangle of an object can be found using cv2.boundingRect() function. It accepts the contour points of the object and returns top-left coordinate (x, y) and (width, height) of the bounding rectangle. We use the width and height to compute the aspect ratio. Syntax x, y, w, h = cv2.boundingRect(cnt) aspect_ratio = float(w)/h Here, "cnt" is a numpy array ...

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 12K+ Views

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

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 2K+ Views

To add a watermark to an image, we will use the cv2.addWeighted() function from OpenCV. You can use the following steps to create a watermark on an input image − Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it. import cv2 Read the input image on which we are going to apply the watermark and read the watermark image. img = cv2.imread("panda.jpg") wm = cv2.imread("watermark.jpg") Access the height and width of the input image, and the height, width of the watermark image. h_img, w_img ...

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 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. It is represented as an array of contour points. Output − It returns coordinate of center (x, y) and radius of minimum enclosing circle. (x, y) and radius are of float dtype. So, to draw a circle on the image, we convert them to integers. To draw ...

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 5K+ Views

A Convex Hull looks similar to contour approximation, but it is not exactly a contour approximation. A convex hull is a convex curve around an object. A convex curve is always bulged out, or at-least flat. A convex hull finds the convexity defects and corrects them. Syntax To find the convex hull, we use the following function − hull = cv2.convexHull(cnt, hull, clockwise, returnPoints) Arguments cnt are the contour points. It is represented as an array of contour points. hull is the output, normally we avoid it. clockwise − Orientation flag. If it is True, the output convex ...

Read More

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 4K+ Views

The Hu-Moments can be found using the cv2.HuMoments() function. It returns seven moments invariant to translation, rotation and scale. Seventh moment is skew-invariant. To compute the Hu-Moments, we need to first find the image. The image moments are computed for an object using the contour of the object. So, first, we detect the contour of the object and then apply cv2.moments() function to compute the moments. Syntax The following syntax is used for this function − M = cv2.moments(cnt) cv2.HuMoments(M) Here, cnt − It is a numpy array of the contour points of an object in the image. M ...

Read More

How to draw filled ellipses in OpenCV using Python?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 28-Sep-2022 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. 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. 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 elliptic arc in degrees. end_angle − The ending angle of the elliptic arc in ...

Read More
Showing 41–50 of 169 articles
« Prev 1 3 4 5 6 7 17 Next »
Advertisements