opencv

OpenCV裁剪图像任意区域

2018-08-17  本文已影响0人  kuizhu

给出区域轮廓点集,通过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;
}

结果如下

原图
截取区域
上一篇 下一篇

猜你喜欢

热点阅读