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);
  • 相关阅读:
    Bonding
    负载均衡
    XML
    wireshark
    IE
    轨迹系列7——Socket总结及实现基于TCP或UDP的809协议方法
    轨迹系列6——车载GPS对接方案汇总小结(809、自定义协议、前置库、WS)
    基于R树索引的点面关系判断以及效率优化统计
    WebGIS中以version方式实现代码更新后前端自动读取更新代码的方法
    轨迹系列5——验证轨迹GPS坐标转换为本地坐标的四/七参数是否准确的一种方案
  • 原文地址:https://www.cnblogs.com/dawnWind/p/OpenCV_00.html
Copyright © 2011-2022 走看看