OpenCV连通域相关操作

  1. 连通域反选

      在使用Opencv的findcontours函数寻找连通域轮廓时,可能需要使用到类似PS中的选区反选功能。

      以下对这一部分进行说明:

      在findcontours函数中的mode参数中选择CV_RETR_CCOMP两级轮廓查找,

      构建反选的选区范围为读入图像大小:

    vector<cv::Point> boundcontours(4);
    boundcontours[0] = cv::Point(0, 0);
    boundcontours[1] = cv::Point(0, src.rows-1);
    boundcontours[2] = cv::Point(src.cols-1, src.rows-1);
    boundcontours[3] = cv::Point(src.cols-1, 0);

 

int main()
{
    cv::Mat src = imread("原图.png", 0);
    vector<vector<cv::Point>>linecontours;
    vector<cv::Vec4i>hierarchy;
    findContours(src, linecontours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//范围限定 linecontours[
0].clear(); vector<cv::Point> boundcontours(4); boundcontours[0] = cv::Point(0, 0); boundcontours[1] = cv::Point(0, src.rows-1); boundcontours[2] = cv::Point(src.cols-1, src.rows-1); boundcontours[3] = cv::Point(src.cols-1, 0); linecontours[0] = boundcontours;
//重绘选区 cv::Mat temptImg(scr.size(),CV_8UC1,Scalar(
0)); drawContours(temptImg, linecontours, -1, Scalar(196), -1);//填充反向选区 }

      原图:

      结果如下:

      这种选取方式可以用来排除感兴趣连通域外部的干扰。

posted @ 2016-12-18 21:59  Didea  阅读(7288)  评论(0编辑  收藏  举报