Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ============================================================================
- class frame_processor
- {
- public:
- typedef std::vector<cv::Point> contour_t;
- struct polygon_type
- {
- cv::Scalar color;
- std::string name;
- };
- struct polygon_info
- {
- polygon_type type;
- contour_t points;
- cv::Point centroid;
- };
- typedef std::vector<polygon_info> polygon_infos_t;
- public:
- frame_processor();
- polygon_infos_t process_frame(cv::Mat const& frame);
- private:
- typedef std::map<size_t, polygon_type> polygon_types_t;
- // void perform_morph_ops(cv::Mat &thresh) const; // Unused
- bool process_contour(contour_t const& contour, polygon_info& info);
- private:
- polygon_types_t polygon_types_;
- double threshold_level_;
- double min_area_;
- cv::Mat erode_element_;
- cv::Mat dilate_element_;
- cv::Mat grayscale_buffer_;
- };
- // ============================================================================
- frame_processor::frame_processor()
- : threshold_level_(128)
- , min_area_(100)
- {
- erode_element_ = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(10, 10));
- dilate_element_ = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(10, 10));
- // Initialize supported polygon types...
- polygon_types_.emplace(3, polygon_type{ cv::Scalar(0, 0, 255), "Triangle" });
- polygon_types_.emplace(4, polygon_type{ cv::Scalar(0, 255, 0), "Quadrilateral" });
- polygon_types_.emplace(7, polygon_type{ cv::Scalar(255, 0, 0), "Heptagon" });
- polygon_types_.emplace(10, polygon_type{ cv::Scalar(127, 127, 0), "Decagon" });
- }
- // ----------------------------------------------------------------------------
- /* Unused
- void frame_processor::perform_morph_ops(cv::Mat &thresh) const
- {
- cv::erode(thresh, thresh, erode_element_, cv::Point(-1, -1), 2);
- cv::dilate(thresh, thresh, dilate_element_, cv::Point(-1, -1), 2);
- }
- */
- // ----------------------------------------------------------------------------
- bool frame_processor::process_contour(contour_t const& contour, polygon_info& info)
- {
- if (contourArea(contour) < min_area_) {
- return false;
- }
- cv::Moments moment = cv::moments(cv::Mat(contour));
- int x1(static_cast<int>(std::round(moment.m10 / moment.m00)));
- int y1(static_cast<int>(std::round(moment.m01 / moment.m00)));
- polygon_types_t::const_iterator it(polygon_types_.find(contour.size()));
- if (it == polygon_types_.end()) {
- return false;
- }
- info.type = it->second;
- info.points = contour;
- info.centroid = cv::Point(x1, y1);
- return true;
- }
- // ----------------------------------------------------------------------------
- frame_processor::polygon_infos_t frame_processor::process_frame(cv::Mat const& frame)
- {
- cv::cvtColor(frame, grayscale_buffer_, cv::COLOR_BGR2GRAY);
- cv::threshold(grayscale_buffer_, grayscale_buffer_, threshold_level_
- , 255, cv::THRESH_BINARY);
- std::vector<contour_t> contours;
- std::vector<cv::Vec4i> hierarchy;
- cv::findContours(grayscale_buffer_, contours, hierarchy
- , CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
- polygon_infos_t result;
- for (contour_t const& contour : contours) {
- std::vector<cv::Point> approx_contour;
- cv::approxPolyDP(cv::Mat(contour), approx_contour, 3, true);
- polygon_info info;
- if (process_contour(approx_contour, info)) {
- result.push_back(info);
- }
- }
- return result;
- }
- // ============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement