1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| #include<opencv2/opencv.hpp> #include<opencv2/highgui/highgui.hpp> #include<opencv/cv.h> #include<opencv/highgui.h>
using namespace std; using namespace cv;
const static int SEAITVITV_VALUE = 20; const static int BLUR_SIZE = 10;
int theObject[2] = { 0,0 };
Rect objectBoundingRect = Rect(0, 0, 0, 0);
string intTostring(int num) {
stringstream ss; ss << num; return ss.str(); }
void searchForMovement(Mat thresgoldImage, Mat &cameraFeed) {
Mat temp; bool objectDetected;
thresgoldImage.copyTo(temp);
vector < vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
if (contours.size() > 0) objectDetected = true; else { objectDetected = false; }
if (objectDetected) { vector<vector<Point> > largesContours; largesContours.push_back(contours.at(contours.size() - 1)); objectBoundingRect = boundingRect(largesContours.at(0)); int xpos = objectBoundingRect.x + objectBoundingRect.width / 2; int ypos = objectBoundingRect.y + objectBoundingRect.width / 2; theObject[0] = xpos; theObject[1] = ypos;
} int x = theObject[0]; int y = theObject[1];
circle(cameraFeed, Point(x, y), 20, Scalar(0, 255, 0), 2);
line(cameraFeed, Point(x, y), Point(x, y - 25),Scalar(0,255,0),2); line(cameraFeed, Point(x, y), Point(x, y + 25), Scalar(0, 255, 0), 2); line(cameraFeed, Point(x, y), Point(x-25, y ), Scalar(0, 255, 0), 2); line(cameraFeed, Point(x, y), Point(x+25, y ), Scalar(0, 255, 0), 2);
putText(cameraFeed, "tracking at:(" + intTostring(x) + "," + intTostring(y) + ")", Point(x, y), 1, 1, Scalar(0, 255, 0)); }
int main() {
bool debugMode = false;
bool trackingEnable = false;
bool pause = false;
Mat frame1, frame2;
Mat gray1, gray2;
Mat differenceImg;
VideoCapture capture;
Mat thres; while (1) { capture.open("3.mp4"); if (!capture.isOpened()) { return -2; } while (capture.get(CV_CAP_PROP_POS_FRAMES) < capture.get(CV_CAP_PROP_FRAME_COUNT) - 1) { capture.read(frame1); cvtColor(frame1, gray1, COLOR_BGR2GRAY); capture.read(frame2); cvtColor(frame2, gray2, COLOR_BGR2GRAY); absdiff(gray1, gray2, differenceImg); threshold(differenceImg, thres, SEAITVITV_VALUE, 255, THRESH_BINARY); blur(thres, thres, Size(BLUR_SIZE, BLUR_SIZE)); threshold(thres, thres, SEAITVITV_VALUE, 255, THRESH_BINARY); if (debugMode == true) { imshow("thres", thres); } else { destroyWindow("thres"); } if (trackingEnable) { searchForMovement( thres, frame1); } imshow("frame", frame1); switch (waitKey(50)) { case 27: return 0; case 't': case 'T': { trackingEnable = !trackingEnable; if (trackingEnable == false) cout << "没有捕获" << endl; else cout << "捕获开始" << endl; break; } case 'd': case 'D': { debugMode = !debugMode; if (debugMode == false) cout << "调试模式退出\n"; else cout << "进入调试模式" << endl; break; } case 112: pause = !pause; if (pause == true) { cout << "暂停" << endl; while (pause == true) { switch (waitKey()) { case 112: pause = false; break; } } } else cout << "开始" << endl; break; } } capture.release(); } }
|