How to perform image rotation in OpenCV using Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:57:33
To perform image rotation by an angle, we first need to get the rotation matrix. To find the rotation matrix, we apply cv2.getRotationMatrix2D() function. The syntax for this function is − M = cv2.getRotationMatrix2D(cr, degree, scale) Where cr is the center of rotation, degree is the angle by which image is rotated and scale is scale factor to scale up or scale down the image. The rotation matrix M is a 2×2 matrix (numpy array). We pass the rotation matrix M to cv2.warpAffine() function as an argument. See the syntax below − Syntax cv2.warpAffine(img, M, (width, height)) Here, ... Read More

How to perform image translation using OpenCV in Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:51:32
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

How to perform Otsu's thresholding on an image using Python OpenCV?

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:45:23
Otsu’s thresholding is a kind of thresholding technique. There are other types of thresholding techniques such as simple thresholding and adaptive thresholding. The simple thresholding technique uses a global threshold value while the adaptive thresholding technique uses different threshold values for different regions. Otsu’s thresholding technique uses a global threshold value but it is not chosen. It is determined automatically. It works accurately for bimodal images. The bimodal images are those images whose histogram has two peaks. The threshold value is the approximate value of the middle of these two peaks. If the image is not bimodal this thresholding is ... Read More

How to perform adaptive mean and gaussian thresholding of an image using Python OpenCV?

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:38:50
Adaptive thresholding is a kind of thresholding technique. There are other types of thresholding techniques such as simple thresholding that uses a global threshold value. But using a global threshold value is not a good idea for an image having different lighting conditions in different areas. Adaptive thresholding calculates the threshold value for a small region in the image. So we have different threshold values for different regions in the image which gives better results in comparison to the simple thresholding technique. There are three special parameters: adaptive_method, block_size and const. See the syntax given below. Syntax cv2.adaptiveThreshold(img, max_val, adaptive_method, ... Read More

How to perform different simple thresholding of an image using Python OpenCV?

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:32:46
In simple thresholding, we define a threshold value and if a pixel value is greater than a threshold value, it is assigned a value (say 255), else it is assigned another value (say 0). A simple thresholding can be applied using the function cv2.threshold(). It accepts four arguments− the source image, threshold value, the maxVal and the thresholding type. OpenCV provides the following different types of thresholding − cv2.THRESH_BINARY − In this thresholding, pixel value more than the threshold value is assigned to 255 else assigned to 0. cv2.THRESH_BINARY_INV − It is the opposite case of cv2.THRESH_BINARY. cv2.THRESH_TRUNC − ... Read More

How to access image properties in OpenCV using Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:26:07
An image in OpenCV is a NumPy array. We can access the image properties using the attributes of the numpy array. We access the following image properties for the input image img − Image Type − data structure of the mage. Image in OpenCV is numpy.ndarray. We can access it as type(img). Image Shape − It is the shape in [H, W, C] format, where H, W, and C are the height, width and number of channels of the image respectively. We can access it as img.shape. Image Size − It is the total number of pixels in an ... Read More

How to find the HSV values of a color using OpenCV Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:22:11
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. To find the HSV values of a color, follow the steps given below − Steps Import the required libraries. In all the following Python examples, the required Python ... Read More

How to create a trackbar as the HSV color palette using OpenCV Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:12:57
To create trackbars as the HSV (Hue, Saturation and Value) color palette in OpenCV, we apply two different functions. These functions are cv2.reateTrackbar() and cv2.getTrackbarPos() The cv2.reateTrackbar() function is used to create a trackbar, while cv2.getTrackbarPos() function is used to access the value of the selected trackbar position. Using these two functions, we create a window that contains the trackbars for H, S, V colors and a color window to display the selected color. By changing the position of trackbars, we can select a particular value of color. The range for H is between 0 and 179, whereas for ... Read More

How to create a trackbar as the RGB color palette using OpenCV Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:05:22
In OpenCV, a trackbar can be created using cv2.reateTrackbar() 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. See the below syntaxes for both functions. 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 ... Read More

How to convert an RGB image to HSV image using OpenCV Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 11:57:39
An RGB (colored) image has three channels, Red, Blue and Green. 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, the Hue, Saturation and Value channels. 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
Advertisements