Found 10784 Articles for Python

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

Shahid Akhtar Khan
Updated on 27-Sep-2022 12:32:46

725 Views

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

3K+ Views

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

12K+ Views

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

1K+ Views

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

1K+ Views

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

20K+ Views

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

How to create a black image and a white image using OpenCV Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 11:54:58

18K+ Views

To create a black image, we could use the np.zeros() method. It creates a numpy n-dimensional array of given size with all elements as 0. As all elements are zero, when we display it using cv2.imshow() or plt.imshow() functions, it displays a balck image. To create a white image, we could use the np.ones() method. It creates a numpy n-dimensional array of given size with all elements as 1. We multiply this array by 255 to create a white image. Now all elements are 255, so when we display it using cv2.imshow() or plt.imshow() functions it gives a white image. ... Read More

How to join two images horizontally and vertically using OpenCV Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 11:52:01

4K+ Views

Images in OpenCV are represented as numpy.ndarray. OpenCV provides two functions − cv2.hconcat() and cv2.vconcat() to join images. The function cv2.hconcat() joins images horizontally and the function cv2.vconcat() joins images vertically. These functions join two or more images. These functions accept a list of images of the same size to join them. The height, width and number of channels of all images must be the same to join them Syntax cv2.hconcat(img_list) cv2.vconcat(img_list) Where img_list is a list of images [img1, img2, …]. To join the images horizontally or vertically, one could follow the steps given below − ... Read More

How to resize an image in OpenCV using Python?

Shahid Akhtar Khan
Updated on 27-Sep-2022 10:33:16

11K+ Views

OpenCV provides the function cv2.resize() to resize an image. Resizing in OpenCV is referred to as scaling. We can resize an image by specifying the image size or scaling factor. The aspect ratio is preserved when we specify the scaling factor. There are different interpolation methods used in cv2.resize() function − cv2.INTER_AREA − Used for shrinking an image. cv2.INTER_CUBIC − It’s slow, used for zooming. cv2.INTER_LINEAR − Used for zooming. It is default for all resizing purposes. Steps You can use the following steps to resize an image − Import the required libraries. In all the following Python ... Read More

What is the use of the WITH statement in Python?

Vikram Chiluka
Updated on 22-Sep-2022 12:50:09

5K+ Views

In this article, we will learn about the “with” statement in python and its uses. The with statement in Python replaces a try-catch block with a simple shorthand. More significantly, it ensures that resources are closed immediately after processing. Reading or writing to a file is a common use of the with statement. A context manager is a function or class that supports the with statement. A context manager enables you to open and close resources right when you want to. The open() function, for example, is a context manager. When you use the with statement to call the ... Read More

Advertisements