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
-
Economics & Finance
Python Articles
Page 196 of 855
How to compare two images in OpenCV Python?
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. What is Mean Square Error? Mean Square Error (MSE) measures the average squared differences between corresponding pixels of two images. Lower MSE values indicate greater similarity between images. Steps to Compare Images You can use the following steps to compare two images using OpenCV − Step 1: Import the required ...
Read MoreHow to Compute Image Moments in OpenCV Python?
Image moments are statistical measures that describe the shape and size characteristics of objects in an image. They are essential for computing features like center of mass, area, and orientation of objects. In OpenCV, image moments are calculated using contours of detected objects. Syntax The basic syntax for computing image moments is ? cv2.moments(contour) Where contour is a NumPy array containing the contour points of an object. Understanding Image Moments Image moments provide valuable information about objects ? m00 ? Area of the object m10, m01 ? First-order moments used ...
Read MoreHow to match image shapes in OpenCV Python?
We use cv2.matchShapes() function to match two image shapes in OpenCV. This function returns a metric showing the similarity between the image shapes using 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, cnt2, method, parameter) Where, cnt1 − The contour points of the first image ...
Read MoreHow to find Laplassian pyramids for an image using OpenCV in Python?
We can form Laplacian Pyramids from Gaussian Pyramids in OpenCV. While OpenCV doesn't provide a direct function to construct Laplacian Pyramids, we can create them by computing differences between Gaussian pyramid levels. In a Laplacian pyramid, images appear as edge-like representations and are commonly used in image compression and image enhancement applications. How Laplacian Pyramids Work 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. The process involves: Creating a Gaussian pyramid using cv2.pyrDown() Expanding higher levels using ...
Read MoreHow to find the image gradients using Sobel and Laplacian derivatives in OpenCV Python?
Image gradients are essential in computer vision for detecting edges and boundaries. OpenCV provides Sobel and Laplacian operators to compute these gradients. The Sobel operator uses first-order derivatives to find gradients in horizontal and vertical directions, while the Laplacian operator uses second-order derivatives. Syntax The following syntaxes are used to compute image gradients ? cv2.Sobel(img, ddepth, dx, dy, ksize) cv2.Laplacian(img, ddepth) Parameters img − The input grayscale image. ddepth − Output image depth. Use cv2.CV_64F for 64-bit floating-point precision. dx − Order of derivative in X-direction (horizontal). Set dx=1, dy=0 for horizontal ...
Read MoreHow to apply custom filters to images (2D convolution) using OpenCV Python?
In this tutorial, we will learn how to apply custom filters to images using OpenCV Python. We'll explore two different low-pass filters: filter2D and boxFilter. These filters perform 2D convolution operations to smooth images and remove noise. 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" that preserves edges. Syntax Following are the syntaxes ...
Read MoreHow to apply Perspective Transformations on an image using OpenCV Python?
In Perspective Transformation, straight lines remain straight even after the transformation. To apply a perspective transformation, we need a 3×3 perspective transformation matrix and four points on both the input and output images. Key Functions We use cv2.getPerspectiveTransform() to find the transformation matrix − M = cv2.getPerspectiveTransform(pts1, pts2) Where: pts1 − An array of four points on the input image pts2 − An array of corresponding four points on the output image To apply the transformation, we use cv2.warpPerspective() − dst = cv2.warpPerspective(img, M, (width, height)) Where: ...
Read MoreHow to apply Affine Transformation on an image in OpenCV Python?
Affine Transformation is a geometric transformation that preserves parallel lines in an image. This transformation requires three corresponding points between the input and output images to create a transformation matrix. Syntax To get the transformation matrix ? M = cv2.getAffineTransform(pts1, pts2) To apply the transformation ? cv2.warpAffine(img, M, (cols, rows)) Parameters pts1 − Array of three points on the input image pts2 − Array of corresponding three points on the output image img − Input image to be transformed M − 2×3 transformation matrix of type np.float64 (cols, ...
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 and plot histograms for different color channels (Blue, Green, and Red) of an input image. A histogram shows the distribution of pixel intensities in an image. For color images, we can create separate histograms for each color channel to analyze the color composition. Understanding cv2.calcHist() Parameters The cv2.calcHist() function takes the following parameters ? images − Source image as a list [img] channels − Channel index [0] for Blue, [1] for Green, [2] for Red ...
Read MoreHow to perform image translation using OpenCV in Python?
Image translation is the process of shifting an image to a new position within the coordinate system. OpenCV provides the cv2.warpAffine() function along with translation matrices to perform this transformation efficiently. Translation Matrix To translate an image by (tx, ty) pixels, where tx is horizontal shift and ty is vertical shift, we define a 2x3 translation matrix: import numpy as np # Translation matrix for shifting by (tx, ty) tx, ty = 100, 50 # 100px right, 50px down M = np.float32([[1, 0, tx], [0, 1, ty]]) print("Translation matrix:") print(M) Translation ...
Read More