[Reprinted] Introduction to Image Processing of OpenCV-Python Series (5)

We have already talked about how to install OpenCV on various platforms. Starting today, we will officially enter the actual combat part. The first thing we need to do is how to read the image and display it, which is the most basic part of image processing.

First, let's understand a few functions :

cv2.imread():

Let's look at the prototype of the function:

retval=cv.imread(filename[, flags])

retval is a custom name, filename refers to the file name that needs to be loaded. Generally, most of the time, we load the file directly, so most of the time we can directly read the image, but in some cases, we want to directly The original image is converted to a grayscale image to facilitate the later morphological processing operations. Then the meaning of the second parameter is shown at this time. The second parameter belongs to a mark, which is used to specify the way of reading the image. There are three ways:

cv.IMREAD_COLOR : Load a color image. Any transparency of the image will be ignored. This is the default flag.

cv.IMREAD_GRAYSCALE : load the image in grayscale mode

cv.IMREAD_UNCHANGED : load image, including alpha channel

Of course, for convenience, they can also be represented by 1, 0, and -1 respectively. 1 refers to the original image, which is the first function; 0 refers to the second function to convert to grayscale; -1 refers to the third function.

cv2.imshow():

This function is used to display the image in the window, and the window automatically fits the current input image size. This function is complementary to the imread function. The function prototype is:

None=cv.imshow(winname, mat)

None means you don’t need to fill in, don’t care about it, we can directly use imshow, winname refers to the name of the image you output, can be customized, but do not use the Chinese name, otherwise it will be garbled; mat is the image you need to display.

This function should be followed by the **cv2.waitKey()** function, which displays the image in the specified milliseconds. If the waitkey function is not used, it will not display the image. For example, waitKey(0) will display the window indefinitely until any key is pressed (suitable for image display). waitKey(25) will display frames for 25 milliseconds, after which the display will be turned off automatically.

Another function **cv2.destroyAllWindows()** will destroy all the windows we created. If you want to destroy any specific window, use the function cv.destroyWindow() to pass the exact window name as a parameter.

Let's do an experiment now, first show the most basic images (both in the code picture):

Insert picture description here

Now we change the read image to grayscale, the last parameter of the mread function can perform related operations, when it is 0:

Insert picture description here

When it is 1:
Insert picture description here

If it is -1:
Insert picture description here

Now the window size of the output image is fixed, so if we want to adjust the window size, we need to use another function. In special cases, we can create an empty window and then load the image to the window. In this case, you can specify whether the window is resizable. This is done through the function cv.namedWindow(), now let’s experiment:
Insert picture description here

When we add a 0 after the function, we can use the mouse to manually adjust the size of the output image, which is very convenient. In fact, the parameter corresponding to 0 is cv2.WINDOW_NORMAL, which is to automatically adjust the image. Let's get a general understanding of the relevant parameters:

WINDOW_NORMAL or WINDOW_AUTOSIZE: WINDOW_NORMAL can adjust the window size, while WINDOW_AUTOSIZE will automatically adjust the window size to fit the displayed image (see imshow), and you cannot manually change the window size.

WINDOW_FREERATIO or WINDOW_KEEPRATIO: WINDOW_FREERATIO does not consider its ratio when adjusting the image, while WINDOW_KEEPRATIO keeps the image ratio.

WINDOW_GUI_NORMAL or WINDOW_GUI_EXPANDED: WINDOW_GUI_NORMAL is the old method of drawing windows without status bar and toolbar, and WINDOW_GUI_EXPANDED is the new enhanced GUI. Default flag == WINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED

If we want to save the processed image and save it in a folder, then we need to use the cv2.imwrite function.

cv2.imwrite ():

The imwrite function saves the image to the specified file. The image format is selected based on the file extension (see cv::imread for the extension list). Generally, only 8-bit single-channel or 3-channel (with "BGR" channel sequence) images can be saved using this function, except in the following cases:

For PNG, JPEG 2000 and TIFF formats, 16-bit unsigned (CV_16U) images can be saved

32-bit floating point (CV_32F) images can be saved in PFM, TIFF, OpenEXR and Radiance HDR formats; using LogLuv high dynamic range encoding (4 bytes per pixel) will save 3-channel (CV_32FC3) TIFF images

Use this function to save PNG images with Alpha channel. For this, an 8-bit (or 16-bit) 4-channel image BGRA is created, with the alpha channel at the end. Fully transparent pixels should have alpha set to 0, and completely opaque pixels should have alpha set to 255/65535

The function prototype is:

retval=cv.imwrite(filename, img[, params])

The first parameter is the file name, and the second parameter is the image to be saved. Now we write a comprehensive code:

	view plaincopy to clipboardprint?
 1. import numpy as np   
 2. import cv2 as cv      
 3. img =cv.imread('cat.jpg', 0)   
 4. cv.imshow('image', img)   
 5. k =cv.waitKey(0) & 0xff   
 6. if k == 27:  # wait for ESC key to exit  
 7.      cv.destroyAllWindows()   
 8. elif k == ord('s'):  # wait for 's' key to save and exit  
 9.     cv.imwrite('cat.png', img)  
 10.    cv.destroyAllWindows()

In the above program, load the image in grayscale, display the image, press the "s" key on the keyboard to save the image and exit, or press the ESC key to exit without saving. We press s experiment: the
Insert picture description here
image is saved.

Then the introductory part of image processing is officially over. This is also the foundation of the basics, and the follow-up will be very useful.
Check the article summary page https://blog.csdn.net/weixin_44237705/article/details/107864965
More openvino technical information can be exchanged in the group~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44237705/article/details/107933299