CamShift的原理还是比较简单的,跟踪直方图特征搜索出目标进行跟踪,相对于meanShift,解决的尺度问题。
代码如下:
#include "opencv2/video/tracking.hpp"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include#include using namespace cv;using namespace std;Mat image;bool backprojMode = false;bool selectObject = false;int trackObject = 0;bool showHist = true;Point origin;Rect selection;int vmin = 10, vmax = 256, smin = 30;static void onMouse( int event, int x, int y, int, void* ){ if( selectObject )只有当鼠标左键按下去时才有效,然后通过if里面代码就可以确定所选择的矩形区域selection了 { selection.x = MIN(x, origin.x);//矩形左上角顶点坐标 selection.y = MIN(y, origin.y); selection.width = std::abs(x - origin.x);//矩形宽 selection.height = std::abs(y - origin.y);//矩形高 selection &= Rect(0, 0, image.cols, image.rows);//用于确保所选的矩形区域在图片范围内 } switch( event ) { case CV_EVENT_LBUTTONDOWN: origin = Point(x,y);//鼠标初始点击坐标 selection = Rect(x,y,0,0);//鼠标刚按下去时初始化了一个矩形区域 selectObject = true; break; case CV_EVENT_LBUTTONUP: selectObject = false; if( selection.width > 0 && selection.height > 0 ) trackObject = -1; break; }}static void help(){ cout << "\nThis is a demo that shows mean-shift based tracking\n" "You select a color objects such as your face and it tracks it.\n" "This reads from video camera (0 by default, or the camera number the user enters\n" "Usage: \n" " ./camshiftdemo [camera number]\n"; cout << "\n\nHot keys: \n" "\tESC - quit the program\n" "\tc - stop the tracking\n" "\tb - switch to/from backprojection view\n" "\th - show/hide object histogram\n" "\tp - pause video\n" "To initialize tracking, select the object with mouse\n";}const char* keys ={ "{c| camero | 0 | camera number}" //简称 | 全称 |值 |帮助说明 "{f| file | F:/.mp4 | open avi files}"};int main( int argc, const char** argv ){ help(); VideoCapture cap; Rect trackWindow;//跟踪窗的大小 int hsize = 16; float hranges[] = {0,180}; //直方图的范围 const float* phranges = hranges; CommandLineParser parser(argc, argv, keys);//命令行解析器 int camNum = parser.get ("c"); cap.open(camNum); parser.printParams(); if( !cap.isOpened() ) { help(); cout << "***Could not initialize capturing...***\n"; cout << "Current parameter's value: \n"; parser.printParams(); //打印出keys return -1; } namedWindow( "Histogram", 0 ); namedWindow( "CamShift Demo", 0 ); setMouseCallback( "CamShift Demo", onMouse, 0 ); createTrackbar( "Vmin", "CamShift Demo", &vmin, 256, 0 ); createTrackbar( "Vmax", "CamShift Demo", &vmax, 256, 0 ); createTrackbar( "Smin", "CamShift Demo", &smin, 256, 0 ); Mat frame, hsv, hue, mask, hist, histimg = Mat::zeros(200, 320, CV_8UC3), backproj; bool paused = false; for(;;) { if( !paused ) //多个相同条件的If语句,可以同步进行好几步操作 { cap >> frame; if( frame.empty() ) break; } frame.copyTo(image); //多个相同条件的If语句,可以同步进行好几步操作 if( !paused ) { cvtColor(image, hsv, CV_BGR2HSV); if( trackObject ) { int _vmin = vmin, _vmax = vmax; //inRange函数的功能是检查输入数组每个元素大小是否在2个给定数值之间,可以有多通道,mask保存0通道的最小值,也就是h分量 //这里利用了hsv的3个通道,比较h,0~180,s,smin~256,v,min(vmin,vmax),max(vmin,vmax)。如果3个通道都在对应的范围内,则 //mask对应的那个点的值全为1(0xff),否则为0(0x00). 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索引数组 //setMouseCallback( "CamShift Demo", NULL, 0 ); //注销鼠标事件 if( trackObject < 0 )//鼠标选择区域松开后,该函数内部又将其赋值1 { //此处的构造函数roi用的是Mat hue的矩阵头,且roi的数据指针指向hue,即共用相同的数据, //select为其感兴趣的区域 mask保存的hsv的最小值 Mat roi(hue, selection), maskroi(mask, selection); calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges); normalize(hist, hist, 0, 255, CV_MINMAX); trackWindow = selection; trackObject = 1;//只要鼠标选完区域松开后,且没有按键盘清0键'c',则trackObject一直保持为1, //因此该if函数只能执行一次,除非重新选择跟踪区域 //histimg是直方图图像 histimg = Scalar::all(0);//与按下'c'键是一样的,这里的all(0)表示的是标量全部清0 int binW = histimg.cols / hsize; Mat buf(1, hsize, CV_8UC3);//定义一个缓冲单bin矩阵,不同的bin画出不同颜色 for( int i = 0; i < hsize; i++ ) buf.at (i) = Vec3b(saturate_cast (i*180./hsize), 255, 255);//saturate_case函数为从一个初始类型准确变换到另一个初始类型 cvtColor(buf, buf, CV_HSV2BGR); for( int i = 0; i < hsize; i++ ) { int val = saturate_cast (hist.at (i)*histimg.rows/255); rectangle( histimg, Point(i*binW,histimg.rows), Point((i+1)*binW,histimg.rows - val), Scalar(buf.at (i)), -1, 8 ); } } calcBackProject(&hue, 1, 0, hist, backproj, &phranges); //imshow("backproj",backproj); //waitKey(20); backproj &= mask; //超出范围的置零 //opencv2.0以后的版本函数命名前没有cv两字了,并且如果函数名是由2个意思的单词片段组成的话, //且前面那个片段不够成单词,则第一个字母要 //大写,比如Camshift,如果第一个字母是个单词,则小写,比如meanShift,但是第二个字母一定要大写 RotatedRect trackBox = CamShift(backproj, trackWindow, TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 )); if( trackWindow.area() <= 1 ) { int cols = backproj.cols, rows = backproj.rows, r = (MIN(cols, rows) + 5)/6; trackWindow = Rect(trackWindow.x - r, trackWindow.y - r, trackWindow.x + r, trackWindow.y + r) & Rect(0, 0, cols, rows); } if( backprojMode ) cvtColor( backproj, image, CV_GRAY2BGR ); ellipse( image, trackBox, Scalar(0,0,255), 3, CV_AA ); } } else if( trackObject < 0 ) paused = false; if( selectObject && selection.width > 0 && selection.height > 0 ) { Mat roi(image, selection); bitwise_not(roi, roi); } imshow( "CamShift Demo", image ); imshow( "Histogram", histimg ); char c = (char)waitKey(10); if( c == 27 ) break; switch(c) { case 'b': backprojMode = !backprojMode; break; case 'c': trackObject = 0; histimg = Scalar::all(0); break; case 'h': showHist = !showHist; if( !showHist ) destroyWindow( "Histogram" ); else namedWindow( "Histogram", 1 ); break; case 'p': paused = !paused; break; default: ; } } return 0;}
一下是上面程序涉及到的几个函数的用法的简单测试代码,以了解,函数的特性
包括 Mat(const Mat& m, const Rect& roi)用法测试,saturate_cast函数测试,RotatedRect类型测试代码,mixChannels用法测试
#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include#include using namespace cv;using namespace std;int main(){ //Mat感兴趣区域设置, Mat(const Mat& m, const Rect& roi)用法测试 // 即: creates a matrix header for a part of the bigger matrix Mat img=imread("./longtan.jpg"); Rect roi(100,100,100,100); Mat img_roi(img,roi);//感兴趣区域选择,img_roi和img共用一个矩阵头,指向相同的值区域 namedWindow("img", WINDOW_AUTOSIZE); namedWindow("img_roi",WINDOW_AUTOSIZE); cout<<"img.cols ="< < < (-100); // a = 0 (UCHAR_MIN)安全转换 short b = saturate_cast (33333.33333); // b = 32767 (SHRT_MAX) cout<<"a ="< <<(int)a<