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
Articles by Shahid Akhtar Khan
Page 6 of 17
How 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 MoreHow to find the HSV values of a color using OpenCV Python?
To find the HSV values of a color, we can use color space conversion from BGR to HSV. First we define the color value in BGR format as numpy.ndarray and then convert it to HSV space. We can also find the lower and upper limits of HSV value as [H-10, 100, 100] and [H+10, 255, 255] respectively. These lower and upper limits can be used to track an object of particular color. Steps Import the required libraries. In all the following Python examples, the required Python libraries are OpenCV and NumPy. Make sure you have already installed ...
Read MoreHow to create a trackbar as the RGB color palette using OpenCV Python?
In OpenCV, a trackbar can be created using cv2.createTrackbar() function. To access the value of the selected trackbar position, we use cv2.getTrackbarPos() function. Using these two functions, we create a window that contains the trackbars for R, G, B colors and a color window to display the selected color. By changing the position of trackbars RGB colors change between 0 and 255. Syntax cv2.createTrackbar(trackbar_name, window_name, default_value, max_value, callback_func) cv2.getTrackbarPos(trackbar_name, window_name) Parameters trackbar_name − It's the trackbar name. This name is used to access the trackbar position value. window_name − It is the ...
Read MoreHow to convert an RGB image to HSV image using OpenCV Python?
An RGB (colored) image has three channels: Red, Green, and Blue. A colored image in OpenCV has a shape in [H, W, C] format, where H, W, and C are image height, width and number of channels. All three channels have a value range between 0 and 255. The HSV image also has three channels: Hue, Saturation and Value. In OpenCV, the values of the Hue channel range from 0 to 179, whereas the Saturation and Value channels range from 0 to 255. In OpenCV, to convert an RGB image to HSV image, we use the cv2.cvtColor() function. ...
Read MoreHow to create a black image and a white image using OpenCV Python?
To create a black image, we use the np.zeros() method which creates a numpy array with all elements as 0. When displayed using cv2.imshow(), it appears as a black image since 0 represents black pixels. To create a white image, we use np.ones() method and multiply by 255 to get maximum pixel intensity. This creates a white image since 255 represents white pixels in 8-bit images. Note − We pass dtype = np.uint8 to create 8-bit unsigned integer arrays suitable for image data. Creating a Black Image Black images are created using np.zeros() which initializes all ...
Read MoreHow to join two images horizontally and vertically using OpenCV Python?
Images in OpenCV are represented as numpy.ndarray. OpenCV provides two functions − cv2.hconcat() and cv2.vconcat() to join images horizontally and vertically respectively. These functions have the following requirements ? They can join two or more images All images must have the same dimensions (height and width) All images must have the same number of channels Syntax cv2.hconcat(img_list) cv2.vconcat(img_list) Where img_list is a list of images [img1, img2, …]. Steps to Join Images Step 1: Import the required libraries ? ...
Read More