OpenCV Articles

Page 7 of 11

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 14K+ 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. 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

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 1K+ Views

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

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 25K+ Views

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

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 22K+ Views

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

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

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 5K+ Views

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

Downsampling an image using OpenCV

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 8K+ Views

In this program, we will down sample an image. Downsampling is decreasing the spatial resolution while keeping the 2D representation of an image. It is typically used for zooming out of an image. We will use the pyrDown() function in the OpenCV library to complete this task. What is Image Downsampling? Image downsampling reduces the number of pixels in an image by decreasing its spatial resolution. OpenCV's pyrDown() function performs Gaussian pyramid downsampling, which smooths the image and reduces its size by half in each dimension. Algorithm Step 1: Read the image using cv2.imread() Step ...

Read More

Performing white TopHat operation on images using OpenCV

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 786 Views

In this tutorial, we will perform the TopHat operation on images using OpenCV. TopHat operation is a morphological transformation that extracts small elements and details from images by highlighting bright objects on dark backgrounds. We will use the cv2.morphologyEx() function with the cv2.MORPH_TOPHAT operation. What is TopHat Operation? TopHat (also called White TopHat) is defined as the difference between the input image and its opening. It highlights small bright details that are smaller than the structuring element ? TopHat = Original Image - Opening Original ...

Read More

Performing an opening operation on an image using OpenCV

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 774 Views

In this program, we will perform the opening operation on an image using OpenCV. Opening removes small objects from the foreground of an image, placing them in the background. This technique can also be used to find specific shapes in an image. Opening is mathematically defined as erosion followed by dilation. The function we use for this task is cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel). Original Image Algorithm Step 1: Import cv2 and numpy Step 2: Read the image Step 3: Define the kernel (structuring element) Step 4: Pass the image and kernel to cv2.morphologyEx() function ...

Read More

Dilating images using the OpenCV function dilate()

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 889 Views

In this tutorial, we will learn how to dilate an image using the dilate() function in OpenCV. Dilation is a morphological operation that adds pixels to the boundaries of objects in an image, effectively expanding or thickening white regions and shrinking black regions. What is Image Dilation? Dilation expands the foreground objects in a binary or grayscale image. It uses a kernel (structuring element) that slides over the image, and for each position, it replaces the center pixel with the maximum value in the kernel's neighborhood. Syntax cv2.dilate(src, kernel, iterations=1, borderType=cv2.BORDER_CONSTANT, borderValue=0) Parameters ...

Read More

Eroding an image using the OpenCV function erode()

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 674 Views

In this program, we will erode an image using the OpenCV function erode(). Erosion of an image means to shrink the image by reducing the size of white regions or foreground objects. If any of the pixels in a kernel is 0, then all the pixels in the kernel are set to 0. One condition before applying an erosion function on an image is that the image should be a grayscale image. What is Image Erosion? Image erosion is a morphological operation that reduces the boundaries of foreground objects (white pixels). It works by sliding a structuring element ...

Read More
Showing 61–70 of 107 articles
« Prev 1 5 6 7 8 9 11 Next »
Advertisements