This is the first day of my participation in the More text Challenge. For details, see more text Challenge

OpenCV is a C++ library, a popular computer vision programming library, used to deal with computer vision problems in real time, it covers many modules in the field of computer vision. In Python, OpenCV library is often used to realize image processing.

This article describes how to use OpenCV in Python3 to implement basic operations for image processing:

  • Read in the image
  • According to the image
  • Copy the image
  • Save the image

Computer environment Preparation

Python version:Python3.7

OpenCV version:OpenCV4.1

Read in the image

Imread (filepath,flags) is used to read an image in Opencv. The parameters in this function are described as follows:

  • filepath: Read the full path to the image
  • flags: The sign to read in the picture
  • cv2.IMREAD_COLOR: the default parameter, which reads in color images and ignores the alpha channel, can be specified as 1
  • cv2.IMREAD_GRAYSCALE: reads a grayscale image, which can be specified as 0
  • cv2.IMREAD_UNCHANGED: Read the full image, including the alpha channel
import cv2
#1Read the full color picture0Img = cv2.imread('hello.jpg'.1)
img1 = cv2.imread('hello.jpg'.0)
Copy the code

According to the image

In Opencv, use cv2.imshow(name,img) to display the image. The parameters in this function are described as follows:

  • name: The name of the window that displays the image
  • img: is the image to be displayed (imRead image), the window size is automatically adjusted to the image size

It is important to pause the application when displaying the image, otherwise the image will flash by and the user will not see it.

Cv2.waitkey (0)

import cv2
img = cv2.imread('hello.jpg'.1The first parameter is the window name. The second parameter is the image variable that is read.'image'# pause the cv2 module or the image window will disappear immediately0)
Copy the code

The running results are as follows:

If img = cv2.imread('hello.jpg'.1Img = cv2.imread()'hello.jpg'.0)
Copy the code

After running the program, the following is displayed:

Copy the image

In Opencv, if you want to copy the current image, you can use the following statement:

-img1 = img.copy() : img1 is the new image, img is the original image

import cv2
img = cv2.imread('hello.jpg'.0) img1=img.copy() # display the image in the window. The first parameter is the window name. The second variable is the image read.'image',img1) # Pause the cv2 module or the image window will disappear immediately0)
Copy the code

After running the program, the following is displayed:

Save the image

Opencv uses the imwrite(“path”,image) function to store images. The parameters in this function are described as follows:

  • path: Save the image path (custom name)
  • image: Read the picture object
import cv2
img = cv2.imread('hello.jpg'.0The first parameter is the window name. The second parameter is the image variable that is read.'image',img)

cv2.imwrite('D:\Pycharm\Demo1\opencv\hello.jpg'Cv2.waitkey (cv2.waitkey (cv2.waitkey (cv2.waitkey))0)
Copy the code

After running the program, the following is displayed:

Set to save the file, also stored in the specified directory:

conclusion

A series of articles will be published this month,

A wonderful article, please pay attention to.