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
| delta_height_point_ = current_target_.deltaPoint(); roi_global_center_ = current_target_.Armor().Rect().center - buff_config_.param.BIG_LENTH_R * delta_height_point_;
if (roi_global_center_.x < 0 || roi_global_center_.y < 0 || roi_global_center_.x > _input_src_img.cols || roi_global_center_.y > _input_src_img.rows) { if (roi_global_center_.x < 0) { roi_global_center_.x = 1; } if (roi_global_center_.y < 0) { roi_global_center_.y = 1; } if (roi_global_center_.x > _input_src_img.cols) { roi_global_center_.x = _input_src_img.cols - 1; } if (roi_global_center_.y > _input_src_img.rows - 1) { roi_global_center_.y = _input_src_img.rows - 1; } }
cv::RotatedRect roi_R(roi_global_center_, cv::Size(buff_config_.param.CENTER_R_ROI_SIZE, buff_config_.param.CENTER_R_ROI_SIZE), 0); cv::Rect roi = roi_R.boundingRect();
roi = roi_tool_.makeRectSafeTailor(_input_src_img, roi);
result_img_ = roi_tool_.cutRoIRect(_input_bin_img, roi); roi_img_ = roi_tool_.cutRoIRect(_input_src_img, roi); is_circle_ = false;
roi_local_center_ = cv::Point2f(roi_img_.cols * 0.5, roi_img_.rows * 0.5);
cv::findContours(result_img_, contours_r_, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
fmt::print("[{}] 圆心目标遍历轮廓数量: {} \n", center_yellow, contours_r_.size());
for (size_t i = 0; i != contours_r_.size(); ++i) { if (contours_r_[i].size() < 6) { continue; }
center_r_.inputParams(contours_r_[i], roi_img_); fmt::print("[{}] 矩形 {} 比例:{}\n", center_yellow, i, center_r_.aspectRatio()); if (center_r_.aspectRatio() < buff_config_.param.CENTER_R_ASPECT_RATIO_MIN || center_r_.aspectRatio() > buff_config_.param.CENTER_R_ASPECT_RATIO_MAX) { continue; } fmt::print("[{}] 矩形 {} 面积:{}\n", center_yellow, i, center_r_.Rect().boundingRect().area()); if (center_r_.Rect().boundingRect().area() < buff_config_.param.CENTER_R_AREA_MIN || center_r_.Rect().boundingRect().area() > buff_config_.param.CENTER_R_AREA_MAX) { continue; } fmt::print("[{}] Find center R target success !!! ", center_yellow); fmt::print(" --》 矩形 {} --》 Ratio: {} / Area: {} ", i, center_r_.aspectRatio(), center_r_.Rect().boundingRect().area()); center_r_box_.push_back(center_r_);
fmt::print("\n"); }
fmt::print("[{}] 符合比例条件的有: {}\n", center_yellow, center_r_box_.size());
if (center_r_box_.size() < 1) { fmt::print("[{}] 圆心为:假定圆心 \n", center_yellow); is_circle_ = false; center_r_point2f = roi_global_center_;
} else { std::sort(center_r_box_.begin(), center_r_box_.end(), [](abstract_center_r::Center_R& c1, abstract_center_r::Center_R& c2) { return c1.centerDist() < c2.centerDist(); });
fmt::print("[{}] 圆心为:真实圆心 \n", center_yellow); is_circle_ = true; center_r_point2f = center_r_box_[0].Rect().center + roi_R.boundingRect2f().tl();
}
center_r_box_.clear(); contours_r_.clear();
std::vector<abstract_center_r::Center_R>(center_r_box_).swap(center_r_box_); std::vector<std::vector<cv::Point>>(contours_r_).swap(contours_r_); return center_r_point2f; }
|