Go to the documentation of this file.00001
00038 #include <bwi_mapper/connected_components.h>
00039 #include <bwi_mapper/map_utils.h>
00040
00041 namespace bwi_mapper {
00042
00050 ConnectedComponents::ConnectedComponents (const cv::Mat& image,
00051 std::vector<int32_t>& component_map) {
00052
00053 cv::Mat src = image.clone();
00054 cv::Mat dst = cv::Mat::zeros(src.rows, src.cols, CV_32S);
00055 cv::vector<cv::vector<cv::Point> > contours;
00056 cv::vector<cv::Vec4i> hierarchy;
00057
00058 cv::findContours(src, contours, hierarchy,
00059 CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
00060
00061 number_components_ = 0;
00062 for(int idx = 0; idx >= 0; idx = hierarchy[idx][0]) {
00063 cv::Scalar color(number_components_ + 1);
00064 cv::drawContours(dst, contours, idx, color, CV_FILLED, 8, hierarchy);
00065 number_components_++;
00066 }
00067
00068 component_map.clear();
00069 component_map.resize(dst.rows * dst.cols);
00070
00071 size_t map_idx = 0;
00072 for (int j = 0; j < dst.rows; ++j) {
00073 const int32_t* dst_row = dst.ptr<int32_t>(j);
00074 for (int i = 0; i < dst.cols; ++i) {
00075 component_map[map_idx] = dst_row[i] - 1;
00076 ++map_idx;
00077 }
00078 }
00079
00080 }
00081
00086 size_t ConnectedComponents::getNumberComponents() {
00087 return number_components_;
00088 }
00089
00090 }