Go to the documentation of this file.00001
00038 #include <bwi_mapper/connected_components.h>
00039 #include <bwi_mapper/map_utils.h>
00040
00041 #include <opencv2/opencv_modules.hpp>
00042 #ifdef HAVE_OPENCV_IMGPROC
00043
00044 #include <opencv2/imgproc/imgproc.hpp>
00045 #endif
00046
00047 namespace bwi_mapper {
00048
00056 ConnectedComponents::ConnectedComponents (const cv::Mat& image,
00057 std::vector<int32_t>& component_map) {
00058
00059 cv::Mat src = image.clone();
00060 cv::Mat dst = cv::Mat::zeros(src.rows, src.cols, CV_32S);
00061 std::vector<std::vector<cv::Point> > contours;
00062 std::vector<cv::Vec4i> hierarchy;
00063
00064 cv::findContours(src, contours, hierarchy,
00065 CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
00066
00067 number_components_ = 0;
00068 for(int idx = 0; idx >= 0; idx = hierarchy[idx][0]) {
00069 cv::Scalar color(number_components_ + 1);
00070 cv::drawContours(dst, contours, idx, color, CV_FILLED, 8, hierarchy);
00071 number_components_++;
00072 }
00073
00074 component_map.clear();
00075 component_map.resize(dst.rows * dst.cols);
00076
00077 size_t map_idx = 0;
00078 for (int j = 0; j < dst.rows; ++j) {
00079 const int32_t* dst_row = dst.ptr<int32_t>(j);
00080 for (int i = 0; i < dst.cols; ++i) {
00081 component_map[map_idx] = dst_row[i] - 1;
00082 ++map_idx;
00083 }
00084 }
00085
00086 }
00087
00092 size_t ConnectedComponents::getNumberComponents() {
00093 return number_components_;
00094 }
00095
00096 }