一 、函数接口介绍
1.1 fillPoly函数
这是个重载函数,有2个实现,具体如下:
1、重载1
void fillPoly(Mat& img,
const Point** pts,
const int* npts,
int ncontours,
const Scalar& color,
int lineType=8,
int shift=0,
Point offset=Point()
);
/*
*img:待绘制的图像
*pts:多边形数组指针,比如Point p[2][5]表示2个轮廓,每个轮廓由5个点组成
*npts:每一个多边形顶点个数的数组,比如int n[] = {5, 5};
*ncontours:要绘制多边形的个数
*color:颜色
*lineType:边界线条颜色
*shift:顶点坐标小数点的位数
*offset:所有顶点的偏移量
*/
2、重载2
void cv::fillPoly (InputOutputArray img,
InputArrayOfArrays pts,
const Scalar & color,
int lineType = LINE_8,
int shift = 0,
Point offset = Point()
)
/*
*img:Mat类型的待绘制图像
*pts:指std::vector<std::vectorcv::Point>类型
*/
1.2 polylines函数
这个与fillPoly区别在于它只绘制边界线,也是重载函数,有2个实现,具体如下
1、重载1
void cv::polylines (InputOutputArray img,
const Point *const * pts,
const int * npts,
int ncontours,
bool isClosed,
const Scalar & color,
int thickness = 1,
int lineType = LINE_8,
int shift = 0
)
/*
*isClosed:是否是封闭曲线的标志位,即首尾是否要相连
*/
2、重载2
void cv::polylines (InputOutputArray img,
InputArrayOfArrays pts,
bool isClosed,
const Scalar color,
int thickness = 1,
int lineType = LINE_8,
int shift = 0
)
二、代码演示
2.1 fillPoly函数
1、重载1
#include <opencv2/opencv.hpp>
int main() {
// 创建一个黑色图像
cv::Mat img = cv::Mat::zeros(500, 500, CV_8UC3);
// 定义第一个多边形(三角形)
cv::Point triangle[3];
triangle[0] = cv::Point(100, 100);
triangle[1] = cv::Point(200, 100);
triangle[2] = cv::Point(150, 200);
// 定义第二个多边形(四边形)
cv::Point quadrilateral[4];
quadrilateral[0] = cv::Point(300, 300);
quadrilateral[1] = cv::Point(400, 300);
quadrilateral[2] = cv::Point(400, 400);
quadrilateral[3] = cv::Point(300, 400);
// 多边形顶点数组
const cv::Point* pts[2] = { triangle, quadrilateral };
// 每个多边形的顶点数量
int npts[2] = { 3, 4 };
// 使用 fillPoly 在图像上填充多边形
cv::fillPoly(img, pts, npts, 2, cv::Scalar(0, 255, 0), cv::LINE_8);
// 显示结果
cv::imshow("Polygons", img);
cv::waitKey(0);
return 0;
}
2、重载2
#include <opencv2/opencv.hpp>
#include <vector>
int main() {
// Create a black image
cv::Mat img = cv::Mat::zeros(500, 500, CV_8UC3);
// Define the first polygon (a triangle)
std::vector<cv::Point> triangle;
triangle.push_back(cv::Point(100, 100));
triangle.push_back(cv::Point(200, 100));
triangle.push_back(cv::Point(150, 200));
// Define the second polygon (a quadrilateral)
std::vector<cv::Point> quadrilateral;
quadrilateral.push_back(cv::Point(300, 300));
quadrilateral.push_back(cv::Point(400, 300));
quadrilateral.push_back(cv::Point(400, 400));
quadrilateral.push_back(cv::Point(300, 400));
// Combine the polygons into a single vector of vectors
std::vector<std::vector<cv::Point>> polygons;
polygons.push_back(triangle);
polygons.push_back(quadrilateral);
// Use fillPoly to fill the polygons on the image
cv::fillPoly(img, polygons, cv::Scalar(0, 255, 0), cv::LINE_8);
// Display the result
cv::imshow("Polygons", img);
cv::waitKey(0);
return 0;
}
2.2 polylines函数
1、重载1
#include <opencv2/opencv.hpp>
int main() {
// 创建一个黑色图像
cv::Mat img = cv::Mat::zeros(500, 500, CV_8UC3);
// 定义第一个多边形(三角形)
cv::Point triangle[3];
triangle[0] = cv::Point(100, 100);
triangle[1] = cv::Point(200, 100);
triangle[2] = cv::Point(150, 200);
// 定义第二个多边形(四边形)
cv::Point quadrilateral[4];
quadrilateral[0] = cv::Point(300, 300);
quadrilateral[1] = cv::Point(400, 300);
quadrilateral[2] = cv::Point(400, 400);
quadrilateral[3] = cv::Point(300, 400);
// 多边形顶点数组
const cv::Point* pts[2] = { triangle, quadrilateral };
// 每个多边形的顶点数量
int npts[2] = { 3, 4 };
// 使用 polylines 在图像上绘制多边形边框
cv::polylines(img, pts, npts, 2, true, cv::Scalar(0, 255, 0), 2, cv::LINE_8);
// 显示结果
cv::imshow("Polylines", img);
cv::waitKey(0);
return 0;
}
2、重载2
#include <opencv2/opencv.hpp>
#include <vector>
int main() {
// 创建一个黑色图像
cv::Mat img = cv::Mat::zeros(500, 500, CV_8UC3);
// 定义第一个多边形(三角形)
std::vector<cv::Point> triangle;
triangle.push_back(cv::Point(100, 100));
triangle.push_back(cv::Point(200, 100));
triangle.push_back(cv::Point(150, 200));
// 定义第二个多边形(四边形)
std::vector<cv::Point> quadrilateral;
quadrilateral.push_back(cv::Point(300, 300));
quadrilateral.push_back(cv::Point(400, 300));
quadrilateral.push_back(cv::Point(400, 400));
quadrilateral.push_back(cv::Point(300, 400));
// 将多边形组合到一个矢量的矢量中
std::vector<std::vector<cv::Point>> polygons;
polygons.push_back(triangle);
polygons.push_back(quadrilateral);
// 使用 polylines 在图像上绘制多边形边框
cv::polylines(img, polygons, true, cv::Scalar(0, 255, 0), 2, cv::LINE_8);
// 显示结果
cv::imshow("Polylines", img);
cv::waitKey(0);
return 0;
}
标签:triangle,img,Point,int,fillPoly,quadrilateral,OpenCV,polylines,cv
From: https://www.cnblogs.com/Terrypython/p/18251734