OpenCV study notes 02--image pixel processing--binary image, grayscale image, color image pixel processing, corresponding functions in numpy.array

 content

(1) Grayscale image pixel processing

(2) Color image pixel processing

(3) Application of numpy.array library in image processing

(4) View the attribute information of the image


        Then note 01 continues to summarize, when we read an image, it is generally a binary image, a grayscale image, and a color image. A binary image is that the pixels that make up the image are either black or white. There are only two values. In the double type, 0 represents black and 1 represents white. For uint8, 0 represents black and 255 represents white. If the pixels of the image are not only composed of two colors, but also gray between white and black, that is, divide the space between white and black into several segments, and each segment represents the gray. The degrees are different, and the image displayed at this time is a grayscale image. Color images have different impact on color. For example, rgb color images are composed of three channels, and each color pixel value is a color presented by superimposing the corresponding pixel values ​​on the three channels.

(1) Grayscale image pixel processing

Here, the lena grayscale image used is released first, so that everyone can practice. As shown below.

Ideas:

The first step: read the file first, and then display its value matrix composed of pixels to display the original image.

Step 2: Access a certain pixel value or certain pixel values ​​in the original image matrix, and then modify these pixel values

Step 3: Display the modified image and the modified pixel values.

code show as below:

import cv2
# 读取灰度图像,然后对像素值和图像显示,然后修改之后显示修改之后的图像和像素
filename = r'C:\Users\LBS\Desktop\lenagray.png'
# 读取图像
f = cv2.imread(filename)
# 显示图像,one是显示图像的窗口的名字
cv2.imshow('one', f)
# 打印图像对应的像素值的矩阵
print(f)
# 利用for循环对原图像的第10行到第99行,第80列到第99列的像素值改为255,
for i in range(10,100):
    for j in range(80,100):
        f[i, j] = 255
# two代表修改后的图像
cv2.imshow('two', f)
print('f(10:100, 80:100)={0}'.format(f[10:100, 80:100]))
# 按下任意键关闭图像窗口
cv2.waitKey()
cv2.destroyAllWindows()

result:

The picture on the left is the original image, and the picture on the right is after our modification. It can be seen that a rectangular white box appears because we have assigned a pixel value of 255.

The output pixel result is:

The modified part has become 255. 

(2), color image pixel processing

        When the rgb color image is read into opencv for processing, the first layer, the second layer, and the third layer correspond to the b channel, the g channel, and the r channel, respectively, and the corresponding order of the channels in matlab processing is just the opposite. But it does not affect our operations on each channel. Add a picture as f, then f[:,:,0], f[:,:,1], f[:,:,2] correspond to the pixel values ​​of the b channel, g channel, and r channel of the image , you can also use cv2.split(f)[0], cv2.split(f)[1], cv2.split(f)[2] to obtain three channels. Each channel can be regarded as a matrix of pixel values ​​corresponding to a grayscale image.

In the image f[x, y, z], x represents the row of the image, y represents the column of the image, and z represents a channel. If it is f[x, y], it is the corresponding three channels, and the printed output x row y column has three values

Ideas:

The first step: first obtain a three-dimensional array of color images. If necessary, then obtain a two-dimensional array of pixel values ​​for each channel

Step 2: Then modify the pixels of the 3D array by accessing its pixel values.

Step 3: Display the image whose pixel value has been modified, and print out the value information of the modified pixel.

Corresponding code:

import numpy as np
import cv2
# 读取彩色图像
filename = r'C:\Users\LBS\Desktop\lena.jpg'
f = cv2.imread(filename)
# 显示彩色图像
cv2.imshow('01', f)
print("修改像素之前,个别像素对应的像素值\n")
# 图像f[x,y,z]中x代表图像的行,y代表图像的列,z代表的某一通道,若为f[x,y],则是对应的三个通道,打印输出的x行y列有三个值
print("f[0, 0]=\n", f[0, 0])
print("f[50,0]=\n", f[50, 0])
print("f[100,0]\n", f[100, 0])
# 对彩色图像的像素进行处理,这里的三重for循环是对三个通道的值都进行修改
for i in range(0, 50):
    for j in range(0, 100):
        for k in range(0, 3):
            f[i, j, k] = 255
for i in range(50, 100):
    for j in range(0, 100):
        for k in range(0, 3):
            f[i, j, k] = 128
# 如果没有指定对0、1、2、中的哪个通道对应的像素值进行修改时,则同时修改的是三个通道对应的像素值,三个通道都被修改为相同的值。
# 这里三个通道对应的值都为0,即黑色,可以通过图像进行观察
for i in range(100, 150):
    for j in range(0, 100):
        f[i, j] = 0
# 显示修改之后的图像
cv2.imshow('02', f)
print("修改像素之后,个别像素对应的像素值\n")
print("f[0, 0]=\n", f[0, 0])
print("f[50,0]=\n", f[50, 0])
print("f[100,0]\n", f[100, 0])
cv2.waitKey()
cv2.destroyAllWindows()

The corresponding result image is shown in the following figure:

01 is the original image, 02 is the modified image. 

The result of the printout before and after changing the pixel value is shown in the following figure.

 (3) Application of numpy.array library in image processing

       There are two functions in this library, item(), itemset(), which can be used to access pixels and modify pixel values. If these two functions are not used, we can also operate, just like the previous (1), (2) We all operate directly through the image object.

For grayscale images:

item (row, column): item is our image object, like the previous f, we can determine the value of a pixel by row and column.

itemset ((row, column), the value to be modified): This is a little more convenient than our previous operation. Through this statement, the pixels can be modified directly.

For color images:

item (row, column, channel): item is our image object, like the previous f, by row and column, and channel we can determine the value of a pixel in a certain channel.

itemset ((row, column, channel), value to be modified): that is, first determine a pixel by (row, column, channel), and then change it with the value to be modified. If the value of the channel is omitted, the same value assignment operation is performed on the values ​​of the three channels.

(4) View the attribute information of the image

If f represents an image, you can pass

f.shape: Get the number of rows, columns, and channels of the image (for color images).

f.size: Get the number of pixels in the image, that is, how many pixels the entire image has.

f.dtype: You can view the data type of the image f.

Summary: The above is the basic operation of pixels, please practice.

Please indicate the source. It's not easy to write, give it a like.

OpenCV study notes 03--image operation--image addition operation, image bit operation, image mask

Guess you like

Origin blog.csdn.net/BaoITcore/article/details/123918053