"Digital Image Processing-OpenCV/Python" serial (5) Image display

"Digital Image Processing-OpenCV/Python" serial (5) Image display


This book’s JD discount purchase link: https://item.jd.com/14098452.html
This book’s CSDN exclusive serial column: https://blog.csdn.net/youcans/category_12418787.html

insert image description here


Chapter 1 Basic Operations of Images

In order to facilitate beginners to learn OpenCV-Python from scratch, this book starts with basic operations such as image reading, saving and display, so that readers can use and understand each routine in this book step by step.


1.2 Display of images


The function cv.imshow is used to display images.

function prototype

cv.imshow(winname, mat) → None
cv.waitKey([, delay]) → retval

The function cv.imshow is used to display the OpenCV image in the specified window. By default, the pixel value of the image is mapped to [0,255] for display. The function cv.waitKey is used to wait for a key event or delay (milliseconds) to keep the window displayed.

Parameter Description

  • winname: The name of the specified display window.
  • mat: The displayed OpenCV image, which is a multidimensional Numpy array.
  • delay: delay time in milliseconds, 0 means infinite delay.

Note:
(1) The function cv.imshow must be followed by the function cv.waitKey. Without the function cv.waitKey, the display window will flash by.
(2) The image display window will automatically close after setting the delay (milliseconds) in the function cv.waitKey. waitKey(0) means that the window will not be closed automatically.
(3) The display window can be created and named using the function cv.namedWindow, and then displayed using the function cv.imshow. If the specified window has not been created yet, the function cv.imshow will create a window that adapts to the image size.
(4) It is recommended to use the function cv.destroyWindow to close the specified window before the program ends. You can also use the function cv.destroyAllWindows to close all display windows.
(5) Multiple windows can be used at the same time, but different window names (winname) must be defined.
(6) If the resolution of the displayed image is higher than the screen resolution, you must first create a window using the function cv.namedWindow.
(7) If the data type of the displayed image is floating point, the pixel value must be normalized to [0.0, 1.0] for normal display.


[Routine 0104] Display the image in the OpenCV image window

This routine is used to display images in the OpenCV image window, as shown in Figure 1-1.

# 【0104】在OpenCV 图像窗口中显示图像
import cv2 as cv

if __name__ == '__main__':
    filepath = "../images/Lena.tif"  # 读取文件的路径
    img = cv.imread(filepath, flags=1)  # flags=1 读取彩色图像(BGR)
    gray = cv.imread(filepath, flags=0)  # flags=0 读取为灰度图像

    cv.imshow("Lena", img)  # 在窗口 img1 显示图像
    cv.imshow("Lena_gray", gray)  # 在窗口 img2 显示图像
    key = cv.waitKey(0)  # delay=0, 不自动关闭
    cv.destroyAllWindows()

insert image description here

Figure 1-1 Displaying the image in the OpenCV image window

Program description
(1) Display the color or grayscale image in the window named "Lena" or "Lena_gray" (see Figure 1-1).
(2) Note that the function cv.imshow must be followed by the function cv.waitKey, otherwise the display window will flash by.


Note: The routine to read the image Lena.tif is as follows.
insert image description here

JD discount purchase link for this book: https://item.jd.com/14098452.html


Copyright statement:
youcans@xupt original work, reprints must be marked with the original link: (https://blog.csdn.net/youcans/article/details/132688395)
Copyright 2023 youcans, XUPT
Crated: 2023-09-05

Welcome to follow this book's CSDN exclusive serial column
"Digital Image Processing-OpenCV/Python": https://blog.csdn.net/youcans/category_12418787.html

Guess you like

Origin blog.csdn.net/youcans/article/details/132688395