C++使用OpenCV绘制直线、正方形、椭圆、圆形、方形填充、随机生成直线

/*
绘制形状与文字
*/
#include 
#include 
using namespace cv;
using namespace std;

Mat bgImage;
const char* dravdeo_win = "drav shapes and text demo";
void MyLines();   //画直线
void MyRectangle();   
void MyEllipse();
void MyPolyon();
void MyCircle();
void RandomLineDemo();
int main(int argc,char**argv)
{
    
    bgImage = imread("E:/Learning_AI/OpenCV_C++/1.jpg");

    if (bgImage.empty())
    {
        cout << "can't open image" << endl;
    }
    //MyLines();   //绘制直线
    MyRectangle();  //绘制正方形框
    //MyEllipse();   //绘制椭圆
    //MyCircle();   //绘制圆
   // MyPolyon();     //填充
   // RandomLineDemo();   //随机生成
   // putText(bgImage, "HELLO OpenCV", Point(300, 300),
    // 4,2.0, Scalar(100, 0, 200), 5, 8);     //在图片上写字
    namedWindow("output image", WINDOW_NORMAL);
    imshow("output image", bgImage);

    waitKey(0);
    return 0;
}

void MyLines()    //绘制直线
{
    //Point 打印Point(第一个点在x轴上的位置,第一个点在y轴上的位置)
  // 采用两点连线的方式划线
    Point p1 = Point(20,30);
    Point p2;
    p2.x = 300;
    p2.y = 300;
    Scalar color = Scalar(0, 0, 255);   //定义线的颜色
    line(bgImage, p1, p2, color, 10, LINE_AA);
    //图片名,第一个点,第二个点,线的颜色,线的厚度,线型
}
void MyRectangle()
{  //Rect(方框在x轴的位置,方框在y轴的位置,方框的宽,方框的高)
    Rect rect = Rect(100, 500, 100, 500);
    Scalar color = Scalar(0, 0, 255);   //方框的颜色
    rectangle(bgImage, rect, color, 10, LINE_8); 
    //方形(图像的窗口,方框的)
}
void MyEllipse()
{
    Scalar color = Scalar(255, 0, 0);
    ellipse(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), Size(bgImage.cols / 4, bgImage.rows / 8),45, 0, 360, color, 10, LINE_8);
                                                                                                         //45倾斜度,360画椭圆的完成度 
}

void MyCircle()
{

    Scalar color = Scalar(0, 0, 255);
    Point center = Point(bgImage.cols / 2, bgImage.rows / 2);
    circle(bgImage, center, 150, color, 2, 8);
}
void MyPolyon()   //图形填充
{
    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, 12, 255);
    fillPoly(bgImage, ppts,npt, 1, color, 8);

}
void RandomLineDemo()   //生成随机线条划线
{
  //1.创建空白窗口
    Mat bg = Mat::zeros(bgImage.size(), bgImage.type());
    RNG rng(12345); //随机数生成
    Point pt1;
    Point pt2;
    namedWindow("random line demo", 1);
    for (int i = 0; i < 1000; i++)
    {
        pt1.x = rng.uniform(0, bgImage.cols);
        pt2.x = rng.uniform(0, bgImage.cols);
        pt1.y = rng.uniform(0, bgImage.rows);
        pt2.y = rng.uniform(0, bgImage.rows);
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        if(waitKey(50)>0)    //按空格键退出
        {  
           //保存图片并退出
            imwrite("E:/Learning_AI/OpenCV_C++/demo.jpg", bg);
            break;   
        }
        line(bg, pt1, pt2, color, 1, 8);
        imshow("random line demo", bg);
    }
}

你可能感兴趣的:(opencv,c++)