使用OpenCV画直线、圆、椭圆、矩形以及添加文字
闲话少说,直接上代码
#include <QCoreApplication>
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void drawLine(Mat &image);
void drawRectangle(Mat &image);
void drawEllipse(Mat &image);
void drawCricle(Mat &image);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Mat image1=imread("/home/hf1/Python_code/animations.jpg");
drawLine(image1);
drawRectangle(image1);
drawEllipse(image1);
drawCricle(image1);
putText(image1,"seam",Point(10,300),FONT_HERSHEY_SIMPLEX,2,Scalar(255,0,0),3,LINE_AA);//添加文字和设置字体、线宽
imshow("Image",image1);
waitKey(0);
return a.exec();
}
void drawLine(Mat& image)
{
Point p1(200,300);//端点
Point p2(600,360);;//端点
Scalar color = Scalar(255,0,0);//颜色
line(image,p1,p2,color,3,LINE_AA);
}
void drawRectangle(Mat& image)
{
Rect rect =Rect(100,200,150,150);//100和200是矩形的起点,150是边长
Scalar color = Scalar(255,255,0);//颜色
rectangle(image,rect,color,1,LINE_AA);
}
void drawEllipse(Mat &image)
{
Scalar color =Scalar(0,0,255);//颜色
ellipse(image,Point(image.cols/2,image.rows/2),Size(image.cols/2,image.rows/4),0,0,360,color,2,LINE_AA);
}
void drawCricle(Mat &image)
{
Point center(image.cols/2,image.rows/2);//圆中心
int r=150;
Scalar color =Scalar(255,0,255);//颜色
circle(image,center,r,color,3,LINE_AA);
}