HighGUI host computer development for python development (1)

Author: ywsydwsbn
Word Count: 5702
Time: 2020-8-24 16:56
Description: Like in the first point of view, a habit! ! !

Introduction to HighGUI

Overview

We need to use visualization when testing the algorithm and checking the effect of the algorithm. Dynamically adjust the threshold . When you see the feedback in real time, you also need to use the graphical interface to edit, so we need to use it here opencv中的HighGUI.

HighGUIIt is a graphical (GUI) component in opencv . We can develop some simple PCs through HighGUI.

What interfaces does OpenCV's HighGUI provide?

Mainly provide the following functions

  • Create multiple windows (Windows), display images in the windows
  • Create simple interactive components such as buttons and sliders, and get their values
  • Listen to mouse events, language key events

Matplotlib shows the biggest difference between the picture and HighGUI

  1. MatplotlibIs static and cannot be interactive
  2. HighGUIIs dynamic and can be interactive

HighGUI window

namedWindow

Create a window called image_win

# 创建一个名字叫做 image_win的窗口
cv2.namedWindow('image_win')

We can also pass in some 参数(flags)to achieve some settings of the window.

  • flagsIn fact, it is an integer, and the specific bit of the binary is used to indicate whether an option is option A (binary 0) or option B (binary 1).

Setting option 1: Window size

WINDOW_NORMAL 1 means that the window size can be changed by dragging the window.

WINDOW_AUTOSIZE 0 Default According to the size of the screen and the picture, auto zoom. Manually changing the window size is not allowed.

Setting option 2: Setting the aspect ratio

WINDOW_FREERATIO 256 does not fix the aspect ratio.

WINDOW_KEEPRATIO 0 The default aspect ratio is fixed, that is, the window is dragged to zoom, and the original aspect ratio must be maintained.

Setting option 3: Window GUI version

WINDOW_GUI_NORMAL 16 Old version window components. The statusbar and toolbar are not supported. It is the status bar and toolbar at the top of the window.

WINDOW_GUI_EXPANDED 0 Default GUI window with enhanced functions in the new version.

We can pass in the value of multiple options at the same time through a parameter by bitwise OR .

  • flagsThe default value of is 0 , which is equivalent toWINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED

So the above statement is equivalent to:

# 创建一个名字叫做 image_win的窗口
cv2.namedWindow('image_win', flags=cv2.WINDOW_AUTOSIZE | cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_EXPANDED)

If I want to set, the window can be dragged freely, then I need to write

# 创建一个名字叫做 image_win的窗口
cv2.namedWindow('image_win',  flags=cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_EXPANDED)

And because the other two options are the default options, the value is 0, so the writing is equivalent to

# 创建一个名字叫做 image_win的窗口
cv2.namedWindow('image_win',  flags=cv2.WINDOW_NORMAL)

If I want this window to be drag-and-drop and not fixed 宽高比(ratio), then I should actually write it like this.

# 创建一个名字叫做 image_win的窗口
cv2.namedWindow('image_win', flags=cv2.WINDOW_NORMAL | cv2.WINDOW_FREERATIO)

If the window shows nothing, the window is actually a 占位符(Placeholder)function.

Create window example

import numpy as np
import cv2

# 创建一个名字叫做 image_win的窗口
cv2.namedWindow('image_win', cv2.WINDOW_NORMAL)

# windows下啥也不放置

# 检测按下的按钮
print("请按任意键关闭窗口")

# 如果没有下面的waitKey, 窗口会一闪而过, 后面会讲解
key_pressed = cv2.waitKey(0)

# cv2.destroyAllWindows()
cv2.destroyWindow('image_win')

Insert picture description here

Import pictures

Use imreadfunction to read color image/gray image

API explanation of imread

When we read a picture, we use a cv2.imreadfunction, and the first parameter passed in is the path of the picture.

I placed a photo of lena1.jpg in the same directory of the code.
Insert picture description here

Note that this is easy to make mistakes. The path format of Linux and Windows are different, and you also need to pay attention to relative and absolute paths.

# 导入一张图像 模式为彩色图片
img = cv2.imread('lena1.jpg')

So you read in this picture of Lena.

What formats of pictures does opencv support?

Specifically, we can check the documentation and enter in the python terminal:

help(cv2.imread)

PS: You have to import the cv2 module.

Import RBG color image or grayscale image?

The second parameter is the image color space , the default is RGB color mapcv2.IMREAD_COLOR

The above statement has the same effect as the following.

# 导入一张图像 模式为彩色图片
img = cv2.imread('cat.jpg', cv2.IMREAD_COLOR)

If you want to import grayscale images , you need to pass incv2.IMREAD_GRAYSCALE

img = cv2.imread('cat.jpg', cv2.IMREAD_GRAYSCALE)

Window display

Show/update the image in the window

API explanation of imshow

In HighGUI image display is required to imshowfunction, the first argument, we pass the name of the window , and the second parameter is the Image object .

# 展示图像
cv2.imshow('image',img)

If imagethis window has not been declared before, then imagea window named by the name will be created at the same time , and then the image in the window will be updated.

Please note that cv2.imshow()after you execute this , the window will flash by. At this time, you need to use cv2.waitKeythis function , which will be discussed later.

Picture save

To save the image, you need to use the imwritefunction.

Simple saving of images

This time we are still reading the lena picture.

Insert picture description here

After reading the img, we perform various operations on the image, such as image equalization, image drawing, etc.

Next, use the imwritefunction directly

  • The first parameter ./lena1.pngis the path and name of the file to be saved.
  • The second parameter is the image matrix img .
import numpy as np
import cv2

# 导入一张图像 模式为彩色图片
img = cv2.imread('lena1.jpg', cv2.IMREAD_COLOR)

# 读入灰度图
cv2.imwrite('lena1.png', img)

Insert picture description here
I will jpg图片save this as png图片.

The specific saving of the image format is determined by the suffix of the image path name, and the suffix is .pngsaved as pnga picture in the format.

Knowledge charging station:
jpg and jpeg are actually the same thing, jpg is short for jpeg. jpeg is the abbreviation of Joint Photographic Experts Group (Joint Photographic Experts Group)

Image compression save

The fidelity of the image is specified by the third parameter of the imwrite function.

The third parameter has different meanings for different image saving types .

In the image compression demonstration, we still use Lena's picture.

JPEG compression save

JPEGThe third parameter represents the image qualitycv2.IMWRITE_JPEG_QUALITY , the value range is 0-100, the default is 95 .

cv2.imwrite('bear_quality_50.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 50])

We divide the quality level into 10 segments to show the pictures under various quality levels.

import numpy as np
import cv2

# 导入一张图像 模式为彩色图片
img = cv2.imread('lena1.jpg', cv2.IMREAD_COLOR)


for quality in range(0, 100, 10):

    # 保存为PNG图片
    cv2.imwrite('lena_quality_{}.jpg'.format(quality), img, [cv2.IMWRITE_JPEG_QUALITY, quality])

Insert picture description here
Picture size comparison:
Insert picture description here

PNG compression save

PNGIt referred to the corresponding level of compression cv2.IMWRITE_PNG_COMPRESSION

The compression level ranges from 0 to 9. 0 means no compression, and 9 means maximum compression .

For example, if we use compression level 4 to store pictures, we can write like this.

cv2.imwrite('bear_compression_4.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 4])

Let everyone intuitively feel the effects of different compression levels:

import numpy as np
import cv2

# 导入一张图像 模式为彩色图片
img = cv2.imread('lena1.png')


for cmpi in range(0, 10):
    # 保存为PNG图片
    cv2.imwrite('lena_compression_{}.png'.format(cmpi), img, [cv2.IMWRITE_PNG_COMPRESSION, cmpi])
    print("压缩级别 {}".format(cmpi))

Insert picture description here

Insert picture description here
To be honest, open the picture to see the sharpness, it is very difficult for our naked eyes to see the difference, but we can reflect it through the file attribute file size.
Insert picture description here

This part first explains the related theory of HighGUI in detail, and also explains the corresponding operation processing of the picture. The next section will focus on the detailed analysis of the HighGUI project development skills.

Welcome everyone to study together (follow, comment, and like)

Guess you like

Origin blog.csdn.net/ywsydwsbn/article/details/108199147