Programming Articles - Page 588 of 3363

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

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

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

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

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

How to match image shapes in OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 07:11:54

7K+ Views

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

How to find Laplassian pyramids for an image using OpenCV in Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 07:09:23

1K+ Views

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

How to find Gaussian pyramids for an image using OpenCV in Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 06:58:20

1K+ Views

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

How to find image gradients using the Scharr operator in OpenCV Python?

Shahid Akhtar Khan
Updated on 28-Sep-2022 06:54:05

1K+ Views

Using the Scharr operator, we can compute image gradients in horizontal as well as vertical direction using first order derivatives. The gradients are computed for a grayscale image. You can apply Scharr operation on an image using the method cv2.scharr(). Syntax The following syntax is used to compute the image gradients using Scharr derivative − cv2.Scharr(img, ddepth, xorder, yorder) Parameters img − The original input image ddepth − Desired depth of the output image. It has information about what kind of data is stored in the output image. We use cv2.CV_64F to as ddepth. It is a 64bit ... Read More

How to draw polylines on an image in OpenCV using Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 14:01:39

5K+ Views

To draw polylines on an image, we use the method cv2.polylines(). We can draw open or closed polylines on the image. The first and last points are not connected while drawing an open polyline. Syntax The syntax of cv2.polylines() is as follows − cv2.polylines(src, [pts], isClosed, color, thickness) Parameters src − It's the input image on which the polylines to be drawn. pts − List of the array of pints. isClosed − Set isClosed=True to draw a closed polyline, for an open polyline set isClosed=False. color − It is the color of the line. thickness − Its thickness ... Read More

How to find the image gradients using Sobel and Laplacian derivatives in OpenCV Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 13:51:22

2K+ Views

Using the Sobel operator, we can compute image gradients in horizontal as well as vertical direction. The gradients are computed for a grayscale image. The Laplacian operator computes the gradients using the second-order derivatives. Syntax The following syntaxes are used to compute the image gradients using Sobel and Laplacian derivatives − cv2.Sobel(img, ddepth, xorder, yorder, ksize) cv2.Laplacian(img, ddepth) Parameters img − The original input image. ddepth − Desired depth of the output image. It has information about what kind of data is stored in the output image. We use cv2.CV_64F to as ddepth. It is a 64bit floating-point ... Read More

Advertisements