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 More
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 More
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
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 More
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 More
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 More
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 More
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
The with statement in Python provides an elegant way to handle resources like files, database connections, and network sockets. It automatically manages resource cleanup and replaces complex try-catch blocks with cleaner, more readable code. What is the WITH Statement? The with statement works with context managers to ensure resources are properly opened and closed. Key benefits include ? Automatic resource cleanup Exception safety Cleaner, more readable code No need for explicit close() calls Reading Files with WITH Statement The most common use case is file handling. Here's how to read a file using ... Read More
In this article, we will explain what are the compound datatypes and data structures in Python. Variables have so far only stored one value. What if we wish to save many related values? We could simply create distinct variables for each. But what if we don't know how many values will be present? What if we wish to use these values within a loop? Compound data structures are data types that can store a large number of values. In Python, there are various types of compound data structures. We will mostly concentrate on ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance