运动目标跟踪与检测的源代码(CAMSHIFT 算法)

采用 CAMSHIFT 算法快速跟踪和检测运动目标的 C/C++ 源代码,OPENCV BETA 4.0 版本在其 SAMPLE 中给出了这个例子。算法的简单描述如下(英文):

This application demonstrates a fast, simple color tracking algorithm that can be used to track faces, hands . The CAMSHIFT algorithm is a modification of the Meanshift algorithm which is a robust statistical method of finding the mode (top) of a probability distribution. Both CAMSHIFT and Meanshift algorithms exist in the library. While it is a very fast and simple method of tracking, because CAMSHIFT tracks the center and size of the probability distribution of an object, it is only as good as the probability distribution that you produce for the object. Typically the probability distribution is derived from color via a histogram, although it could be produced from correlation, recognition scores or bolstered by frame differencing or motion detection schemes, or joint probabilities of different colors/motions etc.

In this application, we use only the most simplistic approach: A 1-D Hue histogram is sampled from the object in an HSV color space version of the image. To produce the probability image to track, histogram "back projection" (we replace image pixels by their histogram hue value) is used.

算法的详细情况,请看论文:

http://www.assuredigit.com/incoming/camshift.pdf

关于OPENCV B4.0 库的使用方法以及相关问题,请查阅下面的相关文章:

http://forum.assuredigit.com/display_topic_threads.asp?ForumID=11&TopicID=3471

运行文件下载:

http://www.assuredigit.com/product_tech/Demo_Download_files/camshiftdemo.exe

该运行文件在VC6.0环境下编译通过,是一个 stand-alone 运行程序,不需要OPENCV的DLL库支持。在运行之前,请先连接好USB接口的摄像头。然后可以用鼠标选定欲跟踪目标。

=====

#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#endif

IplImage *image = 0, *hsv =

  • 0
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 31
    评论
树莓派可以通过连接摄像头实现实时跟踪目标,并通过控制舵机来跟踪目标。其中,Camshift算法是一种基于颜色直方图的目标跟踪算法,可以通过对目标的颜色信息进行建模来实现目标的跟踪。 以下是基于树莓派和摄像头实现目标跟踪的大致步骤: 1. 连接摄像头并打开摄像头。 2. 检测摄像头中的目标,并计算目标的颜色直方图。 3. 使用Camshift算法对目标进行跟踪,通过计算目标的颜色直方图与当前帧的颜色直方图的相似度来确定目标的位置。 4. 根据目标位置的变化,通过控制舵机来实现目标的跟踪。 具体实现过程可以参考以下步骤: 1. 首先需要安装OpenCV库,用于图像处理和目标跟踪。可以通过以下命令进行安装: ``` sudo apt-get install python-opencv ``` 2. 接下来需要连接摄像头并打开摄像头。可以通过以下代码实现: ```python import cv2 # 打开摄像头 cap = cv2.VideoCapture(0) while True: # 读取帧 ret, frame = cap.read() # 显示帧 cv2.imshow('frame', frame) # 检测键盘输入,按'q'退出程序 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头并关闭所有窗口 cap.release() cv2.destroyAllWindows() ``` 3. 接下来需要检测目标并计算目标的颜色直方图。可以通过以下代码实现: ```python import cv2 import numpy as np # 打开摄像头 cap = cv2.VideoCapture(0) # 定义目标范围 lower_range = np.array([0, 70, 50]) upper_range = np.array([10, 255, 255]) # 初始化Camshift算法 roi_hist = None track_window = None term_crit = None while True: # 读取帧 ret, frame = cap.read() # 如果没有目标范围,则手动选择目标 if roi_hist is None: # 显示帧并等待用户选择目标 cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('s'): # 选择目标并计算颜色直方图 roi = cv2.selectROI(frame, False) hsv_roi = cv2.cvtColor(frame[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])], cv2.COLOR_BGR2HSV) roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180]) cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX) track_window = roi term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) else: continue # 将帧转换为HSV格式 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 计算反向投影图像 dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) # 使用Camshift算法进行目标跟踪 ret, track_window = cv2.CamShift(dst, track_window, term_crit) # 在帧上绘制跟踪框和中心点 pts = cv2.boxPoints(ret) pts = np.int0(pts) center = np.int0(ret[0]) cv2.polylines(frame, [pts], True, (0, 255, 0), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) # 显示帧 cv2.imshow('frame', frame) # 检测键盘输入,按'q'退出程序 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头并关闭所有窗口 cap.release() cv2.destroyAllWindows() ``` 4. 最后,根据目标位置的变化,通过控制舵机来实现目标的跟踪。具体实现需要根据舵机型号和控制方式进行调整。可以参考以下代码实现: ```python import RPi.GPIO as GPIO import time # 定义舵机引脚号 servo_pin = 18 # 初始化GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(servo_pin, GPIO.OUT) # 定义舵机控制函数 def set_servo_angle(angle): duty = angle / 18 + 2.5 GPIO.output(servo_pin, True) p = GPIO.PWM(servo_pin, 50) p.start(0) p.ChangeDutyCycle(duty) time.sleep(0.3) p.stop() GPIO.output(servo_pin, False) # 打开摄像头 cap = cv2.VideoCapture(0) # 定义目标范围 lower_range = np.array([0, 70, 50]) upper_range = np.array([10, 255, 255]) # 初始化Camshift算法 roi_hist = None track_window = None term_crit = None # 初始化舵机角度 servo_angle = 90 while True: # 读取帧 ret, frame = cap.read() # 如果没有目标范围,则手动选择目标 if roi_hist is None: # 显示帧并等待用户选择目标 cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('s'): # 选择目标并计算颜色直方图 roi = cv2.selectROI(frame, False) hsv_roi = cv2.cvtColor(frame[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])], cv2.COLOR_BGR2HSV) roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180]) cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX) track_window = roi term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) else: continue # 将帧转换为HSV格式 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 计算反向投影图像 dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) # 使用Camshift算法进行目标跟踪 ret, track_window = cv2.CamShift(dst, track_window, term_crit) # 在帧上绘制跟踪框和中心点 pts = cv2.boxPoints(ret) pts = np.int0(pts) center = np.int0(ret[0]) cv2.polylines(frame, [pts], True, (0, 255, 0), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) # 计算舵机角度 servo_angle = int(servo_angle + (center[0]-frame.shape[1]/2) / (frame.shape[1]/2) * 30) servo_angle = max(min(servo_angle, 150), 30) # 控制舵机 set_servo_angle(servo_angle) # 显示帧 cv2.imshow('frame', frame) # 检测键盘输入,按'q'退出程序 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头、关闭所有窗口并清理GPIO cap.release() cv2.destroyAllWindows() GPIO.cleanup() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值