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);