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
| int main(int argc, char * argv[]){ STAPLE_TRACKER staple; std::vector<cv::Rect_<float>> result_rects; cv::VideoCapture capture(0); cv::Mat image; cv::Mat tempImage; int64 tic, toc; double time = 0; bool show_visualization = true; bool first_image = true;
cv::namedWindow("STAPLE"); cv::setMouseCallback("STAPLE", on_MouseHandle, (void*)&image);
while(1){ capture.read(image); flip(image, image, 1); if (image.empty()) { break; }
if(drawing_finished == false){ if( drawing_box ){ tempImage.copyTo(image); cv::rectangle(image,groundtruth_rect.tl(),groundtruth_rect.br(),cv::Scalar(0,0,255)); } else{ image.copyTo(tempImage); } } else{ if (first_image){ staple.tracker_staple_initialize(image, groundtruth_rect); staple.tracker_staple_train(image, true); first_image = false; } else{ groundtruth_rect = staple.tracker_staple_update(image); staple.tracker_staple_train(image, false); } } if (show_visualization) { cv::rectangle(image, groundtruth_rect, cv::Scalar(0, 128, 255), 2);
cv::imshow("STAPLE", image); std::cout << "Center: [" << groundtruth_rect.tl().x +groundtruth_rect.width/2 << ", " << groundtruth_rect.tl().y + groundtruth_rect.height/2 << "]" << std::endl;
char key = cv::waitKey(10); if (key == 27 || key == 'q' || key == 'Q') break; }
} cv::destroyAllWindows();
}
cv::Rect_<float> getAxisAlignedBB(std::vector<cv::Point2f> polygon) { double cx = double(polygon[0].x + polygon[1].x + polygon[2].x + polygon[3].x) / 4.; double cy = double(polygon[0].y + polygon[1].y + polygon[2].y + polygon[3].y) / 4.; double x1 = std::min(std::min(std::min(polygon[0].x, polygon[1].x), polygon[2].x), polygon[3].x); double x2 = std::max(std::max(std::max(polygon[0].x, polygon[1].x), polygon[2].x), polygon[3].x); double y1 = std::min(std::min(std::min(polygon[0].y, polygon[1].y), polygon[2].y), polygon[3].y); double y2 = std::max(std::max(std::max(polygon[0].y, polygon[1].y), polygon[2].y), polygon[3].y); double A1 = norm(polygon[1] - polygon[2])*norm(polygon[2] - polygon[3]); double A2 = (x2 - x1) * (y2 - y1); double s = sqrt(A1 / A2); double w = s * (x2 - x1) + 1; double h = s * (y2 - y1) + 1; cv::Rect_<float> rect(cx-1-w/2.0, cy-1-h/2.0, w, h); return rect;
}
std::vector<cv::Rect_<float>> getgroundtruth(std::string txt_file) { std::vector<cv::Rect_<float>> rects; std::ifstream gt; gt.open(txt_file.c_str()); if (!gt.is_open()) std::cout << "Ground truth file " << txt_file << " can not be read" << std::endl; std::string line; float x1, y1, x2, y2, x3, y3, x4, y4; while (getline(gt, line)) { std::replace(line.begin(), line.end(), ',', ' '); std::stringstream ss; ss.str(line); ss >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; std::vector<cv::Point2f>polygon; polygon.push_back(cv::Point2f(x1, y1)); polygon.push_back(cv::Point2f(x2, y2)); polygon.push_back(cv::Point2f(x3, y3)); polygon.push_back(cv::Point2f(x4, y4)); rects.push_back(getAxisAlignedBB(polygon)); } gt.close(); return rects; }
void on_MouseHandle(int event, int x, int y, int flags, void* param ){ cv::Mat& image = *(cv::Mat*) param; switch( event) { case cv::EVENT_MOUSEMOVE: { if( drawing_box ) { groundtruth_rect.width = x-groundtruth_rect.x; groundtruth_rect.height = y-groundtruth_rect.y; } } break;
case cv::EVENT_LBUTTONDOWN: { drawing_box = true; groundtruth_rect = cv::Rect( x, y, 0, 0 ); } break;
case cv::EVENT_LBUTTONUP: { drawing_box = false; drawing_finished = true; if( groundtruth_rect.width < 0 ) { groundtruth_rect.x += groundtruth_rect.width; groundtruth_rect.width *= -1; }
if( groundtruth_rect.height < 0 ) { groundtruth_rect.y += groundtruth_rect.height; groundtruth_rect.height *= -1; } cv::rectangle(image,groundtruth_rect.tl(),groundtruth_rect.br(),cv::Scalar(0,0,255)); } break; } }
|