#include<opencv2/opencv.hpp> #include<iostream> using namespace cv; void Mylines(); void MyRect(); void MyEllipse(); void MyPloy(); Mat srcImage; int main(int argc, char** argv) { srcImage = imread("E:/image/6.jpg"); if (srcImage.empty()) { printf("没有找到图片"); return -1; } Mylines();//直线 MyRect();//正方形 MyEllipse();//椭圆 MyPloy();//多边形 namedWindow("draw_image", WINDOW_AUTOSIZE); imshow("draw_image", srcImage); waitKey(0); return 0; } void Mylines() { Point p1 = Point(20, 30); Point p2; p2.x = 300; p2.y = 300; Scalar color = Scalar(0, 0, 255); line(srcImage, p1, p2, color, 1, LINE_8); } void MyRect() { Rect rect = Rect(170, 150, 370, 370); Scalar color = Scalar(255, 0, 0); rectangle(srcImage, rect, color, 2, LINE_4); } void MyEllipse() { Scalar color = Scalar(0, 255, 0); ellipse(srcImage,Point(srcImage.cols / 2, srcImage.rows / 3), Size(srcImage.cols / 4, srcImage.rows / 8), 90, 0, 360, color, 2, LINE_8); } void MyPloy() { 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, 100); pts[0][4] = Point(100, 100); const Point* ppts[] = { pts[0] }; int npt[] = { 5 }; Scalar color = Scalar(255, 100, 255); fillPoly(srcImage, ppts, npt, 1, color, 8); }
效果图: