Found 10784 Articles for Python

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

Shahid Akhtar Khan
Updated on 28-Sep-2022 09:01:38

4K+ 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
Updated on 28-Sep-2022 08:53:51

3K+ 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 detect a rectangle and square in an image using OpenCV Python?

Shahid Akhtar Khan
Updated on 26-Aug-2023 08:26:18

35K+ Views

To detect a rectangle and square in an image, we first detect all the contours in the image. Then Loop over all contours. Find the approximate contour for each of the contours. If the number of vertex points in the approximate contour is 4 then we compute the aspect ratio to make a difference between the rectangle and square. If the aspect ratio is between 0.9 and 1.1 we say it is a square else a rectangle See the below pseudocode. for cnt in contours: approx = cv2.approxPolyDP(cnt) if len(approx) == 4: x, y, w, h = ... Read More

How to draw filled ellipses in OpenCV using Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 08:32:23

2K+ 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

How to detect a triangle in an image using OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 08:28:56

4K+ Views

To detect a triangle in an image, we first detect all the contours in the image. Then we loop over all the contours. Find the approximate contour for each of the contours. If the number of vertex points in the approximate contour is 3, then draw the contour and set it as a triangle. See the below pseudocode. for cnt in contours: approx = cv2.approxPolyDP() if len(approx) == 3: cv2.drawContours() cv2.putText("Triangle") Steps You can use the following steps to detect a ... Read More

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

Shahid Akhtar Khan
Updated on 28-Sep-2022 12:50:30

6K+ Views

The function cv2.approxPolyDP() approximates a contour shape to another shape with less number of vertices. It accepts the following arguments − cnt − The array of the contour points. epsilon − Maximum distance from contour to approximated contour. A wise selection of epsilon is needed to get the correct output. SyntaxThe following syntax are used to approximate a contour shape epsilon = 0.01*cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) Steps You can use the following steps to approximate a contour shape in an image − Import the required library. In all the following Python examples, the required ... Read More

How to find the bounding rectangle of an image contour in OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Aug-2023 13:57:14

25K+ Views

The bounding rectangle of an object is a rectangle drawn around an object in the image. There are two methods to find the bounding rectangle in OpenCV − Straight Bounding Rectangle It is a straight rectangle as it does not consider the rotation of an object. It can be computed using the function cv2.boundingRect(). Its syntax is as follows − x, y, w, h = cv2.boundingRect(cnt) img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) Here, "cnt" is an array of the contour points. It returns the top-left coordinate (x, y) and width and height (w, h) ... Read More

How to perform bitwise XOR operation on images in OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 08:13:59

2K+ Views

Color images (RGB) have three channels: red, blue and green. The image is represented as a 3-dimensional numpy array. The pixel values of an image are stored using 8-bit unsigned integers (uint8) in the range "0 to 255". The bitwise XOR operation on two images is performed on the binary representation of these pixel values of corresponding images. Here is the syntax to perform bitwise XOR operation on two images − cv2.bitwise_xor(img1, img2, mask=None) Here, img1 and img2 are the two input images and mask is a mask operation. Steps To compute bitwise XOR between two images, you can ... Read More

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

Shahid Akhtar Khan
Updated on 28-Sep-2022 08:09:54

1K+ 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, etc. 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. Steps You can use the following steps to check if a contour ... Read More

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

Shahid Akhtar Khan
Updated on 28-Sep-2022 12:42:42

12K+ 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

Advertisements