Article directory
- Code address of this column
https://github.com/xiawei20161308104/xv_opencv_tutorials
- Code path for this section
xv_opencv_tutorials/ImageProcessinginOpenCV/load_img.py
0. Knowledge points in this section
- create window
namedWindow
- Load images locally
imread
- display image in window
imshow
- write image to file
imwrite
- close the window
destroyWindow destroyAllWindows
1. Create a window
opencv provides cv.namedWindow
functions to create a window function
import cv2 as cv
# 参数一winname:string类型的窗口名称,参数二flags:窗口类型,使用规定好的类型,默认WINDOW_AUTOSIZE
cv.namedWindow('img', cv.WINDOW_AUTOSIZE)
cv.waitKey(0)
The optional parameters of the flags window type are
generally used .cv.WINDOW_NORMAL
function name | application |
---|---|
cv.WINDOW_NORMAL | You can adjust the window size with the mouse, and switch the full-screen window to the normal size |
cv.WINDOW_AUTOSIZE | Cannot resize, image is displayed at its original size, but also affected by screen resolution |
cv.WINDOW_FULLSCREEN | full-screen display |
cv.WINDOW_FREERATIO | When resizing the image, the original ratio is not considered |
cv.WINDOW_KEEPRATIO | When resizing the image, keep the original ratio unchanged |
cv.WINDOW_OPENGL | support opengl |
Notice
- If the filling is
winname
repeated, the function will not be executed, for example:
the img image at this time is the first created img, and the third one will not be executed.
WINDOW_AUTOSIZE
Misfits may occur. The left image img only shows the upper half, which is incomplete, and the right image is the original image.
2. Load images locally
opencv provides cv.imread
functions to realize the function of reading images. The types that can be read are:
import cv2 as cv
cv.namedWindow('img WINDOW_NORMAL', cv.WINDOW_NORMAL)
# 读取图像
# 参数一filename:string类型的图片路径,参数二flags:读取方式,比如读取哪个通道,哪些保留哪些丢弃等。使用规定好的类型。
img = cv.imread("../imgs/opencv.png")
cv.imshow('img WINDOW_NORMAL', img)
cv.waitKey(0)
Optional parameter for how flags are read
补充:
jpg
: The format is lossy compression, 24 bit true color, does not support animation,不支持透明色
. Image quality suffers during compression. After a picture is uploaded and downloaded multiple times, the picture will gradually become distorted.
PNG
: The format is lossless data compression. The PNG format has three forms: 8-bit, 24-bit, and 32-bit. Among them, 8-bit PNG supports two different forms of transparency (index transparency and alpha transparency), 24-bit PNG does not support transparency, and 32-bit PNG adds an 8-bit transparent channel (32-24=8) on the basis of 24 bits可展现256级透明程度
.
imread
The function does not read transparency by default, so if there are some images with special formats, they need to be flagged.
function name | application |
---|---|
cv.IMREAD_UNCHANGED | Reading the original image does not change, it can be read to the transparent channel of png |
cv.IMREAD_GRAYSCALE | Internally convert to grayscale with encoder |
cv.IMREAD_COLOR | Default configuration, converted to 3-channel BGR image |
cv.IMREAD_REDUCED_GRAYSCALE_2/ | Convert to grayscale image, size reduced by 1/2 |
cv.IMREAD_REDUCED_GRAYSCALE_4/ | Convert to grayscale image, size reduced by 1/4 |
cv.IMREAD_REDUCED_GRAYSCALE_8 | Convert to grayscale image, size reduced by 1/8 |
cv.IMREAD_REDUCED_COLOR_2 | Convert to BGR color image, the size is reduced by 1/2 (can also be reduced by 1/4, 1/8) |
demo
import cv2 as cv
cv.namedWindow('img IMREAD_GRAYSCALE', cv.WINDOW_NORMAL)
cv.namedWindow('img1 original', cv.WINDOW_NORMAL)
cv.namedWindow('img2 original had alpha', cv.WINDOW_NORMAL)
# 读取图像
# 参数一filename:string类型的图片路径,参数二flags:读取方式,比如读取哪个通道,哪些保留哪些丢弃等。使用规定好的类型。
# 以灰度图形式读取
img = cv.imread("../imgs/opencv.png", cv.IMREAD_GRAYSCALE)
print("灰度图大小为:", img.shape)
# 以默认形式读取原图,没有透明度通道
img1 = cv.imread("../imgs/opencv.png")
print("默认方式读取大小为:", img1.shape)
# 以IMREAD_UNCHANGED形式读取原图,有透明度通道
img2 = cv.imread("../imgs/opencv.png", cv.IMREAD_UNCHANGED)
print("不忽略透明度通道大小为:", img2.shape)
# 展示图像
cv.imshow('img IMREAD_GRAYSCALE', img)
cv.imshow('img1 original', img1)
cv.imshow('img2 original had alpha', img2)
# 窗口停留,0代表无限时停留
cv.waitKey(0)
Console output:
grayscale image size: (610, 570)
default read size: (610, 570, 3)
don't ignore transparency channel size: (610, 570, 4) Look! There is a transparent channel
Notice:
文件丢失
,权限不正确
,格式不受支持
or无效
etc. will return空矩阵
BGR
read by channel- When the read type is
IMREAD_GRAYSCALE
, it may be different from the result of cvtColor() to grayscale- Determine the type based on the content of the image itself, not the file extension
- The flags
IMREAD_UNCHANGED
are the original unaltered read mode- By default, the number of pixels must be less than
2^30
.OPENCV_IO_MAX_IMAGE_PIXELS
Limits can be set using system variables
3. Display the image in the window
opencv provides imshow()
functions to load images in specified windows
cv.namedWindow('img IMREAD_GRAYSCALE', cv.WINDOW_NORMAL)
# 参数一winname:string类型的窗口名称,参数二mat:图像。配合cv.namedWindow使用。
# 展示效果受到图像本身和namedWindow的类型影响。WINDOW_NORMAL能显示大于屏幕分辨率的图像。
cv.imshow('img IMREAD_GRAYSCALE', img)
Notice
When the image size is not 255, it is generally converted to 0-255 display by division or multiplication
- If the image pixel size is 8bit, that is, 2 to the 8th power = 256 pixels, it will be displayed directly.
- If the image is 16bit, that is, 2 to the 16th power=256*256=25536 pixels, it will be mapped to a size of 0-255 for display.
- If it is a floating point number 0-1, it will be mapped to 0-255 for display
4. Write the image to a file
There are two ways to save an image
-
Pressing Ctrl+S will display the local dialog to save the image at your own option. Pressing Ctrl+C will copy the image to the clipboard
-
Combining waitKey with code control
# waitKey(0)将无限显示窗口,直到按下任何键为止,适用于想显示图像的时候。 # waitKey(25)将显示一帧并等待大约25ms的按键,适用于逐帧显示视频的时候。 k=cv.waitKey(0) # 在无限显示窗口的时候,按下s,则保存到本地路径 #imwrite参数一filename:保存的路径,参数二img要保存的图像,参数三params可选,对特定格式进行编码 if k == ord("s"): cv.imwrite("opencv1.png", img)
Notice
When saving, you can decide how to save the image.
Only8bit的单通道
orBGR三通道
can use this function to save directly, others need a specific format or special processing, for example, 16bit needs to be saved as PNG, JPEG 2000, and TIFF.
5. Close the window
Just add the program at the end cv.destroyAllWindows()
and release the resources. In fact, in a simple program, it doesn't matter whether you add or not, and it will be released automatically when you close the program.
6. Integrate the code
The integration of all the above knowledge points, the usual picture reading steps, can be used on demand.
File structure:
complete code:
import cv2 as cv
import sys
# 创建窗口
# 参数一winname:string类型的窗口名称,参数二flags:窗口类型,使用规定好的类型,默认WINDOW_AUTOSIZE
cv.namedWindow('img IMREAD_GRAYSCALE', cv.WINDOW_NORMAL)
cv.namedWindow('img1 original', cv.WINDOW_NORMAL)
cv.namedWindow('img2 original had alpha', cv.WINDOW_NORMAL)
# 读取图像
# 参数一filename:string类型的图片路径,参数二flags:读取方式,比如读取哪个通道,哪些保留哪些丢弃等。使用规定好的类型。
# 以灰度图形式读取
img = cv.imread("../imgs/opencv.png", cv.IMREAD_GRAYSCALE)
# 因为imread读取不到的时候不会报错,所有这里需要判空
if img is None:
sys.exit("Could not read the image.")
print("灰度图大小为:", img.shape)
# 以默认形式读取原图,没有透明度通道
img1 = cv.imread("../imgs/opencv.png")
print("默认方式读取大小为:", img1.shape)
# 以IMREAD_UNCHANGED形式读取原图,有透明度通道
img2 = cv.imread("../imgs/opencv.png", cv.IMREAD_UNCHANGED)
print("不忽略透明度通道大小为:", img2.shape)
# 展示图像
# 参数一winname:string类型的窗口名称,参数二mat:图像。配合cv.namedWindow使用。
cv.imshow('img IMREAD_GRAYSCALE', img)
cv.imshow('img1 original', img1)
cv.imshow('img2 original had alpha', img2)
# waitKey(0)将无限显示窗口,直到按下任何键为止,适用于想显示图像的时候。
# waitKey(25)将显示一帧并等待大约25ms的按键,适用于逐帧显示视频的时候。
k = cv.waitKey(0)
if k == ord("s"):
cv.imwrite("../imgs/opencv1.png", img)
cv.destroyAllWindows()