64,654
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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);
};
#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;
}
#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);
#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);//跟踪的时候以椭圆为代表目标
}