Programming Articles

Page 175 of 2547

How to plot histograms of different colors of an image in OpenCV Python?

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

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

How to perform image translation using OpenCV in Python?

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

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

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 21K+ 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

What is the use of the WITH statement in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 7K+ Views

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

What are compound data types and data structures in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 5K+ Views

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

How do I access the serial (RS232) port in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 24K+ Views

To access the serial (RS232) port in Python, use the pyserial module, which provides Python Serial Port Extension for Win32, OSX, Linux, BSD, Jython, and IronPython. PySerial Features Access to the port settings through Python properties Support for different byte sizes, stop bits, parity and flow control with RTS/CTS and/or Xon/Xoff Working with or without receive timeout The files in this package are 100% pure Python The port is set up for binary transmission. No NULL byte stripping, CR-LF translation etc. Installation To install pyserial, use pip − pip install pyserial ...

Read More
Showing 1741–1750 of 25,469 articles
« Prev 1 173 174 175 176 177 2547 Next »
Advertisements