In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces OpenCV how to achieve object tracking, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Object tracking
Object tracking is the process of locating moving objects in video. You can consider an example of a football match.
You have a real-time feed of an ongoing game, and your task is to track the position of the ball at any time. For ordinary people, the task seems simple, but even the smartest machine is too complicated.
As you may know, computers can only understand numbers. It doesn't know what an image is, but it knows the pixel values associated with the image. Two images that look exactly the same to the human eye may not be the same to the computer, because even small changes in pixels can lead to differences between images. Because object tracking is regarded as one of the most complex tasks in computer vision. Although it is complicated, it is not impossible.
Object tracking can be performed using machine learning and deep learning-based methods.
On the one hand, the deep learning method provides better results on complex tasks, and it is very general, and requires a lot of training data. The ML-based approach is very simple, but not universal.
In this article, we will use a ML-based approach and various computer vision techniques that we will discuss later in this article.
This technology is widely used in surveillance, security, traffic monitoring, robot vision, video communication and other fields. In addition, there are several use cases of object tracking, such as crowd counting, self-driving cars, face detection and so on. Can you think of more examples where object tracking can be used in daily life?
Because there are so many applications in real life, continuous research is under way in this field to achieve higher accuracy and make the model more robust.
For this article, we will use this video (https://drive.google.com/file/d/1N6NcFpveLQLc_DnFjfuMMvfuCMTAJRFm/view?usp=sharing).
As you will see, there is a red ball moving in the maze. Our task is to detect the position of the ball and find its centroid. You can also see loud noise (crowds) in the background, making the task more challenging.
1. First, we import the required libraries that we will use.
Import numpy as npimport cv2
two。 We will define a function to resize the images so that they are large enough to fit our screen. This step is completely optional and can be skipped at will.
Def resize (img): return cv2.resize (img, (512512)) # arg1- input image, arg- output_width, output_height
3. As you may know, video is made up of frames. A frame is just one of many still images that make up the whole dynamic picture. The next step is to read these frames using the VideoCapture () function in OpenCV and use the while loop so that we can see the frames moving.
You can adjust the speed of the video using cv2.waitKey (x), which pauses the screen for x milliseconds.
Cap=cv2.VideoCapture (vid_file_path) ret,frame=cap.read () while ret==True: ret,frame=cap.read () cv2.imshow ("frame", resize (frame)) key=cv2.waitKey (1) if key==ord ('q'): breakcv2.waitKey (0) cv2.destroyAllWindows ()
4. It is time to perform some thresholds and preprocessing. OpenCV reads images in BGR format, so we will convert the color space from BGR to HSV.
Why HSV and not BGR or any other format?
We use the HSV color format because it is more sensitive to small changes in external lighting. Therefore, it will provide a more accurate mask for better results.
After converting the color space, what we need to do is filter out the red channel and create a mask box.
Red channels in hsv format appear in the range of [0230170] to [255255220].
Cap=cv2.VideoCapture (vid_file_path) ret,frame=cap.read () l_b=np.array ([0230170]) # lower hsv bound for redu_b=np.array ([255255220]) # upper hsv bound to red while ret==True: ret,frame=cap.read () hsv=cv2.cvtColor (frame,cv2.COLOR_BGR2HSV) mask=cv2.inRange (hsv,l_b,u_b) cv2.imshow ("frame", resize (frame)) cv2.imshow ("mask") Mask) key=cv2.waitKey (1) if key==ord ('q'): breakcv2.waitKey (0) cv2.destroyAllWindows ()
(this figure has been resized)
5. So far, we have created a masked image of the frame, and we have filtered out most of the noise. Then there is the boundary of getting the ball. To do this, we will use the concept of contour detection.
The outline just surrounds the boundary of our ball. Fortunately, we don't have to find these boundaries ourselves, because OpenCV allows us to use them for our purposes as a function findContours (). It requires a mask image and returns an array of contours.
For more information about contours, visit https://docs.opencv.org/4.5.2/d4/d73/tutorial_py_contours_begin.html
Ideally, in our example, the value of the outline should be 1, because we only have one ball, but because some people are wearing red hats, we will get more than one. Can you think of some ways to further reduce the noise?
To solve this problem, we will use another function in OpenCV, cv2.contourArea (). We know that in the mask image, the area of the ball is the largest, and so is its outline. Therefore, we will get the outline with the largest area.
We have the contours of the ball, which we can draw directly using the cv2.drawContours () function. But for detection tasks, what we usually do is to use a tightly bound rectangle to indicate that the object has been detected.
To do this, we will use the cv2.boundingRect () function. This function will return the coordinates of the rectangle, and the cv2.rectangle () function will draw the rectangle for us.
Cap=cv2.VideoCapture (vid_file_path) ret,frame=cap.read () l_b=np.array ([0230170]) # lower hsv bound for redu_b=np.array ([255255220]) # upper hsv bound to red while ret==True: ret,frame=cap.read () hsv=cv2.cvtColor (frame,cv2.COLOR_BGR2HSV) mask=cv2.inRange (hsv,l_b,u_b) contours,_= cv2.findContours (mask,cv2.RETR_TREE) Cv2.CHAIN_APPROX_SIMPLE) max_contour= contours [0] for contour in contours: if cv2.contourArea (contour) > cv2.contourArea (max_contour): max_contour=contour contour=max_contour approx=cv2.approxPolyDP (contour, 0.01*cv2.arcLength (contour,True), True) 4) cv2.imshow ("frame", resize (frame)) cv2.imshow ("mask", mask)
(this figure has been resized)
6. In addition, what we can do is to detect the centroid of the ball at the same time. To do this, we will use cv2.moments. Cv2.moments calculates the weighted average sum of the pixel intensity in the contour, so it allows you to obtain some more useful information from the speckle, such as its radius, centroid, etc.
Be sure to convert the image to binary format before using this function. You can learn more about moments here: https://docs.opencv.org/3.4/d0/d49/tutorial_moments.html.
Cap=cv2.VideoCapture (vid_file_path) ret,frame=cap.read () l_b=np.array ([0230170]) # lower hsv bound for redu_b=np.array ([255255220]) # upper hsv bound to red while ret==True: ret,frame=cap.read () hsv=cv2.cvtColor (frame,cv2.COLOR_BGR2HSV) mask=cv2.inRange (hsv,l_b,u_b) contours,_= cv2.findContours (mask,cv2.RETR_TREE) Cv2.CHAIN_APPROX_SIMPLE) max_contour = contours [0] for contour in contours: if cv2.contourArea (contour) > cv2.contourArea (max_contour): max_contour = contour approx=cv2.approxPolyDP (contour, 0.01*cv2.arcLength (contour,True), True) (0pr 25510), 4) M=cv2.moments (contour)
Thank you for reading this article carefully. I hope the article "how to achieve object tracking in OpenCV" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.