1. Getting started with Open source CV

1. Getting started with Open source CV1.1. Introduction to OpenCV1.2, OpenCV image reading and display1.2.1, image reading:1.2.2, Image display1.2.3, Code and actual effect display1. 3. OpenCV image writing1.3.1. Function method: cv2.imwrite('new_img_name', img)1.3.2. Code and actual effect display1.4, OpenCV camera read and display video1.4.1. camera read1.4.2. display camera video1.4.3.Code and actual effect display1.5, OpenCV pixel operation1.5.1.pixel operation, we can change the pixel color at any position.1.5.2.Code and actual effect display

 

1.1. Introduction to OpenCV

image-20220217202241894

What is OpenCV? Its full name is Open source Computer Vision Library. As shown in the picture above, we can see the OpenCV logo, which is composed of three small circles of the three primary colors of R, G, and B. In other words, it is an open source API function library for computer vision. This means that

(1) Whether it is scientific research or commercial application, it can be used for development;

(2) The source code of all API functions is public, and you can see the program steps implemented internally;

(3) You can modify the source code of OpenCV and compile it to generate the specific API function you need.

The image processing on ROSMASTER uses some functions of the OpenCV function library, or it can be said that it is indispensable in most image processing design fields. OpenCV has been used in intrusion detection, specific target tracking, target detection, face detection, face recognition, face tracking and other fields many years ago. And these are just the tip of the iceberg of its application. Since we know that OpenCV is so universal, in this chapter we will introduce you to some very basic image processing functions used in our course, which are also some universal functions. Here we first have a general understanding of these knowledge, and then there are two practical projects of color recognition and tracking and face recognition and tracking to teach you how to get started. However, the powerful application functions provided by OpenCV are far more than that. If you are very interested in the development of Opencv computer vision library and want to learn more, the following are several websites for your reference and study:

OpenCV official homepage: https://www.opencv.org

OpenCV Chinese Forum: http://www.opencv.org.cn

OpenCV CSDN Forum: https://bbs.csdn.net/forums/OpenCV

1.2, OpenCV image reading and display

1.2.1, image reading:

img = cv2.imread('yahboom.jpg', 0) The first parameter is the path of the image, and the second parameter is how to read the image.

cv2.IMREAD_UNCHANGED: keep the original format unchanged, -1;

cv2.IMREAD_GRAYSCALE: read the image in grayscale mode, which can be represented by 0;

cv2.IMREAD_COLOR: read a color image, which can be represented by 1; the default value

cv2.IMREAD_UNCHANGED: read an image and include its alpha channel, which can be represented by 2.

1.2.2, Image display

cv.imshow('frame', frame): Open a window named frame and display frame data (image/video data)

Parameter meaning:

The first parameter indicates the name of the window to be created

The second parameter indicates the image to be displayed

1.2.3, Code and actual effect display

Run the program

image-20220211155455979

1. 3. OpenCV image writing

1.3.1. Function method: cv2.imwrite('new_img_name', img)

Parameter meaning:

The first parameter is the saved file name

The second parameter is the saved image

1.3.2. Code and actual effect display

Run the program

image-20220211160650131

1.4, OpenCV camera read and display video

1.4.1. camera read

capture=cv.VideoCapture(0)

Parameter meaning:

The parameter in VideoCapture() is 0, which means to open the built-in camera of the notebook. The parameter is the video file path to open the video, such as cap = cv2.VideoCapture("../test.avi")

1.4.2. display camera video

ret,img = frame.read()

Return value meaning:

ret: ret is a bool value, which determines whether the correct frame is read back

img: image data of each frame

1.4.3.Code and actual effect display

Run the program

Screenshot from 2022-02-09 14-38-31

1.5, OpenCV pixel operation

1.5.1.pixel operation, we can change the pixel color at any position.

First, we need to read the image, then modify the value of bgr and assign an area to black.

1.5.2.Code and actual effect display

Run the program

image-20220211163651407

The red frame is the modified color value.