当前位置:网站首页>Object detection with OpenCV
Object detection with OpenCV
2022-06-28 05:01:00 【Keep_ Trying_ Go】
List of articles
- 1. Pre knowledge points
- (1)Opencv Global binarization and local binarization
- (2)Opencv Medium erode and dilate
- (3)Opencv Medium findContours() and drawContours()
- (4)Opencv Draw a straight line in , rectangular , round , The ellipse , polygon ( Include polygon fill ), Draw text
- (5)Opencv Calculate the perimeter and area of the contour in
- (6)Opencv Basic knowledge points in
- 2. Code combat
1. Pre knowledge points
(1)Opencv Global binarization and local binarization
https://mydreamambitious.blog.csdn.net/article/details/125249121
(2)Opencv Medium erode and dilate
https://mydreamambitious.blog.csdn.net/article/details/125265431
(3)Opencv Medium findContours() and drawContours()
https://mydreamambitious.blog.csdn.net/article/details/125339604
(4)Opencv Draw a straight line in , rectangular , round , The ellipse , polygon ( Include polygon fill ), Draw text
https://mydreamambitious.blog.csdn.net/article/details/125392536
(5)Opencv Calculate the perimeter and area of the contour in
https://mydreamambitious.blog.csdn.net/article/details/125340081
(6)Opencv Basic knowledge points in
https://mydreamambitious.blog.csdn.net/article/details/125351256
2. Code combat
(1) First, the read frame is scaled , Extracting regions of interest and binarization
frame=cv2.resize(src=frame,dsize=(750,500))
# Extract the region of interest
roi=frame[200:400,450:700]
mask=bgsegment.apply(roi)
# Remove the video mask Medium gray shadow
OK,mask=cv2.threshold(src=mask,thresh=245,maxval=255,type=cv2.THRESH_BINARY)
(2) Find outline and draw rectangle
# Find the outline
binary,contours,hierarchy=cv2.findContours(image=mask,mode=cv2.RETR_TREE,method=cv2.CHAIN_APPROX_SIMPLE)
# Record the position of the car
box=[]
# Perform traversal filtering
for step,cont in enumerate(contours):
# Calculated area
area=cv2.contourArea(cont)
if area>=150:
# cv2.drawContours(image=roi,contours=[cont],contourIdx=-1,
# color=(0,255,0),thickness=2)
# Get the coordinates, height and width of the object in the video
(x,y,w,h)=cv2.boundingRect(cont)
# # Drawing a matrix ( Frame column object )
cv2.rectangle(img=roi,pt1=(x,y),pt2=(x+w,y+h),color=(0,255,0),thickness=2)
(3) Draw the number of frames read by the video
# Show FPS
endtime = time.time()
FPS = 1 / (endtime - startime)
cv2.putText(img=frame, text='FPS: ' + str(int(FPS)), org=(30, 450),
fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1.0,
color=(0, 255, 0), thickness=2)
(4) The overall code
import os
import cv2
import time
import numpy as np
# frame=np.zeros(shape=(450,450,3),dtype=np.uint8)
cap=cv2.VideoCapture('Video/DJ_1.mp4')
# Remove the background
bgsegment=cv2.createBackgroundSubtractorMOG2(history=100,varThreshold=40)
# Instance tracker
# trackers=cv2.MultiTracker_create()
# # The instance tracking algorithm used
# track_algorithm=cv2.TrackerKCF_create
# Track the overall situation of the car ID
class_id=0
while cap.isOpened():
startime=time.time()
OK,frame=cap.read()
if OK==False:
break
frame=cv2.resize(src=frame,dsize=(750,500))
# Extract the region of interest
roi=frame[200:400,450:700]
mask=bgsegment.apply(roi)
# Remove the video mask Medium gray shadow
OK,mask=cv2.threshold(src=mask,thresh=245,maxval=255,type=cv2.THRESH_BINARY)
# Do the open operation , take mask Remove all small objects in the
# kernel=cv2.getStructuringElement(shape=cv2.MORPH_RECT,ksize=(3,3))
# mask=cv2.morphologyEx(src=mask,op=cv2.MORPH_OPEN,kernel=kernel,iterations=1)
# Find the outline
binary,contours,hierarchy=cv2.findContours(image=mask,mode=cv2.RETR_TREE,method=cv2.CHAIN_APPROX_SIMPLE)
# Record the position of the car
box=[]
# Perform traversal filtering
for step,cont in enumerate(contours):
# Calculated area
area=cv2.contourArea(cont)
if area>=150:
# cv2.drawContours(image=roi,contours=[cont],contourIdx=-1,
# color=(0,255,0),thickness=2)
# Get the coordinates, height and width of the object in the video
(x,y,w,h)=cv2.boundingRect(cont)
# # Drawing a matrix ( Frame column object )
cv2.rectangle(img=roi,pt1=(x,y),pt2=(x+w,y+h),color=(0,255,0),thickness=2)
# Show FPS
endtime = time.time()
FPS = 1 / (endtime - startime)
cv2.putText(img=frame, text='FPS: ' + str(int(FPS)), org=(30, 450),
fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1.0,
color=(0, 255, 0), thickness=2)
cv2.imshow('image', frame)
cv2.imshow('roi', roi)
cv2.imshow('mask', mask)
key=cv2.waitKey(30)
if key==27:
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
Indicate the : When the reader is extracting the region of interest , You can use your own video to , Because I extracted it according to the moving position of the car in my video .
边栏推荐
- 摄像头基础知识
- Sorting out some topics of modern exchange principle MOOC
- 代码理解:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
- CI & CD must be known!
- PHP code wechat, official account and enterprise wechat send emoticons [u+1f449]
- 氨基染料研究:Lumiprobe FAM 胺,6-异构体
- 二级造价工程师证书含金量到底有多高?看这些就知道了
- [early knowledge of activities] list of recent activities of livevideostack
- 开关电源电压型与电流型控制
- 如何学习可编程逻辑控制器(PLC)?
猜你喜欢
![[noip2002 popularization group] cross the river pawn](/img/6c/31fa210e08c7fd07691a1c5320154e.png)
[noip2002 popularization group] cross the river pawn

2022高处安装、维护、拆除考试题及答案
![[early knowledge of activities] list of recent activities of livevideostack](/img/aa/8b14f9863cd675a7be06a0c3855a93.png)
[early knowledge of activities] list of recent activities of livevideostack

100+ data science interview questions and answers Summary - machine learning and deep learning

交流电和直流电的区别是什么?

CPG 固体支持物研究:Lumiprobe通用 CPG II 型

二级造价工程师考试还没完?还有资格审核规定!

The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022

On the necessity of building a video surveillance convergence platform and its scenario application

2022新版nft源码中国元宇宙数字藏品艺术品交易平台源码
随机推荐
Feign remote call fallback callback failed, no effect
!‘cat‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
PCR/qPCR研究:Lumiprobe丨dsGreen 用于实时 PCR
IP数据报的发送和转发过程
2022年安全员-A证考试题库及模拟考试
羧酸研究:Lumiprobe 磺基花青7二羧酸
Flexible IP network test tool -- x-launch
二级造价工程师证书含金量到底有多高?看这些就知道了
quartus 复制IP核
使用class toplevel的messagebox时,窗口弹出问题。
CUPTI error: CUPTI could not be loaded or symbol could not be found.
Precautions for using C language global variables (global variables in C and H files, static global variables)
Find an SQL that can judge the data in the table and only fill in the SQL that is not overwritten
[CSP-J2020] 优秀的拆分
metaRTC5.0编程之p2p网络穿透(stun)指南
BioVendor sRAGE蛋白解决方案
C语言中函数是什么?编程中的函数与数学中的函数区别?理解编程语言中的函数
mysql修改密码报错需要怎么做
论文详读:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
2022新版nft源码中国元宇宙数字藏品艺术品交易平台源码