#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main( int argc, char** argv ) {
char img_path[] = ".\\faces.png";
cv::Mat img = cv::imread(img_path, -1 );
if (img.empty())
{
std::cout << "\n" << "image null" << std::endl;
return -1;
}
// 画直线函数
Point p1(300, 300); // 点p1
Point p2(400, 400); // 点p2
line(img, Point(200, 300), Point(500, 500), Scalar(0, 255, 255), 2); // 黄色
line(img, p1, p2, Scalar(0, 0, 255), 2); // 红色
//画圆和点
Point p(20, 20);//初始化点坐标为(20,20)
circle(img, p, 50, Scalar(0, 255, 0), -1); // 画半径为1的圆(画点)
Point p4;
p4.x = 300;
p4.y = 300;
circle(img, p4, 100, Scalar(120, 120, 120), -1);
//椭圆
int thickness = 3;
int lineType = 8;
double angle = 30; //椭圆旋转角度
ellipse(img, Point(100, 100), Size(90, 60), angle, 0, 360, Scalar(255, 255, 0), thickness, lineType);
// //画矩形
Point p5;
p5.x = 600;
p5.y = 600;
Point p6;
p6.x = 800;
p6.y = 800;
Rect r(250, 250, 120, 200);
rectangle(img, r, Scalar(0, 255, 255), 3);
rectangle(img, p5, p6, Scalar(0, 255, 255), 3);
//设置绘制文本的相关参数
string text = "Hello World!";
int font_face = cv::FONT_HERSHEY_COMPLEX;
double font_scale = 3;
int thicknesss = 2;
int baseline;
//将文本框居中绘制
Size text_size = cv::getTextSize(text, font_face, font_scale, thicknesss, &baseline);
cout << text_size << endl;
//将文本框居中绘制
Point origin;
origin.x = img.cols / 2 - text_size.width / 2;
origin.y = img.rows / 2 + text_size.height / 2;
//origin.x = 300;
//origin.y = 400;
putText(img, text, origin, font_face, font_scale, cv::Scalar(0, 255, 255), thicknesss, 8, 0);
imshow("123", img);
waitKey();
cv::destroyWindow("Example 2-1");
return 0;
}