之前一直用Halcon 里面有线程的基本图形绘制算子,直接拿来用,其实OpenCV中也是一样的。和Halcon中差不多。
多多学习,多多学习。。。
Halcon中也有绘制基本图形算子,但是Halcon中如果需要显示则,必须gen_ 或者 disp_
如 Halcon代码:
Step 1:draw_rectangle1( : : WindowHandle : Row1, Column1, Row2, Column2) 这个只是生成对象,并不会在屏幕中显示,
Step 2:gen_rectangle1( : Rectangle : Row1, Column1, Row2, Column2 : ) 所以对于用户而言,需要调用这个,在屏幕中显示出来,让用户可以直观的看到。disp_rectangle1( : : WindowHandle, Row1, Column1, Row2, Column2 : )
当然Halcon中有很多。draw_前缀的都是生成对应的对象,disp_都是用来显示所对应的对象到屏幕中。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
Mat OutPutImg;
void MyDrawLine() {
Point p1 = Point(20, 20);
Point p2;
p2.x = 100;
p2.y = 100;
Scalar LineColor = Scalar(0, 0, 255);
line(OutPutImg, p1, p2, LineColor, 1, LINE_8);
//arrowedLine(OutPutImg, p1, p2, LineColor, 2, LINE_AA);
//arrowedLine(OutPutImg, p2, p1, LineColor, 1, LINE_AA);
//! type of line
//enum LineTypes {
// FILLED = -1,
// LINE_4 = 4, //!< 4-connected line
// LINE_8 = 8, //!< 8-connected line
// LINE_AA = 16 //!< antialiased line //};
}
void Myrectangle01() {
Rect re = Rect(20,20,100,100);
Scalar reColor = Scalar(0, 0, 255);
rectangle(OutPutImg, re, reColor,1);
//函数原型
//void rectangle(CV_IN_OUT Mat& img, Rect rec,
// const Scalar& color, int thickness = 1,
// int lineType = LINE_8, int shift = 0);
}
void Myrectangle02() {
Point p1 = Point(20, 20);
Point p2;
p2.x = 100;
p2.y = 100;
Scalar reColor = Scalar(0, 0, 255);
//rectangle(OutPutImg, p2, p1, reColor, 1);
rectangle(OutPutImg, p1, p2, reColor, 1);
//函数原型
//void rectangle(InputOutputArray img, Point pt1, Point pt2,
// const Scalar& color, int thickness = 1,
// int lineType = LINE_8, int shift = 0);
}
void Mycircle(){
Point Center_p;
Center_p.x = OutPutImg.cols/2;
Center_p.y = OutPutImg.rows/2;
int circle_radius = 50;
Scalar circle_Color = Scalar(0, 0, 255);
circle(OutPutImg, Center_p, circle_radius, circle_Color, 1);
//函数原型
//void circle(InputOutputArray img, Point center, int radius,
//const Scalar& color, int thickness = 1,
//int lineType = LINE_8, int shift = 0);
}
void Myellipse01() {
Point Center_p;
Center_p.x = OutPutImg.cols / 2;
Center_p.y = OutPutImg.rows / 2;
Size SizeOfellipse; //= Size(100, 20);
SizeOfellipse.height = 100;//长边
SizeOfellipse.width = 20;//短边
double angle = 90;//角度为长边方向与垂直方向的夹角
double startAngle = 0;
//double endAngle = 360;
double endAngle = -90;
Scalar circle_Color = Scalar(0, 0, 255);
ellipse(OutPutImg, Center_p, SizeOfellipse, angle, startAngle, endAngle, circle_Color,1);
//函数原型
//void ellipse(InputOutputArray img, Point center, Size axes,
// double angle, double startAngle, double endAngle,
// const Scalar& color, int thickness = 1,
// int lineType = LINE_8, int shift = 0);
}
void Myellipse02() {
Point Center_p;
Center_p.x = OutPutImg.cols / 2+10;
Center_p.y = OutPutImg.rows / 2+20;
Size SizeOfellipse; //= Size(100, 20);
SizeOfellipse.height = 200;//长边
SizeOfellipse.width = 100;//短边
double angle = 90;//角度为长边方向与垂直方向的夹角
//如果需要绘制圆弧,则将长短边设置相等,然后起始角度和终止角度设置就OK
double startAngle = 0;
//double endAngle = 360;
double endAngle = -90;
Scalar circle_Color = Scalar(0, 0, 255);
RotatedRect rr;
rr.angle = endAngle;
rr.size = SizeOfellipse;
rr.center = Center_p;
ellipse(OutPutImg, rr, circle_Color, 1);
//函数原型
//CV_EXPORTS_W void ellipse(InputOutputArray img,
//const RotatedRect& box, const Scalar& color,
// int thickness = 1, int lineType = LINE_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,300);
pts[0][4] = Point(400,450);
const Point* ppts[] = { pts[0] };
int NumPts[] = { 5 };
Scalar mColor = Scalar(0, 0, 255);
fillPoly(OutPutImg,ppts,NumPts,1, mColor,8);
//函数原型
//CV_EXPORTS_W void fillConvexPoly(InputOutputArray img, InputArray points,
// const Scalar& color, int lineType = LINE_8,
// int shift = 0);
///** @overload */
//CV_EXPORTS void fillPoly(Mat& img, const Point** pts,
// const int* npts, int ncontours,
// const Scalar& color, int lineType = LINE_8, int shift = 0,
// Point offset = Point());
}
int main() {
Mat srcImage = imread("D:/opencv/Test01.jpg");
//Mat OutPutImg = Mat::zeros(srcImage.size(), srcImage.type());
OutPutImg = Mat::zeros(srcImage.size(), srcImage.type());
if (!srcImage.data) {
printf("骚年:你应该继续查找你的文件路径是什么??或者你电脑是否需要换新的。");
return -1;
}
//if (srcImage.empty()) {//如果图像为空则返回 false
// printf("如果图像为空则返回 false");
// return -1;
//}
MyDrawLine();//先显示线条
//Myrectangle01();
Myrectangle02();
Mycircle();
Myellipse01();
Myellipse02();
MyPolygon();
Point Test_p1 = Point(OutPutImg.rows/2, OutPutImg.cols / 2-100);
Scalar zitiColor = Scalar(255, 255, 255);
string mt = { "This text that U want to show in you scr" };
putText(OutPutImg, mt, Test_p1, CV_FONT_HERSHEY_COMPLEX,0.8, zitiColor,1, LINE_8);
//函数原型
//int fontFace,
//#define CV_FONT_HERSHEY_SIMPLEX 0
//#define CV_FONT_HERSHEY_PLAIN 1
//#define CV_FONT_HERSHEY_DUPLEX 2
//#define CV_FONT_HERSHEY_COMPLEX 3
//#define CV_FONT_HERSHEY_TRIPLEX 4
//#define CV_FONT_HERSHEY_COMPLEX_SMALL 5
//#define CV_FONT_HERSHEY_SCRIPT_SIMPLEX 6
//#define CV_FONT_HERSHEY_SCRIPT_COMPLEX 7
//#define CV_FONT_ITALIC 16
//void putText(InputOutputArray img, const String& text, Point org,
// int fontFace, double fontScale, Scalar color,
// int thickness = 1, int lineType = LINE_8,
// bool bottomLeftOrigin = false);
imshow("源图像", OutPutImg);
//如果放到背景图像显示之后则会覆盖,就和Halcon中一项。类似于PS中的图层
//MyDrawLine();
waitKey();
return 0;
}