Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cv::Rect2d selectROI(const std::string &video_name, const cv::Mat &frame)
- {
- cv::namedWindow(video_name, cv::WINDOW_NORMAL);
- cv::resizeWindow(video_name, frame.cols, frame.rows);
- struct Data
- {
- cv::Point center;
- cv::Rect2d box;
- static void mouseHandler(int event, int x, int y, int flags, void *param)
- {
- Data *data = (Data*)param;
- switch (event)
- {
- // start to select the bounding box
- case cv::EVENT_LBUTTONDOWN:
- data->box = cvRect(x, y, 0, 0);
- data->center = cv::Point2f((float)x, (float)y);
- break;
- // update the selected bounding box
- case cv::EVENT_MOUSEMOVE:
- if (flags == 1)
- {
- data->box.width = 2 * (x - data->center.x);
- data->box.height = 2 * (y - data->center.y);
- data->box.x = data->center.x - data->box.width / 2.0;
- data->box.y = data->center.y - data->box.height / 2.0;
- }
- break;
- // cleaning up the selected bounding box
- case cv::EVENT_LBUTTONUP:
- if (data->box.width < 0)
- {
- data->box.x += data->box.width;
- data->box.width *= -1;
- }
- if (data->box.height < 0)
- {
- data->box.y += data->box.height;
- data->box.height *= -1;
- }
- break;
- }
- }
- } data;
- cv::setMouseCallback(video_name, Data::mouseHandler, &data);
- while (cv::waitKey(1) < 0)
- {
- cv::Mat draw = frame.clone();
- rectangle(draw, data.box, cv::Scalar(255, 0, 0), 2, 1);
- cv::imshow(video_name, draw);
- }
- cv::destroyWindow(video_name);
- return data.box;
- }
- void toGray(const cv::Mat &frame, cv::Mat &gray)
- {
- if (frame.channels() > 1)
- cv::cvtColor(frame, gray, CV_BGR2GRAY);
- else
- frame.copyTo(gray);
- }
- void toRGB(const cv::Mat &frame, cv::Mat &rgb)
- {
- if (frame.channels() == 1)
- cv::cvtColor(frame, rgb, CV_GRAY2RGB);
- else
- frame.copyTo(rgb);
- }
- void drawBoundingBox(cv::Mat image, std::vector<cv::Point2f> bb, cv::Scalar colour)
- {
- for (unsigned i = 0; i < bb.size() - 1; i++) {
- line(image, bb[i], bb[i + 1], colour, 2);
- }
- line(image, bb[bb.size() - 1], bb[0], colour, 2);
- }
- void drawDot(cv::Mat image, std::vector<cv::Point2f> bb, cv::Scalar colour)
- {
- for (unsigned i = 0; i < bb.size() - 1; i++)
- {
- //line(image, bb[i], bb[i + 1], colour, 2);
- cv::circle(image, bb[i], 2, colour, 2);
- }
- cv::circle(image, bb[bb.size() - 1], 2, colour, 2);
- }
- std::vector<cv::Point2f> Points(std::vector<cv::KeyPoint> keypoints)
- {
- std::vector<cv::Point2f> res;
- for (unsigned i = 0; i < keypoints.size(); i++) {
- res.push_back(keypoints[i].pt);
- }
- return res;
- }
- std::string paddingZeros(int num, int max_zeros)
- {
- std::ostringstream ss;
- ss << std::setw(max_zeros) << std::setfill('0') << num;
- return ss.str();
- }
- std::string cv_makeFilename(std::string path_pattern, int index, cv_FileSuffix format)
- {
- std::string format_1 = "";
- switch (format)
- {
- case JPEG: format_1 += ".jpeg";
- case PNG: format_1 += ".png";
- default: format_1 += " ";
- }
- std::string save_index = paddingZeros(index + 1, 4);
- return (path_pattern + save_index + format_1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement