zoukankan      html  css  js  c++  java
  • [OpenCV]绘制填充轮廓drawContours

    我们知道如果要求取轮廓可以使用findContours函数,

    该函数会返回为vector<vector<Point> >的轮廓向量。

    而在以前想对轮廓进行绘制我通常会使用遍历这个vector然后依次将点绘制到img

    其实OpenCV里面已经有drawContours这个函数可以实现这个效果

    具体来说,当我想将这个轮廓进行填充的时候我会有下面2步骤:

    a)依次遍历轮廓点,将点绘制到img上

        void drawMaxAreaLine(cv::Mat &dst, const std::vector<cv::Point> maxAreaPoints)
        {
            int step = dst.step;
            auto data = dst.data;
            for (int i = 0; i < maxAreaPoints.size(); ++i)
            {
                *(data + maxAreaPoints[i].x + maxAreaPoints[i].y * step) = 255;
            }
        }

    b)使用floodFill以及一个种子点进行填充

        floodFill(savedGrayMat, Point(currentFrameEdge[0].x + 2, currentFrameEdge[0].y + 2), 255);

    这里常会碰到种子点不好选取的问题,因为有时候所选择的种子点不能保证对所有轮廓都适用。也就是查找一个在轮廓内的点是存在一定难度的。

    ButInfact:

    drawContours就可以办到!

    vector<vector<Point> > contours;
    contours.push_back(currentFrameEdge);
    Mat savedGrayMat = Mat::zeros(RectData[0].rows, RectData[0].cols, CV_8UC1);
    //drawMaxAreaLine(savedGrayMat, currentFrameEdge);
    //floodFill(savedGrayMat, Point(currentFrameEdge[0].x + 2, currentFrameEdge[0].y + 2), 255);
    drawContours(savedGrayMat, contours, 0, Scalar(255), CV_FILLED);
    imshow("savedGrayMat", savedGrayMat);
    waitKey();

    上面中间的图就是通过drawContours得到的

    上面的左图和中间图进行与就可以得到右图!

     std::stringstream strStream;
     strStream << "L\\" << i << ".jpg";
     string savedName = strStream.str();
     bitwise_and(savedGrayMat, RectData[i], savedGrayMat);
     //这条语句进行与效果是将原始图与上面得到的ROI图进行&&使得只有中间图的白色区域会留下左图的信息
    cv::imwrite(savedName, savedGrayMat);
  • 相关阅读:
    PCB Genesis加邮票孔(线与线)实现算法
    PCB 无需解压,直接读取Zip压缩包指定文件 实现方法
    PCB MS SQL CLR聚合函数(函数作用,调用顺序,调用次数) CLR说明
    PCB MS SQL表值函数与CLR 表值函数 (例:字符串分割转表)
    PCB MS CLR 聚合函数 joinString加排序实现
    PCB 奥宝LDI 输出自动改周期检测内容
    如何介绍项目
    二叉树的深度
    51单片机汇编指令手册
    SSM父子工程搭建
  • 原文地址:https://www.cnblogs.com/dawnWind/p/OpenCV_00.html
Copyright © 2011-2022 走看看