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
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
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
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
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
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
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
We use cv2.matchShapes() function to match two image shapes. This function returns a metric showing the similarity between the image shapes. This function uses Hu-Moments to calculate the metric value. Lower the metric value, higher the similarity between the image shapes. In the following examples, we will match the shapes from different images and also shapes from a single image. Syntax We use the following syntax to match two image shapes − ret = cv2.matchShapes(cnt1, cnt1, 1, 0.0) Where, cnt1 − The contour points of the first image shape. cnt2 − The contour points of the second image ... Read More
We can form the Laplacian Pyramids from the Gaussian Pyramids. OpenCV does not provide any specific function to construct Laplacian Pyramids. In Laplacian pyramid, images look like edge images only. Laplacian Pyramids are used in image compression as well as in image enhancement. A level in the Laplacian Pyramid is formed by the difference between that level in the Gaussian Pyramid and the expanded version of its upper level in the Gaussian Pyramid. To create a level in the Gaussian pyramid, we apply the cv2.pyrDown() or cv2.pyrUp() function. Steps To construct a three-level Laplacian pyramid, follow the steps given below ... Read More
In many cases, we need to work with different resolutions and sizes of the same image. In the concept of image pyramid, we find images with different resolutions and sizes of the original image. The Gaussian pyramid is a type of image pyramid. To find a Gaussian pyramid, OpenCV provides us two functions cv2.pyrDown() and cv2.pyrUp(). The function cv2.pyrDown() decreases the resolution by removing the consecutive rows and columns in the input image. The width and height of the output image become half the input image decreasing the area to one-fourth. The function cv2.pyrUp() increases the resolution by adding ... Read More