美文网首页
6. 图像绘制与填充

6. 图像绘制与填充

作者: 逍遥才子 | 来源:发表于2020-03-06 19:52 被阅读0次

    图形绘制
    图形填充

    #include <opencv.hpp>
    #include <iostream>
    
    using namespace cv;
    using namespace std;
    
    int main(int argc, char** argv) {
    
        Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);
        
        namedWindow("canvas", WINDOW_NORMAL);
        imshow("canvas", canvas);
        //画线
        line(canvas, Point(0, 0), Point(400, 400), Scalar(0, 0, 255), 1, LINE_8);
        //画矩形
        Rect rect(100, 100, 200, 200);
        rectangle(canvas, rect, Scalar(255, 0, 0), 1, LINE_8, 0);
        //画圆
        circle(canvas, Point(300, 300), 100, Scalar(0, 255, 0), 1, LINE_8, 0);
    
        //文字
        putText(canvas, "hello opencv", Point(200, 50), FONT_HERSHEY_PLAIN, 1.0, Scalar(255, 0, 0), 2, 8);
    
        //椭圆 
        RotatedRect rrt;
        rrt.center = Point2f(256, 256);
        rrt.angle = CV_PI / 4;
        rrt.size = Size(100,200);
        ellipse(canvas, rrt, Scalar(0, 255, 255), -1, 8);//填充
    
        imshow("canvas", canvas);
    
    
        //随机画
        Mat image = Mat::zeros(Size(512, 512), CV_8UC3);
    
        int x1 = 0, y1 = 0;
        int x2 = 0, y2 = 0;
        RNG rng(12345);
        while (true) {
            x1 = (int)rng.uniform(0, 512);
            x2 = (int)rng.uniform(0, 512);
            y1 = (int)rng.uniform(0, 512);
            y2 = (int)rng.uniform(0, 512);
            int w = abs(x2 - x1);
            int h = abs(y2 - y1);
            rect.x = x1;
            rect.y = y1;
            rect.width = w;
            rect.height = h;
    
            line(image, Point(x1, y1), Point(x2, y2), Scalar(rng.uniform(0,256), rng.uniform(0, 256), rng.uniform(0, 256)), 1, LINE_8);
            rectangle(image, rect, Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256)), 1, LINE_8, 0);
            imshow("image", image);
            char c = waitKey(10);
            if (c == 27) {
                break;
            }
        }
    
    
        waitKey(0);
        destroyAllWindows();
        return 0;
    }
    
    image.png image.png

    相关文章

      网友评论

          本文标题:6. 图像绘制与填充

          本文链接:https://www.haomeiwen.com/subject/odebrhtx.html