Python Image Processing: OpenCV Getting Started Tutorial

1. Overview of Python image processing

1 Basic concepts of image processing

The so-called image processing refers to the process of processing and enhancing images using computer technology. It is an integral part of computer vision and can be used in many fields.

2 Advantages of Python in image processing

Python is a high-level programming language that is widely used in fields such as data analysis, machine learning, and artificial intelligence. In the field of image processing, Python is associated with OpenCV, which is an open source computer vision library developed by Intel and can be used in image processing, video processing, machine vision and other fields.

Python has many advantages in the field of image processing, including:

  • The code is concise, easy to understand, easy to use, and does not require much programming experience.
  • There are powerful image processing libraries (such as OpenCV) that can support multiple image formats
  • Ability to seamlessly integrate with other Python libraries (such as NumPy, SciPy)

2. Introduction to OpenCV

1 Overview of OpenCV

OpenCV is an open source library for computer vision. It was developed by Intel Corporation and supports multiple programming languages ​​such as C++, Python, and Java. Mainly used for developing real-time computer vision applications. It has powerful image processing capabilities and supports multiple image formats and mathematical operations, making it easy for developers to use and customize this library. In addition, it also has good cross-platform performance and can run on multiple operating systems such as Windows, Linux and macOS.

2 Features of OpenCV

OpenCV mainly has the following features:

  • Ability to process images and videos, supporting multiple image formats
  • Provides basic image processing functions (such as filtering, morphological operations, edge detection, etc.)
  • Supports advanced functions such as face recognition, target tracking, and motion analysis
  • Optimized for computing efficiency, two programming methods, C++ and Python, are provided.
  • It has good cross-platform performance and can run on multiple operating systems such as Windows, Linux and macOS.
  • Image/video import and export possible

3 Application areas of OpenCV

OpenCV is widely used in computer vision and machine vision fields, including:

  • face recognition
  • Target detection and tracking
  • motion capture
  • Video analysis and processing
  • self-driving car
  • Augmented reality technology, etc.

3. OpenCV installation and environment configuration

1 How to install OpenCV

The easiest way to install OpenCV in Python is to use the pip command as follows:

pip install opencv-python 

This command will automatically download and install the latest stable version of OpenCV.

2 OpenCV environment configuration

To use OpenCV for image processing, you need to configure the environment. First, you need to import the OpenCV library in Python. In Python, you can use the following code:

import cv2

If no error message appears, the OpenCV library has been imported successfully. Next, we can use the functions in the OpenCV library for image processing!

4. Basic knowledge of image processing

Before doing image processing, you must first understand some basic knowledge. This knowledge includes how to read, display, and save images; how to adjust the size and color space of images. How to perform geometric transformation, threshold operation, convolution filtering and other operations on images.

1 Read Display Save Image

You can use the functions in the OpenCV library to read, display and save images in Python. The specific code is as follows:

import cv2

# 读取图像
img = cv2.imread('image.jpg')

# 显示图像
cv2.imshow('Image', img)
cv2.waitKey(0)

# 保存图像
cv2.imwrite('new_image.jpg', img)

Among them, thecv2.imread() function is used to read an image, the cv2.imshow() function is used to display an image, and the cv2.waitKey() function is used To wait for keyboard input, the cv2.imwrite() function is used to save the image to the specified file. While the program is running, press any key to close the image window.

2 Adjust the image size

In practical applications, the image may need to be resized. We can use the cv2.resize() function to adjust the size of the image. The specific code is as follows:

import cv2

# 读取图像
img = cv2.imread('image.jpg')

# 调整图像大小
resized_img = cv2.resize(img, (600, 600))

# 显示原图和调整后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Resized Image', resized_img)
cv2.waitKey(0)

# 保存调整后的图像
cv2.imwrite('resized_image.jpg', resized_img)

Wherecv2.resize()The second parameter of the function is the new image size in tuple form, that is, the new image width and height. After resizing, the results can be observed by comparing with the original image.

3 Modify the color space of the image

The color space of the image includes grayscale space and color space. We can use the cv2.cvtColor() function to convert the color space of the image. The specific code is as follows:

import cv2

# 读取图像
img = cv2.imread('image.jpg')

# 将图像转换为灰度空间
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 显示原图和灰度图
cv2.imshow('Original Image', img)
cv2.imshow('Gray Image', gray_img)
cv2.waitKey(0)

# 保存灰度图
cv2.imwrite('gray_image.jpg', gray_img)

Wherecv2.cvtColor()The first parameter of the function is the original image, and the second parameter specifies the converted color space. In this example we convert the original image to grayscale space (GRAY) and save it as a grayscale image.

4. Geometric transformation of images

By performing geometric transformation on the image, operations such as rotation, translation, and scaling of the image can be achieved. The following uses rotation as an example to introduce how to perform geometric transformation on an image. The specific code is as follows:

import cv2
import numpy as np

# 读取图像
img = cv2.imread('image.jpg')

# 获取图像的旋转矩阵
rows, cols = img.shape[:2]
M = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1)

# 进行图像旋转
rotated_img = cv2.warpAffine(img, M, (cols, rows))

# 显示原图和旋转后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Rotated Image', rotated_img)
cv2.waitKey(0)

# 保存旋转后的图像
cv2.imwrite('rotated_image.jpg', rotated_img)

Among them, thecv2.getRotationMatrix2D() function is used to obtain the rotation transformation matrix, specifying the rotation center and rotation angle (45 degrees), and the cv2.warpAffine() function is used to perform rotation operations.

5. Threshold the image

When performing a threshold operation, you can compare the grayscale value (or RGB value) of the image pixel with the specified threshold, set the pixel value that is greater (or less) than the threshold to a specific value, and set the pixel value that is less than (or greater than) the threshold. The value is set to another specific value. The following takes binarization of image gray values ​​as an example to introduce how to perform threshold operations. The specific code is as follows:

import cv2

# 读取图像并转换为灰度图
img = cv2.imread('image.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 进行阈值操作
_, threshold_img = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY)

# 显示原图和阈值化后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Threshold Image', threshold_img)
cv2.waitKey(0)

# 保存阈值化后的图像
cv2.imwrite('threshold_image.jpg', threshold_img)

wherecv2.threshold() function is used to perform threshold operations. The second parameter of this function is the specified threshold, the third parameter is the pixel value greater than the threshold, and the fourth parameter is the pixel value less than the threshold.

6 Image convolution and filtering operations

Image convolution and filtering operations are a basic method for smoothing and enhancing images. We can use thecv2.filter2D() function to perform convolution and filtering operations. The specific code is as follows:

import cv2
import numpy as np

# 读取图像
img = cv2.imread('image.jpg')

# 定义滤波器
kernel = np.ones((5, 5), np.float32) / 25

# 进行滤波操作
filtered_img = cv2.filter2D(img, -1, kernel)

# 显示原图和滤波后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Filtered Image', filtered_img)
cv2.waitKey(0)

# 保存滤波后的图像
cv2.imwrite('filtered_image.jpg', filtered_img)

wherecv2.filter2D() function is used to perform convolution and filtering operations. In this example, an average filter is used, in which each pixel value is equal to the average of the pixel values ​​in the surrounding 5x5 area

5. Advanced skills in image processing

After mastering basic image processing knowledge, we can continue to learn some advanced techniques, such as image edge detection, feature extraction, contour extraction, segmentation and separation, morphological operations, etc. This section will introduce you to the basic concepts and implementation methods of these techniques.

1 Edge detection of images

Edge detection is a common task in image processing. Its purpose is to find distinct edges, contours or lines in an image. Some specific algorithms can be used to implement image edge detection such as Sobel operator, Laplacian operator and Canny operator. The following uses the Canny operator as an example to introduce how to perform edge detection on images. The specific code is as follows:

import cv2

