我根据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
...全文
169 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,648

社区成员

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

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