“This is the third day of my participation in the First Challenge 2022. For details: First Challenge 2022”

OpenCV accesses and manipulates pixels in different color Spaces

This section shows you how to access and read pixel values using OpenCV and how to modify them. In addition, you’ll learn how to access image properties. If you want to process multiple pixels at once, you need to create regions of Image (ROI). In Python, images are represented as NumPy arrays. Therefore, most of the operations included in the sample are NumPy related, and it is recommended that you have some knowledge of the NumPy package to better understand how the sample code works, but even if you don’t, it is ok to explain the functions used if necessary.

Color images access and manipulate pixels in OpenCV

Now let’s look at how to process BGR images in OpenCV. As mentioned above, when OpenCV loads a color image, the blue channel is the first, the green channel is the second, and the red channel is the third. First, the image is read using the cv2.imread() function. The image should be in the working directory, or the full path to the image should be provided. In this case, the sigonghuiye.jpeg image is read and stored in the IMG variable:

img = cv2.imread('sigonghuiye.jpeg')
Copy the code

After the image is loaded into IMG, you can obtain some properties of the image. The first property we’ll extract from the loaded image is Shape, which tells us the number of rows, columns, and channels (if the image is colored). We store this information in the Dimensions variable:

dimensions = img.shape
Copy the code

The second attribute is the size of the image (img.size= image height × image width × number of image channels) :

total_number_of_elements= img.size
Copy the code

The third attribute is the image data type, which can be obtained via img.dtype. Uint8 (unsigned char) : uint8 (uint8)

image_dtype = img.dtype
Copy the code

In the example above, we have used the cv2.imshow() function to display the image in the window. Here we go into more detail. When we use the cv2.imshow() function to display the image, the window automatically ADAPTS to the image size. The first argument to this function is the window name and the second argument is the image to display. In this case, since the loaded image is stored in the img variable, use this variable as the second argument:

cv2.imshow("original image", img)
Copy the code

With the image displayed, let’s take a look at the keyboard binding function — cv2.waitKey(), which waits the specified number of milliseconds for any keyboard event. The parameter is the time in milliseconds. When this function is executed, the program pauses and resumes when any key is pressed. If the number of milliseconds is 0 (cv2.waitkey (0)), it will wait indefinitely for the keystroke event:

cv2.waitKey(0)
Copy the code

To access (read) a pixel value, we need to supply the rows and columns of the required pixels to the img variable (which contains the loaded image), for example, to get the pixel value at (x=40,y=6)(x=40, y=6) :

(b, g, r) = img[6.40]
Copy the code

We store three pixel values in three variables (b, G, r). Keep in mind that OpenCV uses BGR format for color images. In addition, we can access only one channel at a time. In this case, we will index with the rows, columns, and indexes of the required channels. For example, to get only the blue value at pixels (x=40,y=6)(x=40, y=6) :

b = img[6.40.0]
Copy the code

Pixel values can be modified in the same way. For example, to set the pixel (x=40, y=6) to red:

img[6.40] = (0.0.255)
Copy the code

Sometimes, you need to deal with an area instead of a pixel. In this case, you should provide ranges of values (also known as slicing) rather than individual values. For example, to get the upper-left corner of the image:

top_left_corner = img[0:50.0:50]
Copy the code

The variable top_left_corner can be thought of as another image (smaller than img), but we can treat it the same way. Finally, if you want to close and release all Windows, use the cv2.DestroyallWindows () function:

cv2.destroyAllWindows()
Copy the code

Grayscale images access and manipulate pixels in OpenCV

Grayscale images have only one channel. Therefore, some differences are introduced in the processing of these images. We will focus on these differences here and not repeat the same ones. Again, we will use the cv2.imread() function to read the image. In this case, a second parameter is needed because we want to load the image in grayscale. The second argument is a flag bit that specifies how the image is read. The value required to load the image in grayscale is cv2.imread_grayscale:

gray_img = cv2.imread('logo.png', cv2.IMREAD_GRAYSCALE)
Copy the code

In this case, we store the image in the gray_img variable. If we print the size of the image (using gray_img.shape), we can only get two values, row and column. In the grayscale image, no channel information is provided:

dimensions = gray_img.shape
Copy the code

Shape will return the dimensions of the image as tuples — (828, 640). Pixel values can be accessed by row and column coordinates. In grayscale images, only one value is obtained (often called the intensity of the pixel). For example, if we want the pixel intensity at pixel (x=40,y=6)(x=40, y=6) :

i = gray_img[6.40]
Copy the code

The pixel value of the image can be modified in the same way. For example, if you want to change the value at pixel (x=40,y=6)(x=40, y=6)(x=40,y=6) to black (intensity = 0) :

gray_img[6.40] = 0
Copy the code

A link to the

OpenCV image processing basics

Coordinate system and image channel order in OpenCV