飞道的博客

QT调用OpenCV绘制直线、矩形、椭圆、圆、不规则曲线、文本

446人阅读  评论(0)

开发环境:QT5.14.2+OpenCV4.5

提前准备:准备编译好的OpenCV开发环境(如自行编译的mingw版的opencv库,本地路径D:\opencv\qt_build64),准备一张测试图片(如:d:\test.jpg)。

项目结构:

 

核心代码:

 1、QtFace.pro代码

#opencv在mingw里的标准引用
INCLUDEPATH+=D:\opencv\qt_build64\install\include \             D:\opencv\qt_build64\install\include\opencv2 
LIBS+=D:\opencv\qt_build64\install\x64\mingw\lib\libopencv_*.a
2、mainwindow.h代码

#include <QMainWindow>
#include <opencv2/opencv.hpp>
using namespace cv;

3、mainwindow.cpp代码
//绘制直线
void MainWindow::on_actionDrawLine_triggered()
{
    cv::String fileName = "D:\\test.jpg";
    cv::Mat src = cv::imread(fileName);
    if(src.data == NULL){
        printf("图像读入失败\n");
        return;
    }
    cv::Point pt1= cv::Point_<int>(100,10);
    cv::Point pt2= cv::Point_<int>(100,200);
    cv::line(src,pt1,pt2, cv::Scalar(0,255,255),4);
    imshow("绘制直线",src);
    waitKey(0);
    destroyAllWindows();
}
//绘制矩形
void MainWindow::on_actionDrawRect_triggered()
{
    cv::String fileName = "D:\\test.jpg";
    cv::Mat src = cv::imread(fileName);
    if(src.data == NULL){
        printf("图像读入失败\n");
        return;
    }
    cv::Point pt3= cv::Point_<int>(50,50); // 矩阵左上角顶点
    cv::Point pt4=cv::Point_<int>(100,100);  // 矩形右下角顶点
    cv::rectangle(src,pt3,pt4,cv::Scalar(255,0,0),4);
 

    imshow("绘制矩形",src);
    waitKey(0);
    destroyAllWindows();

}
//绘制椭圆
void MainWindow::on_actionDrawEllipse_triggered()
{

    cv::String fileName = "D:\\test.jpg";
    cv::Mat src = cv::imread(fileName);
    if(src.data == NULL){
        printf("图像读入失败\n");
        return;
    }

    cv::RotatedRect box = cv::RotatedRect(cv::Point_<float>(100,100), cv::Size2f(200.0,200.0),45);
    cv::ellipse(src, box, cv::Scalar(0,255,0), 4);

    imshow("绘制椭圆",src);
    waitKey(0);
    destroyAllWindows();
}
//绘制圆形
void MainWindow::on_actionDrawCircle_triggered()
{

    cv::String fileName = "D:\\test.jpg";
    cv::Mat src = cv::imread(fileName);
    if(src.data == NULL){
        printf("图像读入失败\n");
        return;
    }

    cv::circle(src, cv::Point(300,300), 150,cv::Scalar(255,255,255),4);

    imshow("绘制圆形",src);
    waitKey(0);
    destroyAllWindows();
}
//绘制不规则曲线
void MainWindow::on_actionDrawPolyLines_triggered()
{
    cv::String fileName = "D:\\test.jpg";
    cv::Mat src = cv::imread(fileName);
    if(src.data == NULL){
        printf("图像读入失败\n");
        return;
    }

    std::vector<cv::Point> pts;
    pts.push_back(cv::Point(1,0));
    pts.push_back(cv::Point(20,50));
    pts.push_back(cv::Point(100,50));
    pts.push_back(cv::Point(100,300));
    cv::polylines(src,pts,false, cv::Scalar(0,255,0),5);

    imshow("绘制不规则曲线",src);
    waitKey(0);
    destroyAllWindows();
}
//绘制文字
void MainWindow::on_actionDrawText_triggered()
{

    cv::String fileName = "D:\\test.jpg";
    cv::Mat src = cv::imread(fileName);
    if(src.data == NULL){
        printf("图像读入失败\n");
        return;
    }

    cv::putText(src, "测试文字", cv::Point(50,100),cv::FONT_HERSHEY_PLAIN, 2,cv::Scalar(0,0,255),2);

    imshow("绘制文字",src);
    waitKey(0);
    destroyAllWindows();
}

运行截图:

 


转载:https://blog.csdn.net/xqf222/article/details/127631408
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场