当前位置:网站首页>Opencv tutorial 03: how to track an object in a video
Opencv tutorial 03: how to track an object in a video
2022-07-19 12:21:00 【somenzz】
Every frame of the video is a picture , Track an object in the video , Break it down , In fact, it is to find the object in the picture of each frame .
Since you want to find that object , You need to define the target object first , Since it's a picture , Then there are colors , First understand the commonly used 3 Color model .
RGB.RGB It is the three primary color light mode , also called RGB Color model or red, green and blue color model , It's an additive model , Will be red 、 green 、 The light of the three primary colors of blue is added in different proportions , To synthesize light of various colors . Every pixel has 3 A value represents the color , This is the most common color model .OpenCV The order in is BGR.
grayscale . Simple understanding , It's black and white , Each image Pixel point There can only be A value Color , The range of pixel values is [0~255], Most of the existing mature analysis algorithms are based on gray images .
HSV. The parameters of color in this model are hue (H)、 saturation (S) And lightness (V).HSV It is an intuitive color model for users . We can start with a pure color , That is, specify the hue H, And let V=S=1, Then we can add black and white to it to get the color we need .
The above three models , Use HSV It is more convenient to define a target object .
OpenCV It can be easily from BGR Convert to grayscale , from BGR Convert to HSV:
import cv2 as cv
cv.cvtColor(input_image, cv.COLOR_BGR2GRAY)
cv.cvtColor(input_image, cv.COLOR_BGR2HSV)Now we know how to BGR Image to HSV, We can use it to extract color objects . stay HSV in , Indicates that the color ratio is BGR Easier in color space .
Next , We will track blue objects in the video .
step :
Capture every frame of the video
from BGR Convert to HSV Color space
We will HSV The image threshold is set to the blue range
Extract blue objects individually , We can do whatever we want on this image .
The following is the detailed code :
import cv2 as cv
import numpy as np
# Read video from the default camera
cap = cv.VideoCapture(0)
while True:
# Get every frame of the video
_, frame = cap.read()
# BGR Convert to HSV
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
# use HSV Define the color range of the target object .
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
# Generate a HSV The mask only gets blue objects .
mask = cv.inRange(hsv, lower_blue, upper_blue)
# application
res = cv.bitwise_and(frame,frame, mask= mask)
cv.imshow('frame',frame)
cv.imshow('mask',mask)
cv.imshow('res',res)
k = cv.waitKey(5) & 0xFF
if k == 27:
break
cv.destroyAllWindows() The renderings are as follows , You can see a little noise , There are technologies behind it that can eliminate .
Last
If help , Please look at the support .

边栏推荐
- 【C# wpf】个人网盘练习项目总结
- C language drawing example - palette
- PPPoE dial up
- Two misunderstandings of digital transformation
- LeetCode_ 77_ combination
- Flink
- 2022-07-07:Spire. Office 7.7.2 for net debuted
- Core base station_ The error "no gateways configured" is reported when starting the CPA file
- Softmax和Cross-entropy是什么关系?
- 电路故障的分析简略
猜你喜欢

es安装ik分词器
MIHA tour 2023 autumn recruitment officially begins ~ early approval has the opportunity to avoid written examination!

Genesis and bluerun ventures have in-depth exchanges

Mysql-1366 - Incorrect string value: ‘\xE5\xBC\xA0\xE4\xB8\x89‘ for column ‘userName‘ at row 1

李宏毅《机器学习》|1. Introduction of this course(机器学习介绍)

Project construction depends on people, and success depends on people!

Mysql学习笔记-分页-表的创建-数据类型

说说 Redis 缓存穿透场景与相应的解决方法

Flink

编辑技巧篇
随机推荐
WebGPU 会成为 WebGL 的杀手吗?
Leetcode 150. Evaluation of inverse Polish expression
WAV和PCM的关系和区别
Scrapy关键词 爬虫的简单实现(以新华网和人民网为例)
C语言绘图示例-调色板
C#从入门到精通之第一篇: C#概述与入门
[MySQL] add, delete, check and modify MySQL (Advanced)
HCIP (7)
Simple implementation of scrapy keyword crawler (take Xinhuanet and people's network as examples)
MySQL learning notes - paging - table creation - data type
C language drawing example - flower pattern
微机原理与技术接口 实验五 基本IO操作温度控制实验
Flink
[shutter] dart: some features that cannot be ignored
李宏毅《机器学习》|1. Introduction of this course(机器学习介绍)
hicp第三天
SwiftUI Swift 中的数据持久性,保存数据的不同方法
Nature | the carbon sequestration rate of groundwater is similar to that of oligotrophic marine system
一个技巧;教你轻松下载抖音直播视频,抖音直播视频下载新方案!
The underlying principle of file operation (inode and hard and soft links, time attributes of files)