open cv绘制形状与文字
使用cv:Point cv::Scalar
绘制线、矩形、园、椭圆等基本几何形状
随机生成与绘制文本
使用cv::Point与cv::Scalar
Point表示2D平面上 一个点
Point p;
p.x= 10;
p.y= 8;
or
p= Pont(10,8);
Scalar表示三个元素的向量
Scalar(a, b, c); a= blue,b= green,c= red表示RGB三个通道
Scalar all(-1) 某些函数中的随机颜色
绘制线、矩形、园、椭圆等基本几何形状的API
●画线:cv::line(src, p1, p2, color, 3, LINE_AA) 线型 LINE_4\LINE _8\LINE_AA )AA反锯齿
●画椭圆cv::ellipse
●画矩形cv::rectangle
●画圆cv::circle
●画填充cv::fillpoly
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <iostream>
using namespace cv;
using namespace std;
void lines();
void rectangle();
void Ellipse();
void Circle();
void polygon();
void RandomLineDemo();
Mat src;
Mat dst;
int main()
{
src = imread("D:/实验台/机器视觉/测试图片/白人女.png");
if (!src.data)
{
cout << "加载失败" << endl;
return -1;
}
printf("rows=%d", src.rows);
printf("cols=%d", src.cols);
//lines();
//rectangle();
//Ellipse();
Circle();
//polygon();
//RandomLineDemo();
//在屏幕上点Point处打印文字"hello world",字体类型为FONT_HERSHEY_COMPLEX,放缩程度为1.0,后3个参数为颜色,线宽,类型
putText(src, "hello world", Point(157, 250), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(25, 25, 255), 2, 8);
// 字体坐标 字体样式 大小 线宽 线类型
//imshow("原图", src);
dst = src.clone();
//imshow("对比",dst);
waitKey(0);//默认按任何键退出
return 0;
}
void lines()//绘制线段
{
Point p1 = Point(100, 180);//起点的方位 20 x点 30y点
Point p2;//终点的方位
p2.x = 398;
p2.y = 475;
Scalar color = Scalar(0, 0, 255);//设置颜色为红色
line(src, p1, p2, color, 3, LINE_AA);//cpu效率低不要用反锯齿
// 3 为线条的粗细
//LINE_AA线条的类型
}
void rectangle()//绘制矩形图框
{
Rect rect(100, 180, 300, 300);
//100 宽x的起点 180 高y的起点 300*300宽高的矩形
Scalar color = Scalar(255, 0, 0);
rectangle(src, rect, color, 2, LINE_AA);
}
void Ellipse()//绘制椭圆
{
Scalar color = Scalar(0, 255, 0);
ellipse(src, Point(src.rows / 2, src.cols / 2), Size(src.rows / 4, src.cols / 8), 90, 0, 360, color, 2, LINE_AA);
// src.rows / 2 中心点方位
// src.rows / 2 中心点方位
// src.rows / 4 x宽轴以中心点为中心的拉伸距离
// src.rows / 8 y高轴以中心点为中心的拉伸距离
//90 倾斜角度为90°
//0,360 显示范围为0 - 360° 可用来画弧
}
/*
void Circle()//绘制圆形
{
Scalar color = Scalar(0, 255, 255);//设置为黄色
Point center = Point(src.rows / 2, src.cols / 2);//设置中心点
circle(src, center, 50, color, 2, LINE_AA);//50为圆的半径
}*/
void Circle()//绘制圆形
{
RNG rng(12345);
Point pt1, pt2;
for (int i = 0; i < 500; i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));//设置为黄色
pt1.x = rng.uniform(0,src.rows);
pt1.y = rng.uniform(0, src.cols);
pt2.x = rng.uniform(0, src.rows);
pt2.y = rng.uniform(0, src.cols);
Point center = (pt1, pt2);//设置中心点
circle(src, center, 5, color, 2, LINE_AA);//50为圆的半径
imshow("src",src);
if ((char)waitKey(50) == 'A')
{
break;
}
}
}
void polygon()//绘制多边形
{
Point pts[2][5];
//声明多边形的点的坐标point
//声明填充范围的坐标为二维数组
//第一维度代表填充范围 第二维度代表该范围的临界点
//最终传出为指向某一个填充范围的指针
pts[0][0] = Point(100, 100); //左上起点的坐标
pts[0][1] = Point(100, 200);//左下起点的坐标
pts[0][2] = Point(200, 200);//右下起点的坐标
pts[0][3] = Point(200, 100);//右上起点的坐标
pts[0][4] = Point(100, 100);//重复第一个坐标
pts[1][0] = Point(300, 300);
pts[1][1] = Point(300, 400);
pts[1][2] = Point(400, 400);
pts[1][3] = Point(400, 300);
pts[1][4] = Point(300, 300);
const Point* ppts[] = { pts[0],pts[1] };///指向第0个轮廓的指针
int npt[] = { 5,5 };//数组中存放每一个轮廓的轮廓点数
Scalar color = Scalar(255, 15, 255);
fillPoly(src, ppts, npt, 2, color, LINE_AA);
//ppts中存放着这个轮廓信息的位置
//npt代表该轮廓有几个轮廓点
//2表示需要显示几个图形
}
void RandomLineDemo()//绘制随机线条
{
RNG rng(12345);
Point pt1, pt2;
for (int i = 0; i < 500; i++)
{
pt1.x = rng.uniform(0,src.rows);
pt1.y = rng.uniform(0, src.cols);
pt2.x = rng.uniform(0, src.rows);
pt2.y = rng.uniform(0, src.cols);
Scalar color = Scalar(rng.uniform(0, 255),rng.uniform(0, 255),rng.uniform(0, 255));
line(src,pt1,pt2,color,2,LINE_8);
if ((char)waitKey(50) == 'A') { break; };
imshow("随即线条", src);
}
}