Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize pedestrian counting based on background subtraction in OpenCV

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

OpenCV is based on background subtraction to achieve pedestrian counting. In order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Preface

Below, we will use OpenCV C++ to count the number of people in the video.

I. Image preprocessing

The original picture is shown in the picture. The requirement of this case is to count the number of people in the screen. The pedestrians walking in the picture can be regarded as the foreground, so we need to separate the foreground and the background. We can use the BackgroundSubtractorMOG2 Gaussian mixture model provided by OpenCV to segment pedestrians from the picture, and then extract contours to count the flow of people.

PtrMOG = createBackgroundSubtractorMOG2 (); MOG- > apply (frame, mask)

Use the above two lines of code to create a Gaussian mixture background extractor. Input the original image and return the background subtraction result. As shown in the image above. Next, we only need to do some simple operations on the above image, and then extract the outline to count the flow of people.

Threshold (mask, mask, 200,255, THRESH_BINARY); morphologyEx (mask, mask, MORPH_OPEN, kernel); dilate (mask, mask, kernel1)

By binarization, morphology and other operations, pedestrians can be separated as an independent individual. The effect is as shown in the picture.

Second, object count 1. Contour extraction

Carry on the contour detection of the above binary image, and then count the effective contour to complete the object count.

Vectorcontours; vectorEffectiveContours; findContours (mask, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); for (int I = 0; I

< contours.size(); i++) { double area = contourArea(contours[i]); if (area >

{EffectiveContours.push_back (contours [I]);}} 2. The effect shows char text [10]; for (int I = 0; I < EffectiveContours.size (); iTunes +) {RotatedRect rect = minAreaRect (effective contours [I]); Rect box = rect.boundingRect (); rectangle (frame, Rect (box.x, box.y, box.width, box.height), Scalar (0,255,0), 2) Sprintf_s (text, "% s% d", "Current:", EffectiveContours.size ()); putText (frame, text, Point (10,30), FONT_HERSHEY_SIMPLEX, 1, Scalar (0255,0), 2);}

The final effect is shown in the figure.

Source code # include#includeusing namespace std;using namespace cv;int main () {VideoCapture capture; capture.open ("1.avi"); if (! capture.isOpened ()) {cout 300) {EffectiveContours.push_back (contours [I]) }} char text [10]; for (int I = 0; I < EffectiveContours.size ()) {RotatedRect rect = minAreaRect (EffectiveContours [I]); Rect box = rect.boundingRect () Rectangle (frame, Rect (box.x, box.y, box.width, box.height), Scalar (0,255,0), 2); sprintf_s (text, "% s% d", "Current:", EffectiveContours.size ()); putText (frame, text, Point (10,30), FONT_HERSHEY_SIMPLEX, 1, Scalar (0,255,0), 2) } imshow ("frame", frame); imshow ("mask", mask); char key = waitKey (10); if (key = = 27) {break;}} destroyAllWindows (); capture.release () System ("pause"); return 0;} this is the answer to the question about how to realize pedestrian counting based on background subtraction in OpenCV. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report