1、画多边形
#include<iostream>
#include<opencv2/opencv.hpp>
#define w 400
using namespace cv;
/// Function headers
void MyEllipse(Mat img, double angle);
void MyFilledCircle(Mat img, Point center);
void MyPolygon(Mat img);
VideoCapture VCap;//视频容器
int main(void) {
int NumP = 0;
/// Create black empty images
Mat image;
//= Mat::zeros(w, w, CV_8UC3);//生成一个400x400的矩阵,每个像素点占8bit,无符号整型,4通道图像
VCap.open(0);
if (!VCap.isOpened())//判断是否初始化/读取成功。与.open相关联(先后顺序)
return -1;//如是,关闭程序
Point rook_points[1][20];//创建坐标二维数组
rook_points[0][0] = Point(w / 4, 7 * w / 8);
rook_points[0][1] = Point(3 * w / 4, 7 * w / 8);
rook_points[0][2] = Point(3 * w / 4, 13 * w / 16);
rook_points[0][3] = Point(11 * w / 16, 13 * w / 16);
rook_points[0][4] = Point(19 * w / 32, 3 * w / 8);
rook_points[0][5] = Point(3 * w / 4, 3 * w / 8);
rook_points[0][6] = Point(3 * w / 4, w / 8);
rook_points[0][7] = Point(26 * w / 40, w / 8);
rook_points[0][8] = Point(26 * w / 40, w / 4);
rook_points[0][9] = Point(22 * w / 40, w / 4);
rook_points[0][10] = Point(22 * w / 40, w / 8);
rook_points[0][11] = Point(18 * w / 40, w / 8);
rook_points[0][12] = Point(18 * w / 40, w / 4);
rook_points[0][13] = Point(14 * w / 40, w / 4);
rook_points[0][14] = Point(14 * w / 40, w / 8);
rook_points[0][15] = Point(w / 4, w / 8);
rook_points[0][16] = Point(w / 4, 3 * w / 8);
rook_points[0][17] = Point(13 * w / 32, 3 * w / 8);
rook_points[0][18] = Point(5 * w / 16, 13 * w / 16);
rook_points[0][19] = Point(w / 4, 13 * w / 16);
const Point* ppt[1] = { rook_points[0] };
int npt[] = { 20 };//坐标点数总数量
while (1)
{
VCap.read(image);
polylines(
image,
ppt,
npt,
1,
1,
Scalar(0, 0, 0),
8
);
if (NumP >= 500)NumP = 0;
imshow("画多边形", image);
waitKey(33);
}
waitKey(90000);
return(0);
}
2、填充多边形
#include<iostream>
#include<opencv2/opencv.hpp>
#define w 400
using namespace cv;
/// Function headers
void MyEllipse(Mat img, double angle);
void MyFilledCircle(Mat img, Point center);
void MyPolygon(Mat img);
VideoCapture VCap;//视频容器
int main(void) {
int NumP = 0;
/// Create black empty images
Mat image;
//= Mat::zeros(w, w, CV_8UC3);//生成一个400x400的矩阵,每个像素点占8bit,无符号整型,4通道图像
VCap.open(0);
if (!VCap.isOpened())//判断是否初始化/读取成功。与.open相关联(先后顺序)
return -1;//如是,关闭程序
Point rook_points[1][20];
rook_points[0][0] = Point(w / 4, 7 * w / 8);
rook_points[0][1] = Point(3 * w / 4, 7 * w / 8);
rook_points[0][2] = Point(3 * w / 4, 13 * w / 16);
rook_points[0][3] = Point(11 * w / 16, 13 * w / 16);
rook_points[0][4] = Point(19 * w / 32, 3 * w / 8);
rook_points[0][5] = Point(3 * w / 4, 3 * w / 8);
rook_points[0][6] = Point(3 * w / 4, w / 8);
rook_points[0][7] = Point(26 * w / 40, w / 8);
rook_points[0][8] = Point(26 * w / 40, w / 4);
rook_points[0][9] = Point(22 * w / 40, w / 4);
rook_points[0][10] = Point(22 * w / 40, w / 8);
rook_points[0][11] = Point(18 * w / 40, w / 8);
rook_points[0][12] = Point(18 * w / 40, w / 4);
rook_points[0][13] = Point(14 * w / 40, w / 4);
rook_points[0][14] = Point(14 * w / 40, w / 8);
rook_points[0][15] = Point(w / 4, w / 8);
rook_points[0][16] = Point(w / 4, 3 * w / 8);
rook_points[0][17] = Point(13 * w / 32, 3 * w / 8);
rook_points[0][18] = Point(5 * w / 16, 13 * w / 16);
rook_points[0][19] = Point(w / 4, 13 * w / 16);
const Point* ppt[1] = { rook_points[0] };
int npt[] = { 20 };
while (1)
{
VCap.read(image);
fillPoly(image,
ppt,
npt,
1,
Scalar(255, 100, 50),
8);
if (NumP >= 500)NumP = 0;
imshow("填充多边形", image);
waitKey(33);
}
waitKey(90000);
return(0);
}
3、绘制文字
#include<iostream>
#include<opencv2/opencv.hpp>
#define w 400
using namespace cv;
/// Function headers
void MyEllipse(Mat img, double angle);
void MyFilledCircle(Mat img, Point center);
void MyPolygon(Mat img);
VideoCapture VCap;//视频容器
int main(void) {
int NumP = 0;
/// Create black empty images
Mat image;
//= Mat::zeros(w, w, CV_8UC3);//生成一个400x400的矩阵,每个像素点占8bit,无符号整型,4通道图像
VCap.open(0);
if (!VCap.isOpened())//判断是否初始化/读取成功。与.open相关联(先后顺序)
return -1;//如是,关闭程序
Point rook_points[1][20];
rook_points[0][0] = Point(w / 4, 7 * w / 8);
rook_points[0][1] = Point(3 * w / 4, 7 * w / 8);
rook_points[0][2] = Point(3 * w / 4, 13 * w / 16);
rook_points[0][3] = Point(11 * w / 16, 13 * w / 16);
rook_points[0][4] = Point(19 * w / 32, 3 * w / 8);
rook_points[0][5] = Point(3 * w / 4, 3 * w / 8);
rook_points[0][6] = Point(3 * w / 4, w / 8);
rook_points[0][7] = Point(26 * w / 40, w / 8);
rook_points[0][8] = Point(26 * w / 40, w / 4);
rook_points[0][9] = Point(22 * w / 40, w / 4);
rook_points[0][10] = Point(22 * w / 40, w / 8);
rook_points[0][11] = Point(18 * w / 40, w / 8);
rook_points[0][12] = Point(18 * w / 40, w / 4);
rook_points[0][13] = Point(14 * w / 40, w / 4);
rook_points[0][14] = Point(14 * w / 40, w / 8);
rook_points[0][15] = Point(w / 4, w / 8);
rook_points[0][16] = Point(w / 4, 3 * w / 8);
rook_points[0][17] = Point(13 * w / 32, 3 * w / 8);
rook_points[0][18] = Point(5 * w / 16, 13 * w / 16);
rook_points[0][19] = Point(w / 4, 13 * w / 16);
const Point* ppt[1] = { rook_points[0] };
int npt[] = { 20 };
while (1)
{
VCap.read(image);
fillPoly(image,
ppt,
npt,
1,
Scalar(255, 100, 50),
8);
putText(image,//绘制文字
"King",
Point(80, 30),
FONT_HERSHEY_SCRIPT_COMPLEX,//字型(见上表)
1,
Scalar(0, 0, 0),
1,
8
);
if (NumP >= 500)NumP = 0;
imshow("绘制文字", image);
waitKey(33);
}
waitKey(90000);
return(0);
}
3.1、显示数据
char FPS[10] = {'F','P','S',':'};
char ac=0;
while (1)
{
VCap.set(CV_CAP_PROP_BRIGHTNESS,1);//亮度 1
VCap.set(CV_CAP_PROP_SATURATION, 50);//饱和度 50
ac++;
if (ac >=99)
{
ac = 0;
}
FPS[4] = ac / 10 + 48;
FPS[5] = ac % 10 + 48;
VCap.read(image);
putText(image,
FPS,//需要显示的数据
Point(80, 30),
FONT_HERSHEY_SIMPLEX,
1,
Scalar(0, 0, 0),
1,
8
);
if (NumP >= 500)NumP = 0;
imshow("显示数据", image);
waitKey(30);