废话不多说,先上代码: 环境 : Qt5.9.1+ Msv2015
#ifndef DRAWIMAGE_H
#define DRAWIMAGE_H
#include "opencv2/opencv.hpp"
#include "opencv2/calib3d/calib3d.hpp"
using namespace cv;
class DrawImage
{
public:
DrawImage();
void DrawLine(Mat& img,const Point pt1,Point pt2);
/**
* @brief DrawLine 画线
* @param img 输入图(线会画在此图上)
* @param pt1 起始点
* @param pt2 结束点
* @param color 线的颜色
* @param thickness 线的厚度
* @param lineType 线的类型(可输入4、8、CV_AA)4连通/8连通(通道连接)/消除锯齿
* @param shift 默认为0
*/
void DrawLine(Mat& img,const Point pt1,Point pt2,const Scalar& color, int thickness=1,int lineType=8,int shift=0);
void DrawRectangle(Mat& img, Point pt1, Point pt2);
/**
* @brief DrawRectangle 画矩形
* @param img 输入图
* @param pt1 起始点
* @param pt2 结束点
* @param color 线的颜色
* @param tickness 线的厚度
* @param lineType 线的类型
* @param shift 默认为0
*/
void DrawRectangle(Mat& img,Point pt1,Point pt2,const Scalar& color, int tickness=1, int lineType=8, int shift=0);
void DrawRectangle(Mat& img,Rect rec);
/**
* @brief DrawRectangle 画矩形
* @param img 输入图
* @param rec 矩形框的范围(Rect)
* @param color 矩形线颜色
* @param tickness 矩形线宽
* @param LineTypes 矩形线类型
* @param shift 默认为0
*/
void DrawRectangle(Mat& img,Rect rec,const Scalar& color,int tickness=1, int LineTypes=8, int shift=0);
void Circle(Mat& img,Point center,int radius);
/**
* @brief Circle 画圆
* @param img 输入图
* @param center 圆心坐标
* @param radius 圆半径
* @param color 圆线颜色
* @param tickness 圆线厚度
* @param lineType 圆线类型
* @param shift 默认为0
*/
void Circle(Mat& img,Point center,int radius,const Scalar& color,int tickness=1,int lineType=8, int shift=0);
void Ellipse(Mat& img,Point center, Size axes,double angle,double startAngle,double endAngle);
/**
* @brief Ellipse 画椭圆
* @param img 输入图
* @param center 圆心
* @param axes 椭圆轴的尺寸
* @param angle 旋转角度
* @param startAngle 椭圆弧度起始角度(单位角度)
* @param endAngle 椭圆弧度结束角度(单位角度)
* @param color 椭圆的颜色
* @param thickness 椭圆的线宽
* @param lineType 椭圆线形态
* @param shift 默认0
*/
void Ellipse(Mat& img,Point center, Size axes,double angle,double startAngle,double endAngle,const Scalar& color,int thickness=1,int lineType=8,int shift=0);
void Polylines(Mat& img,const Point**pts,const int* npts, int ncontours, bool isClosed);
/**
* @brief Polylines 画多边形
* @param img 输入图
* @param pts 包含多边形各个曲线点的阵列
* @param npts 包含多角度曲线顶点书面的阵列
* @param ncontours 曲线数
* @param isClosed 是否为封闭的多角形
* @param color 多角形的颜色
* @param thickness 多角形的线宽(输入负值表示填满或CV_FILLED)
* @param lineType 多角度线形态
* @param shift 默认0
*/
void Polylines(Mat& img,const Point**pts,const int* npts, int ncontours, bool isClosed,const Scalar& color, int thickness=1,int lineType=8,int shift=0);
void PutText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale);
/**
* @brief PutText 书文字字符串
* @param img 输入图
* @param text 输入文字内容
* @param org 文字左下角位子
* @param fontFace 字体样式
* @param fontScale 字体大小
* @param color 字体颜色
* @param thickness 构成字体的线宽度
* @param lineType 通道形态
* @param bottomLeftOrigin 是否左对齐
*/
void PutText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness, int lineType, bool bottomLeftOrigin);
void setColor(Scalar);
void setThickness(int);
void setLineType(int);
void setting(Scalar,int,int);
private:
Scalar _color=(134,28,134);
int _thickness=3;
int _lineType=8;
};
#endif // DRAWIMAGE_H
DrawImage.cpp
#include "DrawImage.h"
DrawImage::DrawImage()
{
}
void DrawImage::DrawLine(Mat &img, const Point pt1, Point pt2)
{
line(img,pt1,pt2,_color,_thickness,_lineType,0);
}
void DrawImage::DrawLine(Mat &img, const Point pt1, Point pt2, const Scalar &color, int thickness, int lineType, int shift)
{
line(img,pt1,pt2,color,thickness,lineType,shift);
}
void DrawImage::DrawRectangle(Mat &img, Point pt1, Point pt2)
{
rectangle(img,pt1,pt2,_color,_thickness,_lineType,0);
}
void DrawImage::DrawRectangle(Mat &img, Point pt1, Point pt2, const Scalar &color, int tickness, int lineType, int shift)
{
rectangle(img,pt1,pt2,_color,tickness,lineType,shift);
}
void DrawImage::DrawRectangle(Mat &img, Rect rec)
{
rectangle(img,rec,_color,_thickness,_lineType,0);
}
void DrawImage::DrawRectangle(Mat &img, Rect rec, const Scalar &color, int tickness, int LineTypes, int shift)
{
rectangle(img,rec,color,tickness,LineTypes,shift);
}
void DrawImage::Circle(Mat &img, Point center, int radius)
{
circle(img,center,radius,_color,_thickness,_lineType,0);
}
void DrawImage::Circle(Mat &img, Point center, int radius, const Scalar &color, int tickness, int lineType, int shift)
{
circle(img,center,radius,color,tickness,lineType,shift);
}
void DrawImage::Ellipse(Mat &img, Point center, Size axes, double angle, double startAngle, double endAngle)
{
ellipse(img,center,axes,angle,startAngle,endAngle,_color,_thickness,_lineType,0);
}
void DrawImage::Ellipse(Mat &img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar &color, int thickness, int lineType, int shift)
{
ellipse(img,center,axes,angle,startAngle,endAngle,color,thickness,lineType,shift);
}
void DrawImage::Polylines(Mat &img, const Point **pts, const int *npts, int ncontours, bool isClosed)
{
polylines(img,pts,npts,ncontours,isClosed,_thickness,_lineType,0);
}
void DrawImage::Polylines(Mat &img, const Point **pts, const int *npts, int ncontours, bool isClosed, const Scalar &color, int thickness, int lineType, int shift)
{
polylines(img,pts,npts,ncontours,isClosed,color,thickness,lineType,0);
}
void DrawImage::PutText(InputOutputArray img, const String &text, Point org, int fontFace,double fontScale)
{
putText(img,text,org,fontFace,fontScale,_color,_thickness,_lineType,false);
}
void DrawImage::PutText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness, int lineType, bool bottomLeftOrigin)
{
putText(img,text,org,fontFace,fontScale,color,thickness,lineType,bottomLeftOrigin);
}
void DrawImage::setColor(Scalar colarr)
{
_color = colarr;
}
void DrawImage::setThickness(int thicknes)
{
_thickness = thicknes;
}
void DrawImage::setLineType(int LinTye)
{
_lineType = LinTye;
}
void DrawImage::setting(Scalar col, int thick, int lint)
{
_color = col;
_thickness = thick;
_lineType = lint;
}
OpenCV原始函数(算子)
OpenCV 畫線
void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
OpenCV 畫矩形
void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
OpenCV 畫圓
void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
OpenCV 畫橢圓
void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
OpenCV 畫多角形
void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, intlineType=8, int shift=0)
OpenCV 畫文字字串
void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false)