1、使用cv::Point与cv::Scalar
- Point表示2D平面上一个点(x,y)
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三个通道
2、绘制线、矩形、圆、椭圆等基本几何形状
(1)画线:
画线 cv::line (LINE_4\LINE_8\LINE_AA)//几种类型
其中 LINE_AA 为反锯齿类型
画线:line(src,p1,p2,color,1,LINE_8)
第一个参数表示在画线的图像 ,第二个参数表示线的起始点,第三个参数表示线的终点,第四个参数表示线的颜色,第五个参数表示线宽,第六个参数表示线的类型,p1和p2的值通过point函数设置,color=Scalar(a,b,c)设置三通道的颜色。
画直线函数 cv::line(背景图片,起点,终点, 线条颜色,线条粗细,线条形状,坐标点的小数点位数(可略))
其代码实现如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src;//全局变量
const char* dreawdeno_xin = "draw shapes and text demo";
void MyLines();//画线
int main(int argc, char** argv) {
src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
if (!src.data) {
cout << "could not load image..4." << endl;
return -1;
}
MyLines();
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
waitKey(0);
return 0;
}
void MyLines()//画线
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 400;
p2.y = 400;
Scalar color = Scalar(0, 0, 255);//red
line(src, p1, p2, color, 1, LINE_8);
}
其运行结果为:
(2)画矩形:
- 画矩形cv::rectangle//用法类似于画线
rectangle(src, rect, color, 2, LINE_8);
画矩形函数 cv::rectangle(背景图片,矩形区域, 线条颜色,线条粗细,线条形状)
- Rect函数参数列表如下:
Rect(int _x,int _y,int _width,int _height);
int_x和int_y: 代表左上角点的坐标。
int_width和int_height:代表需要裁剪区域的尺寸 。
- 代码实现如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src;//全局变量
const char* dreawdeno_xin = "draw shapes and text demo";
void MyLines();//画线
void MyRectangle();//画矩形
int main(int argc, char** argv) {
src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
if (!src.data) {
cout << "could not load image..4." << endl;
return -1;
}
MyLines();
MyRectangle();
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
waitKey(0);
return 0;
}
void MyLines()//画线
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 400;
p2.y = 400;
Scalar color = Scalar(0, 0, 255);//red
line(src, p1, p2, color, 1, LINE_8);
}
void MyRectangle() //画矩形
{
Rect rect = Rect(100, 200, 300, 300);
Scalar color = Scalar(255, 0, 0);//blue
rectangle(src, rect, color, 2, LINE_8);
}
其运行结果为:
(3)画椭圆:
画椭圆函数
cv::ellipse(
img 背景图像
center 椭圆圆心坐标
axes 轴的长度
angle 偏转的角度(即倾斜角度)
start_angle 圆弧起始角的角度
end_angle 圆弧终结角的角度
color 线条颜色
thickness 线条粗细
line_type 线条形状
shift 圆心坐标点和数轴的精度)
例子:
ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 90, 0, 360, color, 2, LINE_8);
代码实现:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src;//全局变量
const char* dreawdeno_xin = "draw shapes and text demo";
void MyLines();//画线
void MyRectangle();//画矩形
void MyEllipse();//画椭圆
int main(int argc, char** argv) {
src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
if (!src.data) {
cout << "could not load image..4." << endl;
return -1;
}
MyLines();
MyRectangle();
MyEllipse();
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
waitKey(0);
return 0;
}
void MyLines()//画线
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 400;
p2.y = 400;
Scalar color = Scalar(0, 0, 255);//red
line(src, p1, p2, color, 1, LINE_8);
}
void MyRectangle() //画矩形
{
Rect rect = Rect(100, 200, 300, 300);
Scalar color = Scalar(255, 0, 0);//blue
rectangle(src, rect, color, 2, LINE_8);
}
void MyEllipse() //画椭圆
{
Scalar color = Scalar(0, 255, 0);
ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 90, 0, 360, color, 2, LINE_8);
}
其运行结果是:
(4)画圆:
- 画圆函数
cv::circle(
img 背景图像
center 椭圆圆心坐标
圆的半径
color 线条颜色
线条粗细
line_type 线条形状
shift 圆心坐标点和数轴的精度)
)
例子:
circle(src, center, 150, color, 2, 8);
- 代码实现:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src;//全局变量
const char* dreawdeno_xin = "draw shapes and text demo";
void MyLines();//画线
void MyRectangle();//画矩形
void MyEllipse();//画椭圆
void MyCircle();//画圆
int main(int argc, char** argv) {
src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
if (!src.data) {
cout << "could not load image..4." << endl;
return -1;
}
MyLines();
MyRectangle();
MyEllipse();
MyCircle();
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
waitKey(0);
return 0;
}
void MyLines()//画线
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 400;
p2.y = 400;
Scalar color = Scalar(0, 0, 255);//red
line(src, p1, p2, color, 1, LINE_8);
}
void MyRectangle() //画矩形
{
Rect rect = Rect(100, 200, 300, 300);
Scalar color = Scalar(255, 0, 0);//blue
rectangle(src, rect, color, 2, LINE_8);
}
void MyEllipse() //画椭圆
{
Scalar color = Scalar(0, 255, 0);
ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 45, 0, 360, color, 2, LINE_8);
}
void MyCircle() //画圆
{
Scalar color = Scalar(0, 255, 255);
Point center = Point(src.cols / 2, src.rows / 2);
circle(src, center, 150, color, 2, 8);
}
其运行结果为:
(5)填充多边形
- 绘制多边形函数
cv::fillPoly(
image 背景图片
pts数组存放每个多边形的顶点集合
npts 数组存放每个多边形的顶点点数
ncontours 要绘制多边形的数量
color 线条颜色
line_type 线条形状
shift 圆心坐标点和数轴的精度
offset
)
例子:
fillPoly(src, ppts, npt, 1, color, 8);
代码实现:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src;//全局变量
const char* dreawdeno_xin = "draw shapes and text demo";
void MyLines();//画线
void MyRectangle();//画矩形
void MyEllipse();//画椭圆
void MyCircle();//画圆
void MyPolygon();//填充多边形
int main(int argc, char** argv) {
src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
if (!src.data) {
cout << "could not load image..4." << endl;
return -1;
}
MyLines();
MyRectangle();
MyEllipse();
MyCircle();
MyPolygon();
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
waitKey(0);
return 0;
}
void MyLines()//画线
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 400;
p2.y = 400;
Scalar color = Scalar(0, 0, 255);//red
line(src, p1, p2, color, 1, LINE_8);
}
void MyRectangle() //画矩形
{
Rect rect = Rect(100, 200, 300, 300);
Scalar color = Scalar(255, 0, 0);//blue
rectangle(src, rect, color, 2, LINE_8);
}
void MyEllipse() //画椭圆
{
Scalar color = Scalar(0, 255, 0);
ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 45, 0, 360, color, 2, LINE_8);
}
void MyCircle() //画圆
{
Scalar color = Scalar(0, 255, 255);
Point center = Point(src.cols / 2, src.rows / 2);
circle(src, center, 150, color, 2, 8);
}
void MyPolygon() //填充多边形
{
Point pts[1][5];//二维
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);
const Point* ppts[] = { pts[0] };
int npt[] = { 5 };
Scalar color = Scalar(255, 12, 255);
fillPoly(src, ppts, npt, 1, color, 8);
}
其运行结果为:
3、插入文字
-插入文字的函数:
void cv::putText(
cv::Mat& img, // 待绘制的图像
const string& text, // 待绘制的文字
cv::Point origin, // 文本框的左下角
int fontFace, // 字体 (如cv::FONT_HERSHEY_PLAIN)
double fontScale, // 尺寸因子,值越大文字越大
cv::Scalar color, // 线条的颜色(RGB)
int thickness = 1, // 线条宽度
int lineType = 8, // 线型(4邻域或8邻域,默认8邻域)
bool bottomLeftOrigin = false // true='origin at lower left'
);
例子:
putText(src, "Hello OpenCV", Point(300, 300), FONT_HERSHEY_COMPLEX, 1.0, Scalar(12, 23, 200), 3, 8);
代码实现:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src;//全局变量
const char* dreawdeno_xin = "draw shapes and text demo";
void MyLines();//画线
void MyRectangle();//画矩形
void MyEllipse();//画椭圆
void MyCircle();//画圆
void MyPolygon();//填充多边形
int main(int argc, char** argv) {
src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
if (!src.data) {
cout << "could not load image..4." << endl;
return -1;
}
MyLines();
MyRectangle();
MyEllipse();
MyCircle();
MyPolygon();
//添加文字
putText(src, "Hello OpenCV", Point(300, 300), FONT_HERSHEY_COMPLEX, 1.0, Scalar(12, 23, 200), 3, 8);
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
waitKey(0);
return 0;
}
void MyLines()//画线
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 400;
p2.y = 400;
Scalar color = Scalar(0, 0, 255);//red
line(src, p1, p2, color, 1, LINE_8);
}
void MyRectangle() //画矩形
{
Rect rect = Rect(100, 200, 300, 300);
Scalar color = Scalar(255, 0, 0);//blue
rectangle(src, rect, color, 2, LINE_8);
}
void MyEllipse() //画椭圆
{
Scalar color = Scalar(0, 255, 0);
ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 45, 0, 360, color, 2, LINE_8);
}
void MyCircle() //画圆
{
Scalar color = Scalar(0, 255, 255);
Point center = Point(src.cols / 2, src.rows / 2);
circle(src, center, 150, color, 2, 8);
}
void MyPolygon() //填充多边形
{
Point pts[1][5];//二维
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, 300);
const Point* ppts[] = { pts[0] };
int npt[] = { 5 };
Scalar color = Scalar(255, 12, 255);
fillPoly(src, ppts, npt, 1, color, 8);
}
其运行结果为:
4、随机画直线
- 产生随机数
- 生成高斯随机数gaussian (double sigma)
生成正态分布随机数uniform (int a, int b)
RNG rng(12345);
RNG::uniform
int RNG::(int a,int b)
float RNG::uniform(float a,float b)
double RNG::uniform(double a,double b)
随机类RNG:计算机的伪随机数是由随机种子根据一定的计算方法计算出来的数值,所以只要计算方法一定,随机种子一定,那么产生的随机数就是固定的。
RNG rng(12345)
opencv 里RNG类构造函数初始化为固定值后,随机种子也是固定的,所以在相同的平台环境下,编译后每次运行它,显示的随机数是一样的。
例如:
RNG rng(12345);
int main(void)
{
for (int i = 0; i < 10; i++) {
int a = rng.uniform(1, 100);
cout << a << endl;
}
return 0;
}
只要平台不变,每次生成的都是:
6
33
12
54
71
65
94
11
64
76
如果想改变成随机生成的数,使用下面的方法:
RNG rng((unsigned)time(NULL));
int main(void)
{
for (int i = 0; i < 10; i++) {
int a = rng.uniform(1, 100);
cout << a << endl;
}
return 0;
}
第一次结果:
76
79
3
3
83
2
34
65
97
9
第二次结果:
35
46
76
52
32
68
69
42
49
91
代码实现:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src;//全局变量
const char* dreawdeno_xin = "draw shapes and text demo";
void MyLines();//画线
void MyRectangle();//画矩形
void MyEllipse();//画椭圆
void MyCircle();//画圆
void MyPolygon();//填充多边形
void RandomLineDemo();//随机
int main(int argc, char** argv) {
src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4_1.jpg");
if (!src.data) {
cout << "could not load image..4." << endl;
return -1;
}
//MyLines();
//MyRectangle();
//MyEllipse();
//MyCircle();
//MyPolygon();
//putText(src, "Hello OpenCV", Point(100, 300), FONT_HERSHEY_COMPLEX, 1.0, Scalar(12, 23, 200), 3, 8);
char input_win[] = "input image";
namedWindow(input_win, WINDOW_AUTOSIZE);
imshow(input_win, src);
//随机画
RandomLineDemo();
waitKey(0);
return 0;
}
void MyLines()//画线
{
Point p1 = Point(20, 30);
Point p2;
p2.x = 400;
p2.y = 400;
Scalar color = Scalar(0, 0, 255);//red
line(src, p1, p2, color, 1, LINE_8);
}
void MyRectangle() //画矩形
{
Rect rect = Rect(100, 200, 300, 300);
Scalar color = Scalar(255, 0, 0);//blue
rectangle(src, rect, color, 2, LINE_8);
}
void MyEllipse() //画椭圆
{
Scalar color = Scalar(0, 255, 0);
ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 45, 0, 360, color, 2, LINE_8);
}
void MyCircle() //画圆
{
Scalar color = Scalar(0, 255, 255);
Point center = Point(src.cols / 2, src.rows / 2);
circle(src, center, 150, color, 2, 8);
}
void MyPolygon() //填充多边形
{
Point pts[1][5];//二维
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);
const Point* ppts[] = { pts[0] };
int npt[] = { 5 };
Scalar color = Scalar(255, 12, 255);
fillPoly(src, ppts, npt, 1, color, 8);
}
void RandomLineDemo() //随机
{
RNG rng(12345);//随机数生成
Point pt1;
Point pt2;
Mat bg = Mat::zeros(src.size(), src.type());//重新生成一张空图
namedWindow("random line demo", WINDOW_AUTOSIZE);
for (int i = 0; i < 100000; i++) {
pt1.x = rng.uniform(0, src.cols);
pt2.x = rng.uniform(0, src.cols);
pt1.y = rng.uniform(0, src.rows);
pt2.y = rng.uniform(0, src.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50) > 0) //输入数据大于0则停止运行
{
break;
}
line(bg, pt1, pt2, color, 1, 8);
imshow("random line demo", bg);
}
}
其运行结果为: