Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How C++ OpenCV implements object tracking on source video based on color segmentation

Shulou Source: shulou.com Published: 2022-06-01 23:55:43 09月30日 Update

What this article shares with you is about how C++ OpenCV realizes object tracking on source videos based on color segmentation. The editor thinks it is quite practical, so I share it with you for study. I hope you can get something after reading this article.

Realization idea

Color segmentation of source video with InRange

Perform a morphological operation on the segmented image (open operation to remove drying)

Then expand the N flash (N is an integer and need to be grasped by yourself)

Find the outline of the expanded shape

Generate the circumscribed rectangle of the outline

Draw the circumscribed rectangle on the source drawing

Code implementation

Let's use the previous project opencv--video2, follow the configuration properties (VS2017 configure OpenCV common properties), and then add a definition of a rectangle based on the original, as shown in the red box below:

Color segmentation of source video with InRange

Open the segmented image (corrode first and then expand)

Perform four more expansion operations, the effect of this step is because the department blue in the video will be thought to be white due to reflective problems, so it will become several color blocks when the color is extracted. In this way, we fuse the scattered color blocks together through the expansion operation, and the specific number of expansion can be tested by ourselves. I filled in 4 directly. The effect is not bad.

Obtain the minimum circumscribed rectangle according to the expanded switch

Above we use a function of DealRect. Let's focus on this function. First, we define the function of DealRect at the top of the file.

Then we will write the implementation of DealRect.

Finally, we draw an outer rectangle on the source image and display the image.

In this way, the whole effect is completed, and the result is the video display at the beginning of the article. Here is the screenshot in the video.

The above is how C++ OpenCV realizes object tracking on the source video based on color segmentation. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Videos colors rectangles functions images effects objects images that is attributes shapes articles more knowledge articles color blocks contours source images generation configuration Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information MariaDB MySQL NVidia