diff --git a/calib/circles_grid.png b/calib/circles_grid.png index 39b80d4..ed3632f 100644 Binary files a/calib/circles_grid.png and b/calib/circles_grid.png differ diff --git a/calib/draw_circles_grid.py b/calib/draw_circles_grid.py index e3c2321..e19db1a 100644 --- a/calib/draw_circles_grid.py +++ b/calib/draw_circles_grid.py @@ -5,7 +5,7 @@ fig = plt.gcf() n_cols = 5 n_rows = 4 -r = 0.2 +r = 0.1 for x in range(0, n_cols): for y in range(0, n_rows): circle = plt.Circle((x, y), r, color='k') diff --git a/cfg/CalibProcDyn.cfg b/cfg/CalibProcDyn.cfg index e7a7ac9..5dceeba 100755 --- a/cfg/CalibProcDyn.cfg +++ b/cfg/CalibProcDyn.cfg @@ -7,8 +7,16 @@ gen = ParameterGenerator() # name / type / level / description / default / min / max # Flir A35 has a maximum resolution of 320 x 256 +gen.add("sigma", double_t, 0, + "gaussian sigma", 1, 1, 20) +gen.add("min_area", double_t, 0, + "contour min area", 40, 1, 1000) +gen.add("max_area", double_t, 0, + "contour max area", 200, 100, 10000) gen.add("thresh_window", int_t, 0, "adaptive threshold window size", 11, 3, 100) +gen.add("erosion_size", int_t, 0, + "erosion size", 1, 1, 10) size_enum = gen.enum([gen.const("mean", int_t, 0, "Adaptive thresh mean"), gen.const("gaussian", int_t, 1, "Adaptive thresh gaussian")], "Adaptive thresh type ") diff --git a/src/calib_proc/calib_proc_node.cpp b/src/calib_proc/calib_proc_node.cpp index 9ceda6c..f662b30 100644 --- a/src/calib_proc/calib_proc_node.cpp +++ b/src/calib_proc/calib_proc_node.cpp @@ -36,39 +36,55 @@ void CalibProcNode::ConfigCb(CalibProcDynConfig &config, int level) { if (!(config.thresh_window % 2)) { config.thresh_window += 1; } + if (config.min_area > config.max_area) { + config.max_area = config.min_area + 1; + } config_ = config; } void CalibProcNode::ImageCb(const sensor_msgs::ImageConstPtr &image_msg) { cv::Mat image = cv_bridge::toCvCopy(image_msg, image_msg->encoding)->image; - // cv::imshow("image", image); cv::Mat inverted; cv::bitwise_not(image, inverted); - // cv::imshow("inverted", inverted); - /* + // Threshold cv::Mat thresh; cv::adaptiveThreshold(image, thresh, 255, config_.thresh_type, cv::THRESH_BINARY, config_.thresh_window, 0); cv::imshow("thresh", thresh); - */ - /* - std::vector> contours; - cv::findContours(raw, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); + // Gaussian blur + cv::Mat gauss; + cv::GaussianBlur(thresh, gauss, cv::Size(), config_.sigma, config_.sigma, + cv::BORDER_DEFAULT); + cv::imshow("gauss", gauss); + + // Erosion + cv::Mat eroded; + int erosion_size = config_.erosion_size; + cv::Mat element = cv::getStructuringElement( + cv::MORPH_RECT, cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1), + cv::Point(erosion_size, erosion_size)); + cv::erode(thresh, eroded, element); + cv::imshow("erosion", eroded); - cv::Mat contour_image; - cv::cvtColor(raw, contour_image, CV_GRAY2BGR); - for (size_t i = 0; i < contours.size(); ++i) { - cv::drawContours(contour_image, contours, i, cv::Scalar(255, 0, 0), 2, 8); - } - cv::imshow("contour", contour_image); - */ + // Contour + std::vector> contours; + cv::findContours(eroded, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); + + cv::Mat ctr_image; + cv::cvtColor(image, ctr_image, CV_GRAY2BGR); + for (size_t i = 0; i < contours.size(); ++i) { + double area = cv::contourArea(contours[i]); + if (area > config_.min_area && area < config_.max_area) { + cv::drawContours(ctr_image, contours, i, cv::Scalar(255, 0, 0), 2, 8); + } + } + cv::imshow("contour", ctr_image); - // Detect circles grid cv::Mat display; - DetectAndDrawCriclesGrid(inverted, cv::Size(4, 5), display); + DetectAndDrawCriclesGrid(inverted, cv::Size(5, 4), display); cv::Mat calib(inverted);