Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The writing style of this series is explained

This series of columns will use the first question-and-answer writing format to quickly introduce you to the beginner, intermediate, and advanced levels of OpenCV.

1. How do images load and display in OpenCV?

The first step in learning OpenCV is to load an image. Like learning Hello Word in any programming language, load an image from your computer’s disk into the program so that you can perform subsequent operations.

In the loading process, we will use three core functions, as follows:

  • cv2.imreadRead the pictures
  • cv2.imshowShow the picture
  • cv2.imwriteWrite image

Any expert in image processing will start with these three functions, and you will be exposed to these three starting functions as well

Cv2. imread The usage and precautions of reading pictures

Before reading the picture, you should first know the location of the picture, namely the disk path of the computer. If you have no foundation in this part, it is recommended that you learn the knowledge of “absolute path” and “relative path”.

The cv2.imread function takes two mandatory parameters: the image path and the read mode, which tells the program how to read the image.

The value of the first parameter is very simple, is any image path on the computer, pay attention to the image suffix (extension),

The second parameter has a large number of values. You can master the following two values at the beginning stage

  • cv2.IMREAD_COLOR, read the color image, ignore the image transparency, use this value if read is transparentpngImage, be careful, transparent channels will not be read
  • cv2.IMREAD_GRAYSCALERead in grayscale mode
  • CV2.IMREAD_UNCHANGEDThe images read in includealphaA transparent channel

Here is a problem that needs special explanation:

There are other values, but you can skip them during the learning phase until you need them.

IMREAD_ANYCOLOR = cv2.imread_anycolor = cv2.imread_color = cv2.imread_color

What’s the difference?

Cv2. IMREAD_COLOR: Set this value to read only three BGR channels;

Cv2. IMREAD_ANYCOLOR: Set this value to read in any possible color channel;

ANYCOLOR will never notice the difference if you are always working with BGR channel images

The code to read the image is as follows

import cv2

img = cv2.imread('./test_img.jpg')
print(type(img))
Copy the code

There is nothing particularly difficult about the above code from the Python language, but the following error occurs

Since this series of columns was written to solve problems, we focus on bugs.

When you read an image, you will normally receive an image resource. The image type is shown below

<class 'numpy.ndarray'>
Copy the code

Many times, however, a null value is returned

<class 'NoneType'>
Copy the code

The reason for this value is that your image path is not correct, that is, your target image is not read, modify the image path to solve the problem.

In this part of the code ‘./test_img.jpg’ is the picture path, the professional term is called hard coding, the actual development process, it is recommended to use OS, PATH module to achieve automatic processing of the path.

There is another, less common, possibility that you will return

, which many people will ignore. This is that you do not have permission to access the folder in which the image is located. If this problem occurs, you will need to update your knowledge about file permissions.

Cv2. imshow shows the use of images and related matters needing attention

By default, the cv2.imshow function is used to display the image, and the window opened will adapt to the image. This function also contains two parameters, one is the window name, note not to use Chinese, and the other is the image.

The actual application of this function is relatively simple, but there are the following problems, need to pay attention to, the specific code is:

import cv2

img = cv2.imread('./test_img.jpg')
print(type(img))
cv2.imshow('image',img)
cv2.imshow('image',img)
cv2.waitKey(0)
Copy the code

After testing the above code, only one window appears after the program runs. This is because we used the cv2.imshow function twice. The first window name parameter is given the same value.

A new function, cv2.waitkey, has also been extended, without which the window flashes by.

This function is exactly the keyboard binding function, but it is used for the purpose of waiting for a response.

Cv2.waitkey has a parameter that means time, in milliseconds. If you want to set the specific number of milliseconds, just set the parameter value as expected. If set to 0, the window will wait indefinitely until it closes or a specific key is pressed.

There is a special note in this area, and you can expand the knowledge

In general, we carry out window detection according to the following Settings:

k = cv2.waitKey(0)
if k == 27:
    pass
Copy the code

The above code works fine on a 32-bit machine, but on a 64-bit machine it is recommended to modify it as follows:

k = cv2.waitKey(0) & 0xFF
if k == 27:
    pass
Copy the code

The above code can also be modified as follows:

import cv2

img = cv2.imread('./test_img.jpg')
cv2.startWindowThread()
cv2.namedWindow("image_show")
cv2.imshow("image_show", img)
cv2.waitKey()

Copy the code

The cv2.startWindowThread and cv2.namedWindow functions are added

The meanings of the above two functions are as follows:

  • cv2.startWindowThread: Opens a new thread to refresh images in real time
  • cv2.namedWindow: Name a window and make the window adjustable

The cv2.namedWindow function can be disassembled:

By default, we can’t adjust the proportion of Windows with Cv2. imshow. If we want to adjust it, we must use cv2.namedWidows function. After all, not all computers are high resolution.

The prototype of this function is as follows:

cv.namedWindow(winname, flags=None)
Copy the code

Where winname is the window name, cv2.imshow is called, the first parameter should match with it, the second parameter is the window flag.

  1. WINDOW_NORMAL: Allows users to adjust the window size at will after displaying the image;
  2. WINDOW_AUTOSIZE: Display window according to image size, does not allow the user to adjust the size;
  3. WINDOW_FREERATIO: window size adaptive ratio;
  4. WINDOW_KEEPRATIO: Keep the image in proportion.

The default is WINDOW_AUTOSIZE.

The above content is relatively simple, do not do the code demonstration.

Cv2. Imwrite Write image using method and related matters needing attention

You can save an image using the cv2.imwrite function, which also takes two parameters, the first is the image name and the second is the image to save

This function is very simple to use:

import cv2

img = cv2.imread('test_img.jpg')
cv2.imwrite('test.jpg',img)
Copy the code

This function also has a non-mandatory, optional argument, and it is open to different interpretations.

If a JPG image is saved, the image quality is expressed as an integer ranging from 0 to 100.

If you save a PNG image, the compression level is 3 by default

When using, refer to the following code:

cv2.imwrite('test.jpg',img, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
cv2.imwrite('test.png',img, [int(cv2.IMWRITE_PNG_COMPRESSION), 4])
Copy the code

If you use cv2.imwrite to find that the file is not saved successfully, note that the problem may be the Chinese path, such as the following code:

import cv2

img = cv2.imread('test_img.jpg')
cv2.imwrite('/ picture/test. The JPG',img)
Copy the code

After this problem occurs, it is recommended to modify the Chinese pathname, otherwise you can only switch to other image saving methods.