OpenCV裁剪图像任意区域

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


简要代码如下

#include
#include
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> contours;
    vector 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裁剪图像任意区域)