Server Side Programming Articles - Page 467 of 2650

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

Shahid Akhtar Khan
Updated on 28-Sep-2022 10:08:55

11K+ 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 perform bitwise OR operation on two images in OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 10:05:46

1K+ Views

In OpenCV, a color (RGB) image is represented as a 3-dimensional numpy array. The pixel values of an image are stored using 8 bit unsigned integers (uint8) in range from 0 to 255. The bitwise OR operation on two images is performed on the binary representation of these pixel values of corresponding images. Syntax Here is the syntax to perform bitwise OR operation on two images − cv2.bitwise_or(img1, img2, mask=None) img1 and img2 are the two input images and mask is a mask operation. Steps To compute bitwise OR between two images, you can use the steps given below ... Read More

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

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

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
Updated on 28-Sep-2022 09:06:00

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
Updated on 28-Sep-2022 09:01:38

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

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

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

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

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

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

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

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

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

Advertisements