OpenCV學習筆記十九---運動跟蹤 CamShift以及meanShift詳解
阿新 • • 發佈:2019-01-08
#include <opencv2/video/tracking.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> #include <ctype.h> using namespace std; using namespace cv; Mat image; bool backprojMode = false;//表示是否進入反向投影模式,true表示進入反向投影模式 bool selectObject = false;//選擇要跟蹤的初始目標,true代表正在用滑鼠選擇 int trackObject = 0;//代表目標跟蹤數量 bool showHist = true;//是否顯示直方圖 Point origin; //儲存滑鼠選擇第一次單機時點的位置 Rect selection; //用於儲存選擇的矩形框 int vmin = 10, vmax = 256, smin = 30; void onMouse(int event, int x, int y, int, void*) { if (selectObject) //通過滑鼠左鍵按下去選擇矩形區域 { 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; } } 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 = { "{1| | 0 | camera number}" }; int main(int argc, const char** argv) { help(); VideoCapture cap; //定義一個攝像頭捕捉的類物件 Rect trackWindow; RotatedRect trackBox; //定義一個旋轉的矩陣類物件 int hsize = 16; float hranges[] = { 0, 180 }; const float* phranges = hranges; CommandLineParser parser(argc, argv, keys); //命令解析器函式 int camNum = parser.get<int>("1"); cap.open(camNum);//直接呼叫成員函式開啟攝像頭 if (!cap.isOpened()) { help(); cout << "***Could not initialize capturing...***\n"; cout << "Current parameter's value: \n"; parser.printParams(); 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; Mat histimg = Mat::zeros(200, 320, CV_8UC3), backproj; bool paused = false; for (;;) { if (!paused) //沒有暫停 { cap >> frame;//從攝像頭抓取一幀影象並輸出到frame中 if (frame.empty()) break; } frame.copyTo(image); if (!paused) { cvtColor(image, hsv, CV_BGR2HSV); if (trackObject) { int _vmin = vmin, _vmax = vmax; //利用了hsv的3個通道 inRange(hsv, Scalar(0, smin, MIN(_vmin, _vmax)), Scalar(180, 256, MAX(_vmin, _vmax)), mask); int ch[] = { 0, 0 }; //hue初始化為與hsv大小深度一樣的矩陣,色調的度量是用角度表示的,紅綠藍之間相差120度,反色相差180度 hue.create(hsv.size(), hsv.depth()); mixChannels(&hsv, 1, &hue, 1, ch, 1); if (trackObject < 0)//滑鼠選擇區域鬆開後,該函式內部又將其賦值1 { //此處的建構函式roi用的時Mat hue的矩陣頭,且roi的資料指標指向hue 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; histimg = Scalar::all(0); int binW = histimg.cols / hsize; Mat buf(1, hsize, CV_8UC3); //定義一個緩衝單bin矩陣 for (int i = 0; i < hsize; i++) buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*180. / hsize), 255, 255); cvtColor(buf, buf, CV_HSV2BGR); for (int i = 0; i < hsize; i++) { int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows / 255); rectangle(histimg, Point(i*binW, histimg.rows), Point((i + 1)*binW, histimg.rows - val), Scalar(buf.at<Vec3b>(i)), -1, 8); } } calcBackProject(&hue, 1, 0, hist, backproj, &phranges); backproj &= mask; trackBox = CamShift(backproj, trackWindow, TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1)); //meanShift(backproj, trackWindow, TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1)); if (trackWindow.area() <= 1) { int cols = backproj.cols, rows = backproj.rows; int 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); //rectangle(image, Point(trackWindow.x, trackWindow.y), Point(trackWindow.x + trackWindow.width, trackWindow.y + trackWindow.height), Scalar(0, 0, 255), -1, CV_AA); } } //後面程式碼不管pause為真還是假都是要執行的 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; 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; }