在进行图像处理的过程中经常需要在图像上标注出识别的区域,这样就需要进行图形的绘制。
这里介绍一下圆、椭圆、直线、正方形的绘制
圆:
void circle(Mat img, Point center, int radius, Scalar color, int thickness=1, int lineType=8, int shift=0)
椭圆:
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 line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,int thickness = 1, int lineType = LINE_8, int shift = 0);
矩形:
void rectangle(Mat& img,Rect rec, const Scalar&color, intthickness=1, intlineType=8,intshift=0 )
void rectangle(Mat& img, Point pt1,Point pt2,const Scalar& color, int thickness=1, int lineType=8, int shift=0)
//绘制椭圆、圆、直线、多边形等
#define WINDOWS_WIDTH 600
#define WINDOWS_NAME_1 "绘图1"
#define WINDOWS_NAME_2 "绘图2"
void DrawEllipse(Mat image, double angle)
{
int thickness = 2;
ellipse(image, Point(WINDOWS_WIDTH / 2, WINDOWS_WIDTH / 2), Point(WINDOWS_WIDTH / 4.0, WINDOWS_WIDTH / 16.0), angle, 0, 360, Scalar(255, 129, 0), thickness);
}
void DrawFilledCircle(Mat image, Point center)
{
circle(image, center, WINDOWS_WIDTH / 32, Scalar(0, 0, 255), -1);
}
void DrawPolygon(Mat image)
{
Point p[4];
p[0] = Point(WINDOWS_WIDTH / 6, WINDOWS_WIDTH / 6);
p[1] = Point(WINDOWS_WIDTH / 2, WINDOWS_WIDTH / 6);
p[2] = Point(WINDOWS_WIDTH / 6, WINDOWS_WIDTH / 2);
p[3] = Point(WINDOWS_WIDTH / 2, WINDOWS_WIDTH / 2);
const Point* pp[1] = { p };
int npp[] = { 4 };
fillPoly(image, pp, npp, 1, Scalar(255, 255, 255));
}
void DrawLine(Mat image, Point start, Point end)
{
line(image, start, end, Scalar(255, 0, 0), 2);
}
int main()
{
Mat atomImage = Mat::zeros(WINDOWS_WIDTH, WINDOWS_WIDTH, CV_8UC3);
Mat rookImage = Mat::zeros(WINDOWS_WIDTH, WINDOWS_WIDTH, CV_8UC3);
DrawEllipse(atomImage, 90);
DrawEllipse(atomImage, 0);
DrawEllipse(atomImage, 45);
DrawEllipse(atomImage, -45);
DrawFilledCircle(atomImage, Point(WINDOWS_WIDTH / 2, WINDOWS_WIDTH / 2));
DrawPolygon(rookImage);
rectangle(rookImage, Point(400, 400), Point(100, 100), Scalar(0, 255, 0), 1, 8);
DrawLine(rookImage, Point(0, 0), Point(600, 600));
DrawLine(rookImage, Point(0, 600), Point(600, 0));
imshow(WINDOWS_NAME_1, atomImage);
imshow(WINDOWS_NAME_2, rookImage);
waitKey(0);
return 0;
}