Opencv visual eye Python image processing III: Opencv image attributes, ROI region acquisition and channel processing

This column mainly introduces that if image processing is carried out through OpenCv python, the function processing prototype of OpenCv Python is understood through the principle. In specific cases, different levels and methods of processing are carried out for different images, so as to achieve a series of operations such as image denoising and sharpening. At the same time, I hope those who watch this column can understand the powerful image processing of OpenCv. If you reprint it, please indicate the source (original link and author's signature). Thank you!

Previous reference:
OpenCv visual eye Python image processing 1: introduction to OpenCv Python and python environment construction
Opencv visual eye Python image processing II: Opencv image reading, display, saving basic function prototype and use

In the last blog, we introduced the prototype and basic use methods of OpneCv image reading, display and saving functions, and introduced the image display functions commonly used by programmers. In this blog, Lin Jun will introduce the preliminary use of OpenCv for basic image processing, including image attribute acquisition, pixel modification, pixel transplantation, image channel understanding, etc., and deeply introduce the color display principle of image channel B\G\R, Understanding OpenCv through the principle is believed to be a necessary skill for everyone to do image processing. Please understand the principle as soon as possible. If there is something wrong, remember to remind senior Lin Jun to correct it!

Tip: what this article needs to understand is 3, Image channel Therefore, when you understand the third part, you'd better combine the senior's code, and then write the code yourself for understanding, which can deepen your understanding of the color formation of B\G\R channel!

1, Image properties

Since it is an image attribute, first of all, we have to understand what is an image attribute and what attributes does an image include? Only when we understand the above two problems can we operate the image. OpenCv's operation on the image can essentially be understood as the relevant operation on the image attributes to achieve the purpose we need. Next, let's take a look at the attributes included in the image!

1. Image properties

1) What are image attributes?
Image attributes: the characteristics of a picture. Different pictures have different characteristics, but generally speaking, there are only the following basic attributes: pixel, resolution, size, color, bit depth, hue, saturation, brightness, color channel and image level
2) Image attribute type
From the above, we can know that the types of image attributes are divided into 10 basic attributes. Next, introduce the attributes in turn!

  • pixel

The number of pixels of an image refers to the number of pixels contained in the width and height direction of a bitmap image. The display effect of an image on the display is determined by the number of pixels and the setting of the display.

  • Image resolution

Image resolution refers to the number of image pixels per unit print length, which indicates the number or density of image digital information. It determines the clarity of the image. In the area of the same size, the higher the resolution of the image, the more pixels make up the image, the smaller the pixels, and the higher the definition of the image. For example, a 1 with a resolution of 72 DPI × A 1-inch image contains 5184 pixels, while an image of the same size with a resolution of 300 dpi contains 90000 pixels. Because the high-resolution image contains more pixels per unit area, it can better represent the details and subtle color changes of the image than the low-resolution image.
For those images obtained with low resolution during scanning, the image quality cannot be improved by improving the resolution, because this method only expands the information of one pixel into the information of several pixels, and does not fundamentally increase the number of pixels. In addition, in ImageReady, the resolution of images cannot be changed, because ImageReady is specially used to process images of online media, not Photoshop
That's used to process printed images.
When setting the resolution of the image, the final release media of the produced image should be considered. If the produced image is used for online media, it only needs to match the resolution of the image with that of the typical display (72dpi or 96dpi); If the printed image is produced, the low resolution will make the printed image appear rough, while the high resolution will make the pixel of the image smaller than the pixel provided by the printing device, resulting in the increase of the file and the extension of the printing time. Moreover, for images with high resolution, the printing device may not work normally.

  • File Size

The size of the image file first determines the disk storage space required for the image file. It is generally measured in bytes, and its calculation formula is: number of bytes = (bitmap height) × Bitmap width × Image depth) / 8 it can be seen from the calculation formula that the size of the image file is directly related to the number of pixels. Although images with more pixels can better represent the details of the image when printing, they need more storage space, and the editing and printing time is relatively longer. For example, a 1 with a resolution of 200 DPI × A 1-inch image, which contains a number of pixels with a resolution of 100 DPI × An image of one inch is four times as large as the latter. Therefore, when setting the resolution of the image, we should comprehensively consider the quality and size of the image and find their best combination point. The maximum image file supported by Photoshop is 2GB and the maximum number of pixels is 30000 × 30000 pixels, which limits the print size and image resolution. For example, a picture of 100 × For a 100 inch image, the maximum resolution it can get is 300 dpi (300 pixels / 100 inches)

  • Image Color

Image color refers to the most color types in an image. Through image processing software, the proportion of three primary colors can be easily changed and mixed into any color.

  • Image Depth

Image depth, also known as the bit depth of an image, refers to the number of bits that describe the data of each pixel in the image. The data corresponding to each pixel of the image can usually be 1 bit or multiple bytes, which is used to store the color, brightness and other information of the pixel. The more data bits, the more color types of the corresponding image.

  • Tone

For example, adjust the brightness and darkness of the image in various modes of RGB, that is, adjust the brightness and darkness of the image in various modes of RGB. The hue range is 0-255, including 256 hues in total. For example, the of gray mode is to continuously divide white to black into 256 tones, that is, from white to gray, and then from gray to black. Similarly, in RGB mode, it represents the lightness and darkness of each primary color, that is, the lightness and darkness of three primary colors: red, green and blue. Adding red to dark color becomes dark red.

  • Saturation

Saturation refers to the depth of image color, which indicates the purity of color and depends on the reflection or projection characteristics of objects. Saturation is expressed by the number of grayscale that is proportional to the hue. The value range is usually 0% (lowest saturation) – 100% (highest saturation). Adjust the saturation of the image, that is, adjust the chromaticity of the image. When the saturation of an image is reduced to 0%, it will become a gray image. Increasing the saturation should increase its hue. For example, by adjusting the saturation of color TV, users can choose to watch black-and-white or color TV programs. For white, black and gray color images, they have no saturation.

  • Brightness

Brightness refers to the light and dark degree of image color, which is the feeling of human eyes on the light and dark intensity of the object, with a value of 0% - 100%

  • Color channel

The color channel of the image the three primary colors of the image can be mixed according to different proportions to produce many colors. The way or way to save each primary color information and adjust it is the color channel of the corresponding color. According to different application types, the types of primary colors are also different. For example, four printing plates are used in printing, and each printing plate is printed with Cyan, Magenta, Yellow and Black respectively. One channel is equivalent to one printing plate in printing, and each channel stores the data of one color. CMYK image has four color channels of Cyan, Magenta, Yellow and Black and one CMYK channel.

  • Image hierarchy

Image hierarchy in computer design system, in order to process image materials more conveniently and effectively, they are usually placed in different layers, and the image can be regarded as the superposition of several layers of images. Using image processing software, each layer can be processed separately, rather than affecting the image content of other layers. When creating an image file, the system will automatically create a background layer for it. This layer is equivalent to a canvas, which can be used for mapping, painting and other image processing. If an image has multiple layers, each layer has the same pixels, number of channels and format.

2. Get image properties

However, in the process of image processing, the image attributes we use are nothing more than image size (height and width) and the number of image channels. For color images, it is 3 channels, and for gray images, it is usually single channel. Next, let's see how to obtain image attributes through python code!
1) , obtain image size and number of channels

#Import image processing library opencv
import cv2
#Read image
img=cv2.imread("my.jpg")
#Get image size and number of channels
h,w,channel=img.shape
#Print acquired data
print(h,w,channel)


2) . only obtain image size

#Import image processing library opencv
import cv2
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read image
img=cv2.imread("my.jpg")
#Get image size and number of channels
h,w=img.shape[0:2]
#Print acquired data
print(h,w)

2, ROI region of interest

ROI region of interest is a basic processing method for image pixels. You can understand that the part you like in a picture is obtained by clipping pixels and then displayed. The specific method is as follows

1. ROI(region of interest)

1) Region of interest: in machine vision and image processing, the region to be processed is outlined in the form of box, circle, ellipse and irregular polygon from the processed image, which is called region of interest, ROI. In Halcon, OpenCV, Matlab and other machine vision software, various operators and functions are often used to obtain the ROI of the region of interest and carry out the next step of image processing.
2) What is the use of ROI?
ROI is a kind of IVE technology. IVE refers to Intelligent video encoding, that is, intelligent video coding. IVE technology can intelligently encode video according to customer requirements, optimize video coding performance without losing image quality, and finally reduce network bandwidth utilization and storage space.

In the monitoring picture, some monitoring areas do not need to be monitored or irrelevant, such as the sky, walls, grass and other monitoring objects. Ordinary network monitoring cameras encode (compress) and transmit the video of the whole area, which puts pressure on the network bandwidth and video storage. The ROI intelligent video coding technology solves this problem. The ROI camera allows users to select the areas of interest in the picture. After the ROI function is enabled, the important or moving areas will be subjected to high-quality lossless coding. For those areas that are not moved or selected, the bit rate and image quality will be reduced, the standard definition video compression will be carried out, and even the video of these areas will not be transmitted, Save network bandwidth and video storage space.

2. Get ROI area of interest

#Import image processing library opencv
import cv2
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read image
img=cv2.imread("my.jpg")
img= cv2.cvtColor(img,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
#Obtain an image of an area with an image height of 200:400 and a width of 200:500
img1=img[200:400,200:500]
#Image format conversion
#Display image
titles = ['Original drawing', 'Chen Yiyue's ROI region']  #title
images = [img, img1]   #Image contrast display
for i in range(2):
    plt.subplot(1,2,i+1), plt.imshow(images[i])  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


In the above way, directly intercept the position of the area corresponding to the pixel, and then display it to obtain the part you like in an image

3. ROI area transplantation

For the acquisition of ROI area, a common method for photos in life is to add watermark to the pictures, find the watermark pictures you like, and then add them to the pictures you need to set. This is ROI area transplantation. Learn about it through the following Python code!
1) . ROI region of interest transplantation of self image

#Import image processing library opencv
import cv2
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read image
img=cv2.imread("my.jpg")
img= cv2.cvtColor(img,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
#Obtain an image of an area with an image height of 200:400 and a width of 200:500
img1=img[200:400,300:500]
img[:200,:200]=img1 #The ROI area is transplanted to the upper left corner of the original image
#Display image
titles = ['ROI transplant', 'Chen Yiyue's ROI region']  #title
images = [img, img1]   #Image contrast display
for i in range(2):
    plt.subplot(1,2,i+1), plt.imshow(images[i])  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


2) Transplantation of different image regions of interest (add watermark)

#Import image processing library opencv
import cv2
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read the first image
img=cv2.imread("my.jpg")
img= cv2.cvtColor(img,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
#Read the second image
img1=cv2.imread("house.jpg")
img1= cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
#The image of the area with the height of the second image 200:400 and the width of 200:500 is obtained
img1=img1[200:400,300:500]
img[:200,:200]=img1 #Transplant the ROI area of the second image to the upper left corner of the first image
#Display image
titles = ['Image watermark adding', 'watermark']  #title
images = [img, img1]   #Image contrast display
for i in range(2):
    plt.subplot(1,2,i+1), plt.imshow(images[i])  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


3, Image channel

For the consideration of image channel, generally speaking, only color images, that is, 2-channel images, will be considered, and we will not consider the gray image of single channel; We can know from the query data that the color of the color image is composed of three primary colors B\G\R mixed in different proportions, and the depth of B\G\R color is represented by [0-255] in the image pixels. Next, we will get different channels respectively, and then understand the meaning of the three channels.
The channel acquisition in Python is realized by directly operating the pixel array, such as

  • Channel B: img[:,:][0]
  • Channel G: img[:,:][1]
  • R channel: img[:,:][2]

1. B(Blue), G(Green), R(Red) channels

1) Acquisition of, B, G and R channels

#Import image processing library opencv
import cv2
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read the first image
img=cv2.imread("my.jpg")
#3-channel acquisition
B=img[:,:,0]
G=img[:,:,1]
R=img[:,:,2]
#Print 3 channels
print('B',B)
print('G',G)
print('R',R)
#The purpose of converting to display format is that the image read by opencv is in BGR format, while the image displayed by matplotlib is in RBG format. Therefore, we need to convert the format through the following function, which will be discussed later!
B= cv2.cvtColor(B,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
G= cv2.cvtColor(G,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
R= cv2.cvtColor(R,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
#Display image
titles = ['B passageway', 'G passageway','R passageway']  #title
images = [B, G,R]   #Image contrast display
for i in range(3):
    plt.subplot(1,3,i+1), plt.imshow(images[i])  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


2) Of course, in addition to the method of operating the pixel array, we can split the channel through the split() function. The split channel code is as follows:

B, G, R = cv2.split(img)

The 3-channel image obtained by the above method is the same as the first method, so the specific method to choose is related to personal hobbies!

2. Channel merging

Above, we introduced how to split 3-channel images. Since there is splitting, there must be merging. The merging of channels in opencv is mainly through CV2 The merge ([b, G, R]) function is implemented, in which the parameters become BGR channels
1) . channel merging codes are as follows:

#Import image processing library opencv
import cv2
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read the first image
img=cv2.imread("my.jpg")
#3-channel acquisition
B=img[:,:,0]
G=img[:,:,1]
R=img[:,:,2]
#Channel merging
img1=cv2.merge([B, G, R])
img1=cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
plt.imshow(img1)  
plt.title('Channel merging')    
plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image

3. Three channel color display principle

Maybe many kids can't understand why BGR represents blue, green and red. Next, let's extract the different colors of the three channels of B\G\R in the image. Let's see why B\G\R represents the three primary colors, so that we can understand the principle
1) , B(Blue), G(Green) and R(Red)

#Import image processing library opencv
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read the first image
img=cv2.imread("my.jpg")
#Get image height and width
h,w=img.shape[0:2]

'''
Process the blue channel,
'''
#Get channel B - Blue
B=img[:,:,0]
#Set the other two channels to 0
G=np.zeros((h,w),dtype=img.dtype)
R=np.zeros((h,w),dtype=img.dtype)
#Merge channels, only keep the blue channel, and set other channels to 0
img1=cv2.merge([B, G, R])

'''
Dealing with the green channel,
'''
#Get channel G-green
B1=np.zeros((h,w),dtype=img.dtype)
#Set the other two channels to 0
G1=img[:,:,1]
R1=np.zeros((h,w),dtype=img.dtype)
#Merge channels, only keep the green channel, and set other channels to 0
img2=cv2.merge([B1, G1, R1])

'''
Processing the red channel,
'''
#Get channel R - red
B2=np.zeros((h,w),dtype=img.dtype)
#Set the other two channels to 0
G2=np.zeros((h,w),dtype=img.dtype)
R2=img[:,:,2]
#Merge channels, only keep the red channel, and set other channels to 0
img3=cv2.merge([B2, G2, R2])

img1= cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
img2= cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
img3= cv2.cvtColor(img3,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format

#Display image
titles = ['B passageway', 'G passageway','R passageway']  #title
images = [img1, img2,img3]   #Image contrast display
for i in range(3):
    plt.subplot(1,3,i+1), plt.imshow(images[i])  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


2) . when we extract the pixels of each channel, and then set the other two channel pixels to 0 respectively, and only the pixels that need to display the channel are reserved separately, then we can see the color representation of the three primary colors. By mixing the color proportion of the above three primary colors, we can get other colors. The color mixing proportion is the pixel proportion of the combination of the three primary colors;

3) Here, we need to understand that for a pixel with three channels (for example: [127234158]), 127 represents the depth of blue. The closer it is to 255, the darker the color, the bluer it gives us. The closer it is to 0, the lighter the color, and the lighter the blue. However, it is worth noting that 0-255 here represents the color level, and 127 belongs to the 128th level of the inner color level (starting from 0), that is, It's still blue. It just gives us different visual feelings, showing different shades of blue; Similarly, 234 indicates the green level and 158 indicates the red level.
4) However, let's combine the above three colors together, that is [127234158], and the visual color given to us may be other colors, which is the mixing proportion of B\G\R pixels, which forms other colors after mixing; For example, when we know that red and blue are matched with purple, the pixels in the code can be expressed as having two primary colors: red and blue, and green is set to 0, such as: [127,0158], which represents the purple category, because the purple color also has depth, that is, the depth of purple can be adjusted by adjusting the pixel values of blue channel B and red channel R, as shown in the following code:

#Import image processing library opencv
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #Display Chinese
#Read the first image
img=cv2.imread("my.jpg")
#Get image height and width
h,w=img.shape[0:2]
#Custom blank image, the same size as the original image, is used to save the purple after red and blue matching
img1=np.zeros((h,w,3),dtype=img.dtype)
for i in range(h):
    for j in range(w):
        img1[i,j][0]=img[i,j][0]  #B blue channel reserved
        img1[i,j][1]= 0           #Green channel, set to 0
        img1[i,j][2]=img[i,j][2]  #Red channel reservation
img= cv2.cvtColor(img,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
img1= cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)#Convert BGR to RGB display format
#Display image
titles = ['Original drawing', 'Red and blue with purple']  #title
images = [img, img1]   #Image contrast display
for i in range(2):
    plt.subplot(1,2,i+1), plt.imshow(images[i])  
    plt.title(titles[i])    
    plt.axis('off')#If the off coordinate axis is set to on, it means that the coordinate axis is on
plt.show()#Display image


5) So what should I do to set purple as the darkest color? And all are purple, then we can set red channel R and blue channel B to 255, so the purple obtained is the deepest effect. The core code is as follows:

for i in range(h):
    for j in range(w):
        img1[i,j][0]=255          #B blue channel reserved
        img1[i,j][1]= 0           #Green channel, set to 0
        img1[i,j][2]=255          #Red channel reservation


I believe that by now, many children will understand the meaning of B\G\R. in fact, it is still very simple. Understanding the principle of color display in opencv, that is, the principle of image color display, is of great significance to the later image processing. At the same time, it is also the entry-level basis of OpenCV image processing. Children must understand it!

The above is the whole content of this blog. Remember to leave a message and comment if you encounter problems in installation. The senior will answer for you when you see it. This senior is not too cold!

True warriors dare to face the bleak life and face the dripping blood. What kind of sad and happy person is this? However, fortune often designs for mediocre people to wash the old traces with the flow of time, leaving only light red blood and slight sadness. In this pale red blood and slight sadness, it gives people a temporary life to maintain this human like and inhuman world. I don't know when this world will come to an end----- Lu Xun

Another programming year in January^

Tags: Python

Posted by seikan on Tue, 24 May 2022 21:58:39 +0300