OpenCV绘制图像及写文字

OpenCV绘制图像及写文字

一、绘制直线

void line ()
{
    Mat mat;
    mat=cv::imread("图像地址");
    Point p1=Point(20,30);
    Point p2;
    p2.x=300;
    p2.y=300;
    Scalar color=Scalar(0,0,255);
    line(mat,p1,p2,color,1,LINE_8);//p1,p2是线段的两端的坐标,color是线段颜色,1是线段粗细,LINE_8不用管,可以默认
}

二、绘制矩形

void rectangle ()
{
    Mat mat=cv::imread("图像路径",读取图像的方式);
    Rect rect=Rect(200,200,300,300);//前面两个是左上角坐标,后面是矩形的长和宽
    Scalar color=Scalar(255,0,0);
    rectangle(mat,rect,color,2,LINE_8);
}

三、绘制椭圆

void ellipse ()
{
    Mat mat=cv::imread("图像路径",读取图像方式);
    Scalar color=Scalar(255,0,0);
    ellipse(mat,Point(mat.cols/2,mat.rovs/2),Size(mat.cols/4,mat.rovs/8),90,0,360,color,2,LINE_8);//Point为椭圆中心坐标,Size为长轴短轴大小,90为椭圆倾斜程度,0和360为弧度范围,若为0和180则为半个弧

}

四、绘制圆

void circle ()
{
    Mat mat=cv::imread("图像路径",读取图像方式);
    Scalar color=Scalar(0,255,255);
    Point center=Point(mat.cols/2,mat.rovs/2);
    circle(mat,center,150,color,2,LINE_8);//center为圆心位置,150为半径
}

五、绘制多边形

void polygon ()
{
    Mat mat=cv::imread("图像路径",读取图像方式);
    Point pts[1][5];
    pts[0][0]=Point(100,100);
    pts[0][1]=Point(100,100);
    pts[0][2]=Point(100,100);
    pts[0][3]=Point(100,100);
    pts[0][4]=Point(100,100);

    const Point* ppts[]={pts[0]};
    int npt[]={5};
    Scalar color=Scalar(0,255,255);
    fillPoly(mat,ppts,npt,1,color,LINE_8);
}

六、绘制随机线

void f ()
{
    Mat mat=cv::imread("图像路径",读取图像方式);
    RNG rng(12345);
    Point pt1;
    Point pt2;
    Mat bg=Mat::zeros(mat.size(),mat.type());
    for(int i=0;i<100000;++i)
    {
        pt1.x=rng.uniform(0,mat.cols);
        pt2.x=rng.uniform(0,mat.cols);
        pt1.y=rng.uniform(0,mat.rows);
        pt2.y=rng.uniform(0,mat.rows);
        Scalar color=Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255));
        if(waitKey(50)>0)
            break;
        line(bg,pt1,pt2,color,1,LINE_8);
        cv::imshow("line_picture",bg);
    }


}

七、写文字

Mat mat=cv::imread("图像路径",读取图像方式);
putText(mat,"文字内容",Point(300,300),字体,1.0,Scalar(0,0,255),3,LINE_8);//Point为字体左下角的坐标,1.0这个值越大字越大,3为字体的粗细

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值