我根据opencv的camshift demo改写了一下,分成几个头文件源文件,程序跟踪不了,能帮忙看看哪里出错了吗

u010947001 2015-05-06 10:15:52
main.cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include "track_utils.h"
#include "camshift.h"


using namespace cv;
using namespace std;


//Global variables
Rect box;
bool drawing_box = false;
bool gotBB = false;
bool isfirst = true;

//draw box
void mouseHandler(int event, int x, int y, int flags, void *param){
switch (event){
case CV_EVENT_MOUSEMOVE:
if (drawing_box){
box.width = x - box.x;
box.height = y - box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = cv::Rect(x, y, 0, 0);
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if (box.width < 0){
box.x += box.width;
box.width *= -1;
}
if (box.height < 0){
box.y += box.height;
box.height *= -1;
}
gotBB = true;
break;
}
}




int main() {

VideoCapture capture;
capture.open(0);
// check if video successfully opened
if (!capture.isOpened()) {
cout << "couldn't open video file" << endl;
return 1;
}



Mat frame, processFrame, hsv, hue, hist, mask;
int vmin = 10, vmax = 256, smin = 30;
RotatedRect trackbox;
camshift camTracker;


namedWindow("tracking");
createTrackbar("Vmin", "tracking", &vmin, 256, 0);//createTrackbar函数的功能是在对应的窗口创建滑动条,滑动条Vmin,vmin表示滑动条的值,最大为256
createTrackbar("Vmax", "tracking", &vmax, 256, 0);//最后一个参数为0代表没有调用滑动拖动的响应函数
createTrackbar("Smin", "tracking", &smin, 256, 0);//vmin,vmax,smin初始值分别为10,256,30

setMouseCallback("tracking", mouseHandler, NULL); //call mouse


capture >> frame;

while (!gotBB)
{

imshow("tracking", frame);

if (waitKey(30) == 'q')
return 1;
}
setMouseCallback("tracking", NULL, NULL);

while (true)
{
if (frame.empty())
return -1;
frame.copyTo(processFrame);
cvtColor(processFrame, hsv, CV_BGR2HSV);

inRange(hsv, Scalar(0, smin, MIN(vmin, vmax)),
Scalar(180, 256, MAX(vmin, vmax)), mask);
int ch[] = { 0, 0 };
hue.create(hsv.size(), hsv.depth());
mixChannels(&hsv, 1, &hue, 1, ch, 1);//将hsv第一个通道(也就是色调)的数复制到hue中,0索引数组
if (isfirst) {

hist = camTracker.getHistogram(hue, mask, box);
isfirst = false;
}
capture >> frame;





trackbox = camTracker.process(hue, hist, mask, box);
// draw ellipse

drawBox(processFrame, trackbox);

imshow("tracking", processFrame);


if (waitKey(30) >= 0)
break;
}

waitKey();



return 0;
}


camshift.h
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;

class camshift{

private:


int hsize = 16;
float hranges[2];//hranges在后面的计算直方图函数中要用到
const float* phranges = hranges;
Mat backproj;

public:


camshift(){

hranges[0] = 0.0;
hranges[1] = 180.0;

};

Mat getHistogram(Mat &hue, Mat &mask, Rect selection);
RotatedRect process(Mat &hue, Mat &hist, Mat &mask, Rect selection);

};


camshift.cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/tracking.hpp>
#include "camshift.h"
#include <iostream>


using namespace cv;
using namespace std;



Mat camshift::getHistogram(Mat &hue, Mat &mask, Rect selection) {

Mat hist;
Mat roi(hue, selection), maskroi(mask, selection);
calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);//将roi的0通道计算直方图并通过mask放入hist中,hsize为每一维直方图的大小
normalize(hist, hist, 0, 255, CV_MINMAX);//将hist矩阵进行数组范围归一化,都归一化到0~255
return hist;
}


RotatedRect camshift::process(Mat &hue, Mat &hist, Mat &mask, Rect selection) {

RotatedRect trackbox;
calcBackProject(&hue, 1, 0, hist, backproj, &phranges);//计算直方图的反向投影,计算hue图像0通道直方图hist的反向投影,并让入backproj中
backproj &= mask;
trackbox = CamShift(backproj, selection, TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1));
return trackbox;
}



track_utils.h
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#pragma once

void drawBox(cv::Mat& image, cv::RotatedRect box, cv::Scalar color = cv::Scalar(0, 0, 255), int thick = 3, int line_type = CV_AA);



track_utils.cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "track_utils.h"

using namespace cv;


void drawBox(Mat& image, RotatedRect box, Scalar color, int thick, int line_type){
//rectangle(image, Point(box.x, box.y), Point(box.x + box.width, box.y + box.height), color, thick);
ellipse(image, box, color, thick, line_type);//跟踪的时候以椭圆为代表目标
}


camshift demo地址:http://www.cnblogs.com/tornadomeet/archive/2012/03/15/2398769.html
...全文
175 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 松下电工数字压力传感器用户手册详细介绍了DP-100系列数字压力传感器,涵盖其技术参数、操作方法及适用场景等,适用于各类需要精准压力测量的工业环境。 双屏显示:主屏与输出动作同步,可同时显示当前值和基准值,便于实时监控与调整。显示屏为12段字母数字显示,数字清晰易读。 三色指示:屏幕颜色随传感器状态变化(红、绿、橙),便于快速判断工作状态。 紧凑结构:尺寸仅□30mm,适合空间狭窄的安装环境。 多种操作模式:提供RUN模式(日常操作)、菜单设定模式(深入设置如输出模式切换)及PRO模式(高级功能如应差调整、复制设定)。 安全认证:DP-101(A)/102(A)型号通过特定认证,确保产品安全可靠。 复制功能:可通过数据通信将主传感器设定内容复制到其他传感器,减少人工设定错误,节省时间。 高性能传感:具备高精度,分辨率1/2,000,反应时间2.5ms(最长5,000ms可调),温度特性±0.5%F.S.,重复精度±0.1%F.S. 电子元件吸附检测:监测吸盘是否成功吸附电子元件。 总压力监测:测量管道或容器内的压力水平。 空气泄漏检测:通过压力变化检测泄漏情况。 DP-101□:适用于低压环境(-100kPa至100kPa)。 DP-102□:适用于高压环境(0kPa至1MPa)。 订购时需根据实际需求选择合适型号,考虑传感器的适用范围和工作条件。手册提供详细订购流程及注意事项,包括相关认证信息(如韩国S标志)。 复制功能:通过数据通信将主传感器设定复制到其他传感器,支持多种设定模式,避免设定错误,节省时间。 操作模式:RUN模式用于日常监控,菜单设定模式用于深入设置,PRO模式提供高级功能。 使用前需仔细阅读手册,了解各功能使用方法。遵循安全指南,正确安装和使用传感器,避免损坏。对于

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