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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| #include "opencv2/opencv.hpp" #include <Windows.h>
using namespace std; using namespace cv;
TermCriteria criteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 20, 1.0);
bool selecting = false; Rect selection; Point spo;
void onMouse(int event, int x, int y, int, void*) { switch(event) { case EVENT_LBUTTONDOWN: { spo.x = x; spo.y = y; selection.x = spo.x; selection.y = spo.y; selection.width = 0; selection.height = 0; selecting = true;
} break; case EVENT_LBUTTONUP: { selecting = false; } break; default: { selection.x = min(x, spo.x); selection.y = min(y, spo.y); selection.width = abs(x - spo.x); selection.height = abs(y - spo.y); } break; } }
int main() { VideoCapture cam(0); if(!cam.isOpened()) return -1;
Rect srchWnd;
string outputWindow = "Display"; namedWindow(outputWindow); cv::setMouseCallback(outputWindow, onMouse);
vector<Point> route; Mat histogram, backProject, mask;
int key = -1; while(key != ' ') { Mat frame; cam >> frame; if(frame.empty()) break;
Mat frmHsv, hue; vector<Mat> hsvChannels; cvtColor(frame, frmHsv, COLOR_BGR2HSV); split(frmHsv, hsvChannels); hue = hsvChannels[0];
int bins = 120; int nimages = 1; int channels[] = {0}; float rangeHue[] = {0, 180}; const float* ranges[] = {rangeHue}; int histSize[] = { bins }; bool uniform = true;
if(selecting && selection.area() > 0) { Mat sel(frame, selection);
int lbHue = 00 , hbHue = 180; int lbSat = 30 , hbSat = 256; int lbVal = 30 , hbVal = 230;
inRange(frmHsv, Scalar(lbHue,lbSat,lbVal), Scalar(hbHue, hbSat, hbVal), mask);
Mat roi(hue, selection); Mat maskroi(mask, selection);
calcHist(&roi, nimages, channels, maskroi, histogram, 1, histSize, ranges, uniform);
normalize(histogram, histogram, 0, 255, NORM_MINMAX);
bitwise_not(sel, sel);
srchWnd = selection; } else if(!histogram.empty()) {
double scale = 1.0; calcBackProject(&hue, nimages, channels, histogram, backProject, ranges, scale, uniform);
erode(backProject, backProject, Mat());
CamShift(backProject, srchWnd, criteria);
Point p(srchWnd.x + srchWnd.width/2, srchWnd.y + srchWnd.height/2);
route.push_back(p); if(route.size() > 60) route.erase(route.begin());
rectangle(frame, srchWnd, Scalar(0,0,255), 2);
polylines(frame, route,false, Scalar(0,255,0), 2);
cvtColor(backProject, backProject, COLOR_GRAY2BGR); rectangle(backProject, srchWnd, Scalar(0,0,255), 2);
polylines(backProject, route,false, Scalar(0,255,0), 2);
}
switch(key) { case 'b': if(!backProject.empty()) imshow(outputWindow, backProject); else imshow(outputWindow, frame); break; case 'v': default: imshow(outputWindow, frame); break;
}
int k = waitKey(10); if(k > 0) key = k; }
cam.release();
return 0;
}
|