Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCV Tutorial

OpenCV Tutorial

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library.

OpenCV consists of a series of C functions and a small number of C++ classes, and also provides interfaces in languages such as Python, Java, and MATLAB.

OpenCV provides a large number of computer vision algorithms and image processing tools, widely used in image and video processing, analysis, and machine learning.

OpenCV is designed to provide a set of simple and easy-to-use computer vision foundation libraries, helping developers quickly build complex visual applications.


What you need to know before studying this tutorial

OpenCV supports many programming languages, but Python and C++ are the two most commonly used; Python has simple and easy-to-learn syntax, suitable for beginners.

Before starting to learn the OpenCV tutorial, we need to have a basic foundation in Python. If you are not yet familiar with Python, you can read our tutorial:


Historical development

The OpenCV project was first launched by Intel in 1999, dedicated to CPU-intensive tasks, as part of a plan that included things like ray tracing and 3D display.


Example

OpenCV reads and displays an image:

Example

Import the OpenCV library and use the alias cv instead of cv2

import

cv2

as

cv

Replace "path/to/image" with the actual image path, such as "cat.jpg" or "C:/images/dog.png"

img

=

cv.

imread

(

"path/to/image"

)

If the image path is wrong or the file does not exist, cv.imread() returns None

if

img

is

None

:

   

Print error information

   

print

(

"Error: Could not load image."

)

   

Exit program

    exit

(

)

"Display window" is the name of the display window and can be customized

img is the image data to be displayed

cv.

imshow

(

"Display window"

,

img

)

Wait for key input

The parameter 0 means wait indefinitely until the user presses any key

The return value k is the ASCII code of the key pressed by the user

k

=

cv.

waitKey

(

0

)

Check whether the user pressed the Esc key (ASCII code 27)

if

k

==

27

:

   

Close all OpenCV windows

    cv.

destroyAllWindows

(

)


Related links