Image representation and channel number issues, reading and displaying pictures, cv2.imread(filename, flags=None)

Image representation and number of channels

Basic concepts of digital images

For a digital image, what we see is a real picture visible to the naked eye, but to the computer, this image is just a bunch of dots with different brightness. An image with a size of M × N can be represented by a matrix of M × N. The value of the matrix element represents the brightness of the pixel at this position. Generally speaking, the larger the pixel value, the brighter the point.

In general, grayscale images are represented by 2-dimensional matrices, and color (multi-channel) images are represented by 3-dimensional matrices (M × N × 3).

————————————————————————————————————————

number of channels

Single channel
Commonly known as grayscale image, each pixel can only have one value to represent the color, its pixel value is between 0 and 255, 0 is black, 255 is white. The grayscale image is a single-channel image obtained by calculating the three components of RGB according to a certain ratio. There are some different levels of gray in the middle. It can be said that grayscale is a transitional color between black and white
. To describe it, it is a single channel. Both grayscale and binarized images are single-channel images.
2 8 2^{8}28 = 2 2 2^{2} 22(B) * 2 3 2^{3} 23(G) * 2 3 2^{3} 23 (R)
A total of 256 colors are displayed.
Value range: 0~255

  • The bit depth of the grayscale image is 1*8=8.
  • The bit depth of the binarized image is 1*1=1.

Three channels
are RGB three colors, each pixel has 3 bytes (1 byte is equal to 8 bits) to represent (RGB), and the maximum value range is 0~255. 2 24 2^{
24 }224 = 2 8 2^{8} 28(B) * 2 8 2^{8} 28(G) * 2 8 2^{8} 28 (R)
So the value range of each element of RGB is: 0~255.
Then the combination of the three colors is 256 * 256 * 256 = 16777216, and a total of 16777216 colors are displayed.
Value range: 0~16777215
The three-channel is the most used picture. A three-channel map means that each pixel has three values ​​​​represented. Bit depth=3*8=24.

Four-channel
and four-channel image, that is, R, G, B plus an A channel, indicating transparency. Generally called the alpha channel, which means transparency.
2 32 2^{32}232 = Alpha transparency +2 8 2^{8}28(B) * 2 8 2^{8} 28(G) * 2 8 2^{8} 28 (R)
Four-channel image means that each pixel has 4 value representations, bit depth=4*8=32.
PNG is an image format that uses RGBA.

Binarized image
The meaning of a binary image is that each pixel in each image can only take 0 or 255, where 0 is black and 255 is white, that is, either black or white. We convert our color image to grayscale and output it.
The bit depth is 1, which means that each pixel is represented by 1 bit, that is, either 1 or 0.

Conversion between channel numbers

# 高通道数向低通道数转化
img_gray=img.convert('L')  # 将四通道或三通道图片转化为灰度图
img_white=img.convert('1') # 将四通道,三通道,灰度图转化为二值化图片
#低通道数向高通道数转化,主要涉及到灰度图向RGB的转化。

# 灰度图向三通道图的转化注意这里输出的图片依然是灰色的,但是位深度已经达到24.
img_color=img_gray.convert('RGB') 

————————————————————————————————————————

Then let’s talk about how the image is stored, even how it is encoded:
if it is a single-channel image, that is, a grayscale image, each pixel value can use an eight-bit binary, as shown below:
insert image description here

Among them, I(ij) represents the brightness value of row i and column j.

If it is a multi-channel image, such as an RGB image, each pixel is represented by three bytes. In OpenCV, the channel order of RGB images is BGR, and the storage is as shown in the following figure:
insert image description here

Original link: https://blog.csdn.net/mao_hui_fei/article/details/78217049

————————————————————————————————————————

read and display images

cv2.imread parameter introduction
cv2.imread(filename, flags=None)

filename: Image address such as: './xxx.png'

flags: Flag bits, indicating the format of the read data

  • -1: Original image. Preserve the original color channel of the read image. Equivalent to IMREAD_UNCHANGED
  • 0: grayscale image. Equivalent to IMREAD_GRAYSCALE
  • 1: Color map. The transparency of the image is ignored, this is the default parameter. Equivalent to IMREAD_COLOR

code show as below:

# 引入包
import cv2 as cv

img = cv.imread("./static/image/blur.jpg")
#Mat imread(const String& filename,int flags);  读取图片
# 第一个参数是图片地址:“\”"\\"和"/" "//"   无论正反,单双python2.7实测没影响
# 第二个参数是图片读取方式:默认正常读取,如果为0 则为 灰度图

cv.namedWindow("Image", 0)
# cv.NamedWindow( const char* name, int flags );
# 创建窗口,其实不写这行代码也可以show出来。
#  第一个参数是窗口名字,尽量使用英文命名,中文会出现乱码
# 第二个参数是窗口显示方式, 为0或cv.WINDOW_NORMAL:可以改变窗口大小,不写或cv.WINDOW_AUTOSIZE则不可改变大小

cv.imshow('Image', img)
# mshow(const string& winname, InputArray mat) 显示图片窗口
# 第一个参数:窗口名称。如果上面有NamedWindow()函数,这个名称要与它一样,不然会出现两个窗口,一个是NamedWindow的空白窗口,一个是imshow的图片窗口。
# 第二个参数:要显示的图片。
# 如果窗口是用CV_WINDOW_AUTOSIZE(默认值)标志创建的,那么显示图像原始大小。否则,将图像进行缩放以适合窗口。而imshow 函数缩放图像,取决于图像的深度

cv.waitKey(5000)
# waitKey(K) 窗口显示时间,单位:毫秒
# k=0: (也可以是小于0的数值)一直显示,键盘上按下一个数字键即会消失
# k>0:显示多少毫秒

cv.destroyAllWindows() # 删除建立的全部窗口,释放资源

The result display:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_33538887/article/details/126478280