opencv处理不规则多边形ROI

来源:互联网 发布:算法第四版pdf 图灵 编辑:程序博客网 时间:2024/04/29 11:24

使用到的opencv函数

  • fillpoly函数
  • polylines函数

程序实例

#include <opencv2/opencv.hpp>#include <iostream>using namespace std;using namespace cv;int main(){    Mat img = imread("1.png");    Point root_points[1][6];      root_points[0][0] = Point(5,5);      root_points[0][1] = Point(img.cols,5);      root_points[0][2] = Point(img.cols,225);      root_points[0][3] = Point(465,225);    root_points[0][4] = Point(215,220);    root_points[0][5] = Point(5,220);    const Point* ppt[1] = {root_points[0]};      int npt[] = {6};      polylines(img, ppt, npt, 1, 1, Scalar(255),1,8,0);      //imshow("Test", img);     // waitKey();      fillPoly(img, ppt, npt, 1, Scalar(255,255,255));      ///////////对下方的区域外的像素进行赋值为0    Point root_points_1[1][8];      root_points_1[0][0] = Point(5,220);      root_points_1[0][1] = Point(215,220);      root_points_1[0][2] = Point(235,465);      root_points_1[0][3] = Point(466,450);    root_points_1[0][4] = Point(460,225);    root_points_1[0][5] = Point(img.cols,225);    root_points_1[0][6] = Point(img.cols,img.rows);    root_points_1[0][7] = Point(0,img.rows);    const Point* ppt_1[1] = {root_points_1[0]};      int npt_1[] = {8};      polylines(img, ppt_1, npt_1, 1, 1, Scalar(255),1,8,0);      //imshow("Test", img);     // waitKey();      fillPoly(img, ppt_1, npt_1, 1, Scalar(255,255,255));      imshow("Test_img", img);      waitKey();      //imshow("img",img);    //waitKey(0);    return 0;}

处理结果

这里写图片描述

这里写图片描述

总结

对特定区域像素值保持原状,特定区域之外像素值变化。

0 0
原创粉丝点击