美文网首页opencv
OpenCV裁剪图像任意区域

OpenCV裁剪图像任意区域

作者: kuizhu | 来源:发表于2018-08-17 09:32 被阅读0次

    给出区域轮廓点集,通过drawContours函数填充区域,生成mask图像,与原图相与


    简要代码如下

    #include<iostream>
    #include<opencv2\opencv.hpp>
    using namespace std;
    using namespace cv;
    
    int main()
    {
        Mat src = imread("F:/testdata/input.png");
        Mat mask = Mat::zeros(src.size(), CV_8UC1);
        Mat dst;
        vector<vector<Point2i>> contours;
        vector<Point2i> points;
        points.push_back(Point2i(100, 100));
        points.push_back(Point2i(50, 150));
        points.push_back(Point2i(100, 200));
        points.push_back(Point2i(200, 200));
        points.push_back(Point2i(220, 150));
        points.push_back(Point2i(200, 100));
        contours.push_back(points);
        drawContours(mask, contours, 0, Scalar::all(255), -1);
        imshow("src", src);
        src.copyTo(dst, mask);
        imshow("dst", dst);
        waitKey(0);
        return 0;
    }
    

    结果如下

    原图
    截取区域

    相关文章

      网友评论

        本文标题:OpenCV裁剪图像任意区域

        本文链接:https://www.haomeiwen.com/subject/vfczbftx.html