Found 184 Articles for OpenCV

How to detect a rectangle and square in an image using OpenCV Python?

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

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

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

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

How to compare two images in OpenCV Python?

Shahid Akhtar Khan
Updated on 23-Aug-2023 14:06:47

61K+ Views

To compare two images, we use the Mean Square Error (MSE) of the pixel values of the two images. Similar images will have less mean square error value. Using this method, we can compare two images having the same height, width and number of channels. Steps You can use the following steps to compare two images using OpenCV − 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 images using cv2.imread() and convert it to grayscale. The height, width and ... Read More

How to Compute Image Moments in OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 07:17:39

4K+ Views

Image Moments are very important to compute the features like center of mass of an object, area of an object, etc., in a given image. Image moments are computed for an object using the contour of the object. So first, we detect the contour of the object, then apply cv2.moments(cnt) function to compute the moments. Syntax This is the syntax used for the function − cv2.moments(cnt) Where, "cnt" is a numpy array of the contour points of an object in the image. Steps You can use the following steps to compute the moments in an image − Import the ... Read More

Previous 1 ... 6 7 8 9 10 ... 19 Next
Advertisements