#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <stdio.h>
using namespace cv;
static void help(char** argv)
{
printf("\nThis program demonstrates OpenCV drawing and text output functions.\n"
"Usage:\n"
" %s\n", argv[0]);
}
//随机颜色
static Scalar randomColor(RNG& rng)
{
int icolor = (unsigned)rng;
return Scalar(icolor & 255, (icolor >> 8) & 255, (icolor >> 16) & 255);
}
int main(int /* argc */, char** argv)
{
help(argv);
char wndname[] = "Drawing Demo";
const int NUMBER = 100;
const int DELAY = 5;
int lineType = LINE_AA; //将其更改为 LINE_8 以查看非抗锯齿图形 change it to LINE_8 to see non-antialiased graphics
int i, width = 1000, height = 700;
int x1 = -width / 2, x2 = width * 3 / 2, y1 = -height / 2, y2 = height * 3 / 2;
RNG rng(0xFFFFFFFF);
Mat image = Mat::zeros(height, width, CV_8UC3);
imshow(wndname, image);//黑色背景
waitKey(DELAY);
for (i = 0; i < NUMBER * 2; i++)
{
Point pt1, pt2;
pt1.x = rng.uniform(x1, x2);
pt1.y = rng.uniform(y1, y2);
pt2.x = rng.uniform(x1, x2);
pt2.y = rng.uniform(y1, y2);
int arrowed = rng.uniform(0, 6);//是否带箭头
if (arrowed < 3)
line(image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), lineType);//随机颜色,随机线宽 直线
else
arrowedLine(image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), lineType);//随机颜色 随机线宽 带箭头直线
imshow(wndname, image);//更新显示
if (waitKey(DELAY) >= 0)//按键按下
return 0;//终止程序
}
for (i = 0; i < NUMBER * 2; i++)
{
Point pt1, pt2;
pt1.x = rng.uniform(x1, x2);
pt1.y = rng.uniform(y1, y2);
pt2.x = rng.uniform(x1, x2);
pt2.y = rng.uniform(y1, y2);
int thickness = rng.uniform(-3, 10);//线宽
int marker = rng.uniform(0, 10);//标记类型
int marker_size = rng.uniform(30, 80);//标记轴的长度 [默认 = 20 像素]
if (marker > 5)
rectangle(image, pt1, pt2, randomColor(rng), MAX(thickness, -1), lineType);//随机颜色 矩形
else
drawMarker(image, pt1, randomColor(rng), marker, marker_size);//在图像中的给定位置绘制一个标记
imshow(wndname, image);
if (waitKey(DELAY) >= 0)
return 0;
}
for (i = 0; i < NUMBER; i++)
{
Point center;//椭圆中心点坐标
center.x = rng.uniform(x1, x2);
center.y = rng.uniform(y1, y2);
Size axes;
axes.width = rng.uniform(0, 200);//长轴
axes.height = rng.uniform(0, 200);//短轴
double angle = rng.uniform(0, 180);//角度
//绘制椭圆
ellipse(image, center, axes, angle, angle - 100, angle + 200,
randomColor(rng), rng.uniform(-1, 9), lineType);//
imshow(wndname, image);
if (waitKey(DELAY) >= 0)
return 0;
}
for (i = 0; i < NUMBER; i++)
{
Point pt[2][3];
pt[0][0].x = rng.uniform(x1, x2);
pt[0][0].y = rng.uniform(y1, y2);
pt[0][1].x = rng.uniform(x1, x2);
pt[0][1].y = rng.uniform(y1, y2);
pt[0][2].x = rng.uniform(x1, x2);
pt[0][2].y = rng.uniform(y1, y2);
pt[1][0].x = rng.uniform(x1, x2);
pt[1][0].y = rng.uniform(y1, y2);
pt[1][1].x = rng.uniform(x1, x2);
pt[1][1].y = rng.uniform(y1, y2);
pt[1][2].x = rng.uniform(x1, x2);
pt[1][2].y = rng.uniform(y1, y2);
const Point* ppt[2] = { pt[0], pt[1] };//轮廓数组
int npt[] = { 3, 3 };//轮廓点数 数组
//多边形
polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1, 10), lineType);//三角形
imshow(wndname, image);
if (waitKey(DELAY) >= 0)
return 0;
}
for (i = 0; i < NUMBER; i++)
{
Point pt[2][3];
pt[0][0].x = rng.uniform(x1, x2);
pt[0][0].y = rng.uniform(y1, y2);
pt[0][1].x = rng.uniform(x1, x2);
pt[0][1].y = rng.uniform(y1, y2);
pt[0][2].x = rng.uniform(x1, x2);
pt[0][2].y = rng.uniform(y1, y2);
pt[1][0].x = rng.uniform(x1, x2);
pt[1][0].y = rng.uniform(y1, y2);
pt[1][1].x = rng.uniform(x1, x2);
pt[1][1].y = rng.uniform(y1, y2);
pt[1][2].x = rng.uniform(x1, x2);
pt[1][2].y = rng.uniform(y1, y2);
const Point* ppt[2] = { pt[0], pt[1] };
int npt[] = { 3, 3 };
//填充多边形
fillPoly(image, ppt, npt, 2, randomColor(rng), lineType);
imshow(wndname, image);
if (waitKey(DELAY) >= 0)
return 0;
}
for (i = 0; i < NUMBER; i++)
{
Point center;
center.x = rng.uniform(x1, x2);
center.y = rng.uniform(y1, y2);
//圆
circle(image, center, rng.uniform(0, 300), randomColor(rng),
rng.uniform(-1, 9), lineType);
imshow(wndname, image);
if (waitKey(DELAY) >= 0)
return 0;
}
for (i = 1; i < NUMBER; i++)
{
Point org;
org.x = rng.uniform(x1, x2);
org.y = rng.uniform(y1, y2);
//绘制文本
putText(image, "Testing text rendering", org, rng.uniform(0, 8),
rng.uniform(0, 100) * 0.05 + 0.1, randomColor(rng), rng.uniform(1, 10), lineType);
imshow(wndname, image);
if (waitKey(DELAY) >= 0)
return 0;
}
Size textsize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, 0);
Point org((width - textsize.width) / 2, (height - textsize.height) / 2);
Mat image2;
for (i = 0; i < 255; i += 2)
{
image2 = image - Scalar::all(i);//前景逐渐变暗消失
putText(image2, "OpenCV forever!", org, FONT_HERSHEY_COMPLEX, 3,
Scalar(i, i, 255), 5, lineType);//显示文本
imshow(wndname, image2);
if (waitKey(DELAY) >= 0)
return 0;
}
waitKey();
return 0;
}
【opencv450-samples】绘制图形和文本drawing.cpp
最新推荐文章于 2024-05-07 07:07:26 发布