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
|
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <stdio.h> using namespace cv; using namespace std;
int nFrame_num = 0; bool pause = false; bool tracking = false; Rect preselectROI, mySelectROI; bool comp = true;
Mat rhist, ghist, bhist; int channels[] = {0, 1, 2};
const int histsize[] = {16, 16, 16}; const int histsize1 = 16; float rranges[] = {0, 255}; float granges[] = {0, 255}; float branges[] = {0, 255}; float range[] = {0, 255}; const float *ranges1 = {range}; const float *ranges[] = { rranges, granges, branges};
void onMouse(int event, int x, int y, int, void *) { if (event == cv::EVENT_LBUTTONDOWN) { mySelectROI.x = x; mySelectROI.y = y; tracking = false; } else if (event == cv::EVENT_LBUTTONUP) { mySelectROI.width = x - mySelectROI.x; mySelectROI.height = y - mySelectROI.y; tracking = true; comp = true; nFrame_num++; if (nFrame_num >= 10) nFrame_num = 10; } }
int main(int argc, const char *argv[]) { Mat frame, img; Mat staRoiHist; MatND RoiHist; int DRAW_H = 400, DRAW_W = 400; Mat draw(DRAW_W, DRAW_H, CV_8UC3, Scalar(0, 0, 0)); int DRAW_BIN_W = cvRound(DRAW_W / histsize1);
VideoCapture cam(0); if (!cam.isOpened()) return -1;
cout << "\n\n\n\n\t程序运行成功,请用鼠标在窗口中两次框选需要计算直方图的区域。" << endl; namedWindow("camera", 1); namedWindow("rgb_hist", 1); setMouseCallback("camera", onMouse, 0);
while (1) { if (!pause) { cam >> frame; if (frame.empty()) break; }
if (tracking) { Mat RoiImage(frame, mySelectROI);
calcHist(&RoiImage, 1, channels, Mat(), RoiHist, 3, histsize, ranges);
normalize(RoiHist, RoiHist);
vector<Mat> rgb_planes; split(RoiImage, rgb_planes); calcHist(&rgb_planes[ 0 ], 1, 0, Mat(), rhist, 1, &histsize1, &ranges1); normalize(rhist, rhist, 0, DRAW_H, NORM_MINMAX); calcHist(&rgb_planes[ 1 ], 1, 0, Mat(), ghist, 1, &histsize1, &ranges1); normalize(ghist, ghist, 0, DRAW_H, NORM_MINMAX); calcHist(&rgb_planes[ 2 ], 1, 0, Mat(), bhist, 1, &histsize1, &ranges1); normalize(bhist, bhist, 0, DRAW_H, NORM_MINMAX); if (nFrame_num == 1) { preselectROI.x = mySelectROI.x; preselectROI.y = mySelectROI.y; preselectROI.width = mySelectROI.width; preselectROI.height = mySelectROI.height; staRoiHist = RoiHist.clone(); } else if (nFrame_num > 1 && comp == true) { double distence = compareHist( staRoiHist, RoiHist, cv::HISTCMP_INTERSECT); printf("\n\t>与第1次选定的图像区域相似度为:%f\n", distence);
for (int i = 1; i < histsize1; i++) { line(draw, Point(DRAW_BIN_W * (i - 1), DRAW_H - cvRound(rhist.at<float>((i - 1)))), Point(DRAW_BIN_W * (i), DRAW_H - cvRound(rhist.at<float>(i))), Scalar(255, 0, 0), 2, 8, 0); line(draw, Point(DRAW_BIN_W * (i - 1), DRAW_H - cvRound(ghist.at<float>((i - 1)))), Point(DRAW_BIN_W * (i), DRAW_H - cvRound(ghist.at<float>(i))), Scalar(0, 255, 0), 2, 8, 0); line(draw, Point(DRAW_BIN_W * (i - 1), DRAW_H - cvRound(bhist.at<float>((i - 1)))), Point(DRAW_BIN_W * (i), DRAW_H - cvRound(bhist.at<float>(i))), Scalar(0, 0, 255), 2, 8, 0); } imshow("rgb_hist", draw); draw = Mat::zeros(DRAW_W, DRAW_H, CV_8UC3); comp = false; } rectangle(frame, mySelectROI, Scalar(0, 255, 0), 2, 8); } rectangle(frame, preselectROI, Scalar(0, 0, 255), 5, 8); imshow("camera", frame);
char c = (char)waitKey(10); if (c == 27) break; switch (c) { case 'p': pause = !pause; break; default:; } } return 0; }
|