Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Shahid Akhtar Khan
Page 6 of 17
How to check if an image contour is convex or not in OpenCV Python?
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 MoreHow to Compute Image Moments in OpenCV Python?
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 MoreHow to match image shapes in OpenCV Python?
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 MoreHow to find Laplassian pyramids for an image using OpenCV in Python?
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 MoreHow to find the image gradients using Sobel and Laplacian derivatives in OpenCV Python?
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 MoreHow to apply custom filters to images (2D convolution) using OpenCV Python?
In this tutorial, we will see how to apply two different low-pass filters to smooth (remove noise from) the image. The two filters are filter2D and boxFilter. These filters are 2D filters in space. Applying 2D filters to images is also known as the "2D Convolution operation". These filters are commonly referred to as averaging filters. The main disadvantage of these filters is that they also smooth the edges in the image. If you don't want to smooth the edges, you can apply a "bilateral filter". A bilateral filter operation preserves the edges.Syntax Following are the syntaxes of Filter2D and ...
Read MoreHow to apply Perspective Transformations on an image using OpenCV Python?
In Perspective Transformation, the straight lines remain straight even after the transformation. To apply a perspective transformation, we need a 3×3 perspective transformation matrix. We need four points on the input image and corresponding four points on the output image. We apply the cv2.getPerspectiveTransform() method to find the transformation matrix. Its syntax is as follows − M = cv2.getPerspectiveTransform(pts1, pts2) where, pts1 − An array of four points on the input image and pts2 − An array of corresponding four points on the output image. The Perspective Transformation matrix M is a numpy array. We pass M ...
Read MoreHow to apply Affine Transformation on an image in OpenCV Python?
In Affine Transformation, all parallel lines in the original image will still be parallel in the output image. To apply affine transformation on an image, we need three points on the input image and corresponding point on the output image. So first, we define these points and pass to the function cv2.getAffineTransform(). It will create a 2×3 matrix, we term it a transformation matrix M. We can find the transformation matrix M using the following syntax − M = cv2.getAffineTransform(pts1, pts2) Where pts1 is an array of three points on the input image and pts2 is an array of ...
Read MoreHow to plot histograms of different colors of an image in OpenCV Python?
To compute the histogram in OpenCV, we use the cv2.calcHist() function. In this tutorial, we will show how to compute the histogram for different colors (Blue, Green, and Red) of the input image. To compute and plot the histograms of different colors of an image, one could follow the steps given below − Steps Import the required libraries OpenCV and matplotlib. Make sure you have already installed them. import cv2 import matplotlib.pyplot as plt Read the images using cv2.imread() method. The width and height of images must be the same. img1 = cv2.imread('birds.jpg') Compute the histograms for different ...
Read MoreHow to perform image translation using OpenCV in Python?
Shifting the image location in a particular direction is referred to as Image Translation. To perform image translation, we should first understand what is a translation matrix and how to define it using OpenCV and NumPy. If we want to make a translation in (x, y) direction, let it be (tx, ty). tx is the shift in horizontal direction and ty is the shift in vertical direction. Using (tx, ty), we can define the translation matrix M as below − M = np.float32([[1, 0, tx], [0, 1, ty]]) The translation matrix M is a numpy array of type np.float32. ...
Read More