跟踪对象 meashift
long frameTotal = capture.get(CV_CAP_PROP_FRAME_COUNT);
if (!capture.isOpened()) {
return;
}
capture.set(CV_CAP_PROP_POS_FRAMES, 30);
double frameRate = capture.get(CV_CAP_PROP_FPS);
bool isLoopFirst = true;
//视频中要跟踪的区域
cv::Rect rectTarget(310, 494, 47, 52);
Mat roi,hsv_roi,mask;
MatND roi_hist;
TermCriteria term_crit(TermCriteria::EPS|TermCriteria::COUNT,10,1);
vector<int> imagesNum = {1};
int histSize = 255;
float ranges[] = { 0, 180 };
const float* histRange = { ranges };
int channels[] = {0,1,2};
while (true) {
if (!capture.read(frame)) {
break;
}
Mat M = getRotationMatrix2D(Point2f(frame.cols/2,frame.rows/2),90,1);
warpAffine(frame,frame,M,frame.size());
if(isLoopFirst){
isLoopFirst = false;
frame(rectTarget).copyTo(roi);
cvtColor(frame, hsv_roi, CV_BGR2HSV);
vector<float> lowPass = {0,60,32};
vector<float> highPass = {180,255,255};
inRange(hsv_roi, lowPass, highPass, mask);
calcHist(&hsv_roi, 1, channels, mask, roi_hist, 1, &histSize, &histRange);
normalize(roi_hist, roi_hist, 255, 0, NORM_MINMAX);
}else{
Mat hsv;
cvtColor(frame, hsv, CV_BGR2HSV);
Mat dst;
calcBackProject(&hsv,1, channels, roi_hist, dst, &histRange);
RotatedRect rr = CamShift(dst, rectTarget, term_crit);
Mat pts2f,ptsCov;
boxPoints(rr, pts2f);
pts2f.assignTo(ptsCov,CV_32S);
polylines(frame, ptsCov, true, Scalar(255),5);
}
cvtColor(frame, frame, CV_BGR2RGB);
//这里写刷新ui代码
usleep(0.05*1000000);
}
这里我跟踪的对象不正确,变成了暗色的山,本来跟踪的是黄色衣服
camshift 跟踪
将meanshift算法扩展到连续图像序列,就是camshift算法。它将视频的所有帧做meanshift运算,并将上一帧的结果,即搜索窗的大小和中心,作为下一帧meanshift算法搜索窗的初始值。如此迭代下去,就可以实现对目标的跟踪
https://blog.csdn.net/dcrmg/article/details/52771372
https://blog.csdn.net/leixiaohua1020/article/details/12236091