# 读取图像并转换为灰度图
img = cv2.imread('image.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 进行边缘检测
edges = cv2.Canny(gray_img, 100, 200)

# 显示原图和边缘检测后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Edges', edges)
cv2.waitKey(0)

# 保存边缘检测后的图像
cv2.imwrite('edges.jpg', edges)

wherecv2.Canny() function is used for edge detection. The second and third parameters of this function are the specified thresholds, which are used to control the sensitivity of edge detection.

2 Feature extraction of images

Image feature extraction is a processing method that converts the information in the image into numerical features. It is widely used in image recognition, target detection and other fields. We can use some commonly used algorithms to extract features from images, such as SIFT, SURF, ORB, etc. The following uses the SIFT algorithm as an example to introduce how to extract features from images. The specific code is as follows:

import cv2

# 读取图像并转换为灰度图
img = cv2.imread('image.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 初始化SIFT算法对象并提取图像的关键点和描述符
sift = cv2.xfeatures2d.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(gray_img, None)

# 在图像中绘制关键点
res_img = cv2.drawKeypoints(img, keypoints, None)

# 显示原图和特征点标注后的图像
cv2.imshow('Original Image', img)
cv2.imshow('SIFT Features', res_img)
cv2.waitKey(0)

# 保存特征点标注后的图像
cv2.imwrite('sift_features.jpg', res_img)

Whereincv2.xfeatures2d.SIFT_create() function is used to initialize the SIFT algorithm object, sift.detectAndCompute() function is used to extract key points and descriptors of the image, cv2.drawKeypoints()Function is used to draw key points in the image.

3 Extract contours from images

Contour extraction refers to performing boundary extraction and connectivity check on the image to obtain the contour shape in the image. We can use the cv2.findContours() function of the OpenCV library to extract contours from the image. The specific code is as follows:

import cv2

# 读取图像并转换为灰度图
img = cv2.imread('image.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 进行边缘检测
edges = cv2.Canny(gray_img, 100, 200)

# 进行轮廓提取
contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# 在图像中绘制轮廓
res_img = cv2.drawContours(img, contours, -1, (0, 255, 0), 2)

# 显示原图和绘制轮廓后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Contours', res_img)
cv2.waitKey(0)

# 保存绘制轮廓后的图像
cv2.imwrite('contours.jpg', res_img)

Wherecv2.findContours() function is used for contour extraction. The second parameter of this function specifies the retrieval mode of the contour, and the third parameter specifies the approximation method of the contour.cv2.drawContours() function is used to draw contours in an image.

4 Segment and separate images

Image segmentation and separation is a processing method that splits a complex image into multiple independent regions or images. Some algorithms can be used to segment and separate images, such as GrabCut algorithm, K-means algorithm, etc. The following uses the K-means algorithm as an example to introduce how to segment and separate images. The specific code is as follows:

import cv2
import numpy as np

# 读取图像
img = cv2.imread('image.jpg')

# 获取图像像素并进行K-means聚类
pixel_values = img.reshape((-1, 3))
pixel_values = np.float32(pixel_values)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
_, labels, center = cv2.kmeans(pixel_values, 5, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

# 对图像进行分割和分离
center = np.uint8(center)
res_img = center[labels.flatten()]
res_img = res_img.reshape((img.shape))

# 显示原图和分割后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Segmented Image', res_img)
cv2.waitKey(0)

# 保存分割后的图像
cv2.imwrite('segmented_image.jpg', res_img)

wherecv2.kmeans() function is used to perform K-means clustering. The second parameter of this function specifies the number of clusters, and the third parameter is the initialized cluster center. < The /span>np.uint8() function is used to convert the cluster center into an unsigned 8-bit integer.

5 Perform morphological operations on images

Morphological operations are a common image processing method. It can perform image expansion, erosion, opening operations, closing operations, etc., and is used for tasks such as image denoising, segmentation, and detection. We can use the cv2.erode(), cv2.dilate(), cv2.morphologyEx() functions of the OpenCV library to perform morphological operations on images. The following takes the opening operation as an example to introduce how to perform morphological operations on images. The specific code is as follows:

import cv2
import numpy as np

# 读取图像并转换为灰度图
img = cv2.imread('image.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 对图像进行二值化处理
_, binary_img = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY)

# 进行开运算操作
kernel = np.ones((5, 5), np.uint8)
opened_img = cv2.morphologyEx(binary_img, cv2.MORPH_OPEN, kernel)

# 显示原图和开运算后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Opened Image', opened_img)
cv2.waitKey(0)

# 保存开运算后的图像
cv2.imwrite('opened_image.jpg', opened_img)

Among them, thecv2.threshold() function is used to perform image binarization processing, and the cv2.morphologyEx() function is used to perform morphological operations. The second parameter of this function specifies The type of operation is specified, and the third parameter is the kernel function. In this example, a 5x5 all-1 kernel function is used for the open operation.

At this point, you have mastered advanced image processing techniques, including image edge detection, feature extraction, contour extraction, segmentation and separation, morphological operations, etc. These techniques can help you process images better and provide more image feature information, laying a good foundation for subsequent processing tasks.

6. Actual case: Image processing project demonstration based on OpenCV

1 Face recognition example

Algorithm overview

Face recognition is an automatic recognition technology based on images and videos. In image processing, we usually use classifiers based on facial features for face detection and face recognition. OpenCV provides many classifiers based on face recognition, including Haar classifier, LBP classifier, etc. We can use these classifiers to build our own face recognition system.

Code

# 导入OpenCV和numpy库
import cv2
import numpy as np

# 加载人脸分类器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# 加载图片并转化为灰度图像
img = cv2.imread('img.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 检测人脸
faces = face_cascade.detectMultiScale(gray, 1.2, 5)

# 绘制矩形框
for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

# 显示结果
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Implementation instructions

First, you need to import the required libraries.cv2The module is the interface of OpenCV under Python3.0, and numpy is the scientific computing toolkit, used here for array processing.

import cv2
import numpy as np

Then you need to load an already trained face classifier. The Haar classifier is used here, which can be loaded through the CascadeClassifier class provided by OpenCV:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Then you need to load the image for face recognition and convert it into a grayscale image, because the calculation amount is smaller and the processing speed is faster under grayscale images. You can use the imread() function to load images, and the cvtColor() function to convert images to grayscale images.

img = cv2.imread('img.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Next, you can use thedetectMultiScale() function to detect faces in the picture. This function is used to detect multiple targets in a picture, so it is called a cascade detector. This function is relatively complex, and the parameters need to be set carefully. Common parameters are as follows:

  • gray: Input grayscale image data;
  • 1.2: Indicates the image scaling ratio.
  • 5: Indicates the minimum number of neighbors of the target, that is, at least 5 must be detected to detect a face.
faces = face_cascade.detectMultiScale(gray, 1.2, 5)

Finally, mark the detected face with a rectangular frame. Among them, (x,y) represents the coordinates of the upper left corner of the rectangular frame, w and h are the width and height of the rectangular frame.

for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

Finally use the imshow() function to display the final image, waitKey() the function waits for the user to press a key on the keyboard, destroyAllWindows()Function is used to close all open windows.

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

2 Dynamic gesture recognition example

Algorithm overview

Gesture recognition is an automatic recognition technology based on images and videos. It converts human hand movements into language and actions that can be recognized by machines, making it more humane and friendly. Gesture recognition technology is widely used in smart homes, autonomous driving and other fields, and has become an important part of people's lives.

Code

# 导入OpenCV和numpy库
import cv2
import numpy as np

# 常量定义
camera_width = 640
camera_height = 480
camera_fps = 30

# 手势分类器定义
gesture_cascade = cv2.CascadeClassifier('palm.xml')

# 打开摄像头
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)
camera.set(cv2.CAP_PROP_FPS, camera_fps)

# 循环检测
while True:
    # 读取当前帧
    ret, frame = camera.read()

    # 灰度转换
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # 手势检测
    gestures = gesture_cascade.detectMultiScale(gray, 1.3, 5)

    # 绘制矩形框
    for (x, y, w, h) in gestures:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)

    # 显示结果
    cv2.imshow('Gesture detection', frame)

    # 等待用户输入
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 关闭摄像头,并销毁所有窗口
camera.release()
cv2.destroyAllWindows()

Implementation instructions

First you need to import the required librariescv2The module is the interface of OpenCV under Python3.0, which is used for the collection and processing of video streams;numpy is a scientific computing toolkit, used here for array processing.

import cv2
import numpy as np

Then you need to turn on the camera and set its parameters. The way to read the camera is to use the VideoCapture() function. You can configure its parameters, including frame rate, resolution and other parameters, by calling the set() method of the camera object.

camera_width = 640
camera_height = 480
camera_fps = 30

camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)
camera.set(cv2.CAP_PROP_FPS, camera_fps)

Then a trained gesture classifier needs to be loaded. The palm.xml classifier is used here.

gesture_cascade = cv2.CascadeClassifier('palm.xml')

Next, in the while loop, the video frames in the camera can be continuously read. The current video frame is then converted into a grayscale image, which is used to reduce the amount of calculation and improve processing speed. Then use the detectMultiScale() function to detect the gesture in the current frame. This function is used to detect multiple objects in a picture, so it is called a cascade detector. Common parameters are explained as follows:

  • gray: Input grayscale image, this parameter needs to be set in the code;
  • 1.3: Indicates the image scaling ratio;
  • 5: Indicates the minimum number of neighbors of the target, that is, at least 5 must be detected to be considered a gesture.
ret, frame = camera.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gestures = gesture_cascade.detectMultiScale(gray, 1.3, 5)

Finally, mark the detected gesture with a rectangular frame. Among them, (x, y) represents the coordinates of the upper left corner of the rectangular frame, w and h are the width and height of the rectangular frame.

for (x, y, w, h) in gestures:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)

Finally outside the while loop close the camera and destroy all windows

camera.release()
cv2.destroyAllWindows()

3 Video surveillance and analysis examples

Algorithm overview

Video surveillance and analysis systems can automatically detect, track and analyze people and objects in the scene, such as moving object detection, face recognition, fireworks detection, etc.

Code

# 导入OpenCV, numpy和datetime库
import cv2
import numpy as np
import datetime

# 常量定义
camera_width = 640
camera_height = 480
camera_fps = 30

# 打开摄像头
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)
camera.set(cv2.CAP_PROP_FPS, camera_fps)

# 初始化运动检测器
foreground_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=50)

# 循环处理每一帧
while True:
    # 读取当前帧
    ret, frame = camera.read()

    # 转换图像为灰度图
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # 移除背景并二值化
    fgmask = foreground_detector.apply(gray)
    fgmask = cv2.threshold(fgmask, 200, 255, cv2.THRESH_BINARY)[1]

    # 膨胀并腐蚀处理
    kernel = np.ones((5, 5), np.uint8)
    fgmask = cv2.dilate(fgmask, kernel, iterations=2)
    fgmask = cv2.erode(fgmask, kernel, iterations=2)

    # 检测并绘制运动物体的边框
    contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for c in contours:
        if cv2.contourArea(c) < 500:
            continue
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    # 显示结果
    cv2.putText(frame, datetime.datetime.now().strftime('%A %d %B %Y %I:%M:%S%p'),
                (10, camera_height - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
    cv2.imshow('Video Surveillance', frame)

    # 等待用户输入
    if cv2.waitKey(1) == ord('q'):
        break

# 关闭摄像头,并销毁所有窗口
camera.release()
cv2.destroyAllWindows()

Implementation instructions

First you need to import the required libraries. The cv2 module introduced here is the interface of OpenCV under Python3.0, which is used for the collection and processing of video streams; numpy is the scientific computing toolkit, which is used here For array processing;datetime is a library used to support date and time tags for video frames on video streams.

import cv2
import numpy as np
import datetime

Then you need to turn on the camera and set its parameters. The way to read the camera is to use the VideoCapture() function. You can configure its parameters, including frame rate, resolution and other parameters, by calling the set() method of the camera object.

camera_width = 640
camera_height = 480
camera_fps = 30

camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)
camera.set(cv2.CAP_PROP_FPS, camera_fps)

The motion detector then needs to be initialized. The createBackgroundSubtractorMOG2() method was used in the code to create a background subtractor based on Mixtures of Gaussians (MOG2), and set a history of 100 frames and a change threshold of 50.

foreground_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=50)

Next, in the while loop, the video frames in the camera can be continuously read and multi-step processing is performed. The current video frame is first converted into a grayscale image to reduce the amount of calculation and increase the processing speed. Then, we use the apply() function to remove the background from the input grayscale image. The subtracted matrix is ​​then binarized.

ret, frame = camera.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

fgmask = foreground_detector.apply(gray)
fgmask = cv2.threshold(fgmask, 200, 255, cv2.THRESH_BINARY)[1]

Next, an opening operation is used to expand and erode the binary image to remove impurity points attached to the object.

kernel = np.ones((5, 5), np.uint8)
fgmask = cv2.dilate(fgmask, kernel, iterations=2)
fgmask = cv2.erode(fgmask, kernel, iterations=2)

Next use the findContours() function to find the connected domain in the binary image and draw the border. When drawing the border, we need to first determine whether the area meets the detection conditions.

contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
if cv2.contourArea(c) < 500:
        continue
    (x, y, w, h) = cv2.boundingRect(c)
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

Finally outside the while loop close the camera and destroy all windows.

camera.release()
cv2.destroyAllWindows()

We also added a datetime tagframe to show when the current video frame was processed.

cv2.putText(frame, datetime.datetime.now().strftime('%A %d %B %Y %I:%M:%S%p'),
            (10, camera_height - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

Guess you like

Origin blog.csdn.net/u010349629/article/details/130663640