opencv+ image processing (GUI) 1-0 image: create load display save close

  • Code address of this columnhttps://github.com/xiawei20161308104/xv_opencv_tutorials
  • Code path for this sectionxv_opencv_tutorials/ImageProcessinginOpenCV/load_img.py

0. Knowledge points in this section

  • create windownamedWindow
  • Load images locallyimread
  • display image in windowimshow
  • write image to fileimwrite
  • close the windowdestroyWindow destroyAllWindows

1. Create a window

opencv provides cv.namedWindowfunctions to create a window function

import cv2 as cv
# 参数一winname:string类型的窗口名称,参数二flags:窗口类型,使用规定好的类型,默认WINDOW_AUTOSIZE
cv.namedWindow('img', cv.WINDOW_AUTOSIZE)
cv.waitKey(0)

The effect of the above code
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

  1. If the filling is winnamerepeated, 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.
    winname is duplicated, only two windows are created
  2. WINDOW_AUTOSIZEMisfits may occur. The left image img only shows the upper half, which is incomplete, and the right image is the original image.The img of WINDOW_AUTOSIZE only shows the upper half, and the display is incomplete

2. Load images locally

opencv provides cv.imreadfunctions to realize the function of reading images. The types that can be read are:
The type that imread can read

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级透明程度.
imreadThe 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

The effect of three reading methods

Notice:

  • 文件丢失, 权限不正确, 格式不受支持or 无效etc. will return空矩阵
  • BGRread 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_UNCHANGEDare the original unaltered read mode
  • By default, the number of pixels must be less than 2^30. OPENCV_IO_MAX_IMAGE_PIXELSLimits 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

  1. 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
    insert image description here

  2. 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.
Only 8bit的单通道or BGR三通道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:
insert image description here
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()

Guess you like

Origin blog.csdn.net/cvxiayixiao/article/details/129896781