其他分享
首页 > 其他分享> > Qt + opencv的学习(二)

Qt + opencv的学习(二)

作者:互联网

本节内容对opencv提供的一些绘图接口进行了学习。

其中对于绘制椭圆的偏转以及绘制矩形时的坐标计算,都使用注释标明了

 

#include "widget.h"
#include <QApplication>
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <QDebug>


#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

using namespace std;
using namespace cv;

const int w = 400;

//绘制椭圆
void MyEllipse(Mat img, double angle)
{
    int thickness = 2;//线宽
    int lineType = 8;//线的类型

    //img 输入输出图像
    //Point 指定的椭圆中心坐标
    //Size 指定的椭圆所在的区域矩形。这里两个参数表示长和高(相差越大椭圆越椭,相等则为圆)
    //angle 椭圆长轴偏离角度(起始绘图点和中心坐标的角度)
    //startAngle 绘制椭圆起始角度
    //endAngle 绘制椭圆中点角度
    //start-end 如果是0-360,则表示整个椭圆,180表示半个椭圆。所绘制的形状和起始绘制点有关
    //Scalar 指定绘制线的颜色

    ellipse(img, Point(w/2.0, w/2.0), Size(w/4.0, w/16.0), angle,
            0, 270, Scalar(255, 0, 0), thickness, lineType);

}

//绘制圆形
void MyFilledCircle(Mat img, Point center)
{
    int thickness = -1;
    int lineType  = 8;

    //img 输入输出图像
    //Point 指定二维点作为圆心
    //int 圆半径
    //Scalar 指定的绘制圆颜色

    circle(img, center, w/32.0, Scalar(0, 0, 255), thickness, lineType);
}

//绘制直线
void MyLine(Mat img, Point start, Point end)
{
    int thickness = 2;
    int lineType = 8;
    line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
}

//绘制多边形
void MyPolygon(Mat img)
{
    int lineType = 8;

    Point rook_points[1][20];

    rook_points[0][0] = Point(w/4.0, 7*w/8.0);
    rook_points[0][1] = Point(3*w/4.0,  7*w/8.0);
    rook_points[0][2] = Point(3*w/4.0, 13*w/16.0);
    rook_points[0][3] = Point(11*w/16.0, 13*w/16.0);
    rook_points[0][4] = Point(19*w/32.0, 3*w/8.0);
    rook_points[0][5] = Point(3*w/4.0, 3*w/8.0);
    rook_points[0][6] = Point(3*w/4.0, w/8.0);
    rook_points[0][7] = Point(26*w/40.0, w/8.0);
    rook_points[0][8] = Point(26*w/40.0, w/4.0);
    rook_points[0][9] = Point(22*w/40.0, w/4.0);
    rook_points[0][10] = Point(22*w/40.0, w/8.0);
    rook_points[0][11] = Point(18*w/40.0, w/8.0);
    rook_points[0][12] = Point(18*w/40.0, w/4.0);
    rook_points[0][13] = Point(14*w/40.0, w/4.0);
    rook_points[0][14] = Point(14*w/40.0, w/8.0);
    rook_points[0][15] = Point(w/4.0, w/8.0);
    rook_points[0][16] = Point(w/4.0, 3*w/8.0);
    rook_points[0][17] = Point(13*w/32.0, 3*w/8.0);
    rook_points[0][18] = Point(5*w/16.0, 13*w/16.0);
    rook_points[0][19] = Point(w/4.0, 13*w/16.0);

    const Point* ppt[1] = {rook_points[0]};
    int npt[] = {20};//说明顶点个数

    fillPoly(img, ppt, npt, 1, Scalar(255,255,255), lineType);
}



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QLabel *lab = NULL;
    if(lab)
        qDebug() << "null";
    else{
        qDebug() << "not null";
    }

    int i;
    Widget w;
    w.show();



    /**绘制图形
     */
    QString atom_window = "Drawing 1:Action";
    QString rook_window = "Drawing 2:Rook";

    //创建两个空的图像
    Mat atom_image = Mat::zeros(w, w, CV_8UC3);
    Mat rook_image = Mat::zeros(w, w, CV_8UC3);

    // 角度是延y轴顺时针计算,例如45表示y轴顺时针转动45°开始画
    MyEllipse(atom_image, 0);
    MyEllipse(atom_image, 45);
    MyEllipse(atom_image, 90);//-90
    MyEllipse(atom_image, -45);//-45 两个不同

    MyFilledCircle(atom_image, Point(w/2.0, w/2.0));

    namedWindow(atom_window.toStdString(), WINDOW_AUTOSIZE);
    imshow(atom_window.toStdString(), atom_image);

    //绘制多边形
    MyPolygon(rook_image);

    rectangle(rook_image, Point(0, 7*w/8.0),Point(w,w), Scalar(0,255,255), -1, 8);

    //直线
    MyLine(rook_image, Point(0, 15*w/16), Point(w, 15*w/16));
    MyLine(rook_image, Point(w/4, 7*w/8), Point(w/4, w));
    MyLine(rook_image, Point(w/2, 7*w/8), Point(w/2, w));
    MyLine(rook_image, Point(3*w/4, 7*w/8), Point(3*w/4, w));

    namedWindow(rook_window.toStdString(), WINDOW_AUTOSIZE);
    imshow(rook_window.toStdString(), rook_image);



    /**绘制字体*/
    QString text = "你好";

    int fontFace = FONT_HERSHEY_SCRIPT_COMPLEX;
    double fontScale = 2;
    int thickness = 3;

    Mat img(600, 800, CV_8UC3, Scalar::all(0));//创建一个黑色的三通道图片
    int baseline = 0;

    Size textSize = getTextSize(text.toStdString(), fontFace, fontScale,
                                thickness, &baseline);
    qDebug() << "textSize width:" << textSize.width;
    qDebug() << "textSize height:" << textSize.height;
    qDebug() << "text y :" << baseline;

    // 确定矩形框大小所用
    baseline += thickness;

    //Org坐标为字符串左下角位置坐标
    Point textOrg((img.cols - textSize.width)/2, (img.rows + textSize.height)/2);
    cout << "textOrg:" << endl << textOrg << endl;
    //绘制矩形,坐标点分别是
    //左上角坐标 textOrg - Point(0, textSize.height)
    //右下角坐标 textOrg + Point(textSize.width, baseline)
    //左下角坐标 textOrg + Point(0, baseline);
    //右上角坐标 textOrg + Point(textSize.width, -textSize.height)

    rectangle(img, textOrg - Point(0, textSize.height),
              textOrg + Point(textSize.width, baseline), Scalar(0,0,255));

    cout << "text-Point:" << endl << textOrg + Point(textSize.width, -textSize.height) << endl;


    line(img, textOrg + Point(0, thickness), textOrg +
         Point(textSize.width, thickness), Scalar(0,0,255));

    putText(img, text.toStdString(), textOrg, fontFace, fontScale, Scalar::all(255),
            thickness, 8);
    imshow("t", img);


    return a.exec();
}
View Code

 

标签:8.0,rook,Qt,Point,int,学习,opencv,points,4.0
来源: https://www.cnblogs.com/warmSnowFY/p/11088475.html