This is the first day of my participation in the August Text Challenge.More challenges in August

OpenCV entry

Author: ZackSock

1. What is OpenCV?

OpenCV is a cross-platform and Open Source Computer Vision Library. Open-source by Intel. The main library code is written by Intel in C/C++, part of the contribution library code provided by community programmers.

OpenCV not only supports multiple platforms, but also provides multiple language interfaces, including Java, Python, Ruby and so on. Python is used in this course.

Details can be found on OpenCV’s website: [opencv.org/][https://o…

2. What can OpenCV do?

OpenCV realized a lot of computer vision algorithms, including basic image operation, threshold processing, image filtering, morphological operations. In addition, OpenCV also provides a cascade classifier can be used for face detection.

Not only images, OpenCV also provides operations related to video processing. Let’s take a look at some examples.

(1) Edge detection

There are many algorithms for edge detection, among which Canny algorithm is the current optimal algorithm. The following picture is the edge detection achieved by Canny algorithm:

You can see that the edges of the phone are fully displayed. Canny also has good performance for portraits or other complex pictures. Here is the edge detection map of portraits:

(2) face detection

In OpenCV’s official website provides the face feature file, we can use Haar cascade classifier to quickly realize the operation of face detection, the following is the use of OpenCV face detection:

The detection effect is quite good.

(3) image corrosion

Corrosion is one of the most basic morphological operations, we can eliminate some small cracks in the picture by corrosion operation, for example, the following is a simple example of corrosion operation:

In addition to this, OpenCV has many other operations that will be covered in future articles. Let’s install OpenCV in Python.

3. Install OpenCV in Python

To use OpenCV in Python, it’s very easy. All you need to do is install a module using PIP.

pip install opencv-python
Copy the code

The main library installed above is OpenCV, which is provided by Intel corporation. We can install the contribution library by saying:

pip install opencv-contrib-python
Copy the code

Not installing the contribution library does not affect our use of the basic functionality of the main library.

In a Python file, we import OpenCV with the following statement:

import cv2
Copy the code

Once imported, we can use it.

4. Picture reading

The format of the function to read the picture is as follows:

retval = cv2.imread(filename, flags=None)
Copy the code
  • Retval: Picture object returned (of type numpy.ndarray), None on failure to read.

  • Filename: specifies the filename. This parameter is mandatory.

  • Flags: Read flags that control the type of file read. We can choose not to pass a value to it.

At this stage we can think of retval simply as a picture object. Flags common values can be found in the following table:

value meaning
cv2.IMREAD_UNCHANGED The same format
cv2.IMREAD_GRAYSCALE grayscale
cv2.IMREAD_COLOR 3-channel BGR graph, default value for flags

For now we don’t need to understand the meanings of each parameter, we won’t use flags parameter at this stage. Here we use the imread function to read an image:

import cv2
retval = cv2.imread('im.jpg')
Copy the code

This is the operation to read the image.

5. Display images

Image reading is the most basic operation, and subsequent image operations need to obtain the image object first, such as the next to talk about the display picture.

The action of displaying an image is usually accompanied by waiting and destroying it. In case you don’t understand, let’s look at the code for displaying an image:

import cv2
im = cv2.imread('im.jpg')
# display images
cv2.imshow(im, 'im')
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

The main function above is the imshow function, which has the following format:

imshow(winname, mat)
Copy the code

The parameters are explained as follows:

  • Winname: The name of the window that displays the picture
  • Mat: Picture object, that is, the picture object we get through imread.

We can call the imshow function to display the image, but the imshow function will only show us the image for a moment. What we see is a flash window, so we need to use our waitKey function. The waitKey function has the following format:

key = waitKey(delay=None)
Copy the code

It waits for input from the user and returns the ASCII value of a keyboard. With the waitKey function we can make the window display.

The function of the delay parameter is the number of milliseconds to wait. We can choose not to give it or we can give 0, which means we’re waiting.

The destroyAllWindows function is a general operation, and since OpenCV is written in C/C++, we need to reclaim the window’s memory manually.