Qt-OpenCV学习笔记--识别图形轮廓并计算图形坐标

概述

思路:将获得的图像进行修正,然后识别图形轮廓,最后计算图形坐标。

整体方案是基本函数和基本操作的综合应用。

此方案可以用于标签位置检测。

代码没有经过实际验证,此处仅是做个记录。

测试代码

myCV.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    sm.cpp \
    widget.cpp

HEADERS += \
    sm.h \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

unix|win32: LIBS += -L$$PWD/../../../../../opencv/install/x64/mingw/lib/ -llibopencv_world454.dll

INCLUDEPATH += $$PWD/../../../../../opencv/install/include
DEPENDPATH += $$PWD/../../../../../opencv/install/include

sm.h

#ifndef SM_H
#define SM_H

#include 
#include 
#include 

#include 

class sm
{
public:
    sm();

public:

    //预处理
    static cv::Mat pretreatment(cv::Mat src, int width, int height);
    //后处理
    static std::vector postTreatment (cv::Mat src);
    //坐标排序
    static void sortPoints(cv::RotatedRect rect,cv::Point2f pts[]);
    //获取轮廓
    static std::vector > getContour(cv::Mat src, int threshold1, int threshold2);
    //裁切
    static cv::Mat cutBorder (cv::Mat src,int number);
};

#endif // SM_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

main.cpp

#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

sm.cpp

#include "sm.h"

#include 
#include 
#include 

#include 

sm::sm()
{

}

cv::Mat sm::pretreatment(cv::Mat src,int width, int height)
{
    //定义输出对象
    cv::Mat dst;

    //获得轮廓
    std::vector> contours;
    contours = sm::getContour(src,100,200);

    //先要判断所有图形是否全部内嵌
    if (contours.size()!=1)
    {
        qDebug()<<"图像加载错误,请重新加载!";
        return dst;
    }

    //获得矩形
    cv::RotatedRect box;
    box = cv::minAreaRect(contours[0]);
    qDebug()<<"矩形的角度:"< sm::postTreatment(cv::Mat src)
{
    //获得轮廓
    std::vector> contours;
    contours = sm::getContour(src,100,200);

    //定义输出对象
    std::vector RotatedRect(contours.size());

    //获得矩形
    for (uint i=0;ip0.x && p[i].yp0.x && p[i].y>p0.y )
            pts[2]=p[i];
        else
            pts[3]=p[i];
    }

    //打印
    qDebug()<<"中心点:"<> sm::getContour(cv::Mat src,int threshold1,int threshold2)
{
    //初步处理
    cv::Mat dst_gray,
            dst_blur,
            dst_canny,
            dst_dilate;
    //灰度处理
    cv::cvtColor(src,dst_gray,cv::COLOR_BGR2GRAY);
    //cv::imshow("dst_gray",dst_gray);
    //高斯模糊
    cv::GaussianBlur(dst_gray,dst_blur,cv::Size(3,3),0,0);
    //cv::imshow("dst_blur",dst_blur);
    //边缘检测
    cv::Canny(dst_blur,dst_canny,threshold1,threshold2);
    //cv::imshow("dst_canny",dst_canny);
    //膨胀
    cv::dilate(dst_canny,dst_dilate,cv::Mat());
    //cv::imshow("dst_dilate",dst_dilate);

    //检测轮廓
    std::vector> contours;
    findContours(dst_dilate,contours,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_NONE);
    //drawContours(src,contours,-1,cv::Scalar(255,0,255),cv::FILLED);
    //cv::imshow("src",src);

    //打印轮廓数量
    qDebug()<<"轮廓数量:"<

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include 

#include 
#include 

#include "sm.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    //获取路径
    QString qstr = ui->lineEdit->text();
    //转换
    std::string str = qstr.toStdString();

    //加载图像
    std::string path =str;
    cv::Mat src = cv::imread(path);
    //判断是否加载成功
    if(src.empty())
    {
        qDebug()<<"加载图片失败!";
        return;
    }

    //图像预处理
    cv::Mat dst_p;
    //获取数据
    QString width = ui->lineEdit_2->text();
    QString height = ui->lineEdit_3->text();
    //qstring转int
    int width_i = width.toInt();
    int height_i = height.toInt();
    //预处理
    dst_p = sm::pretreatment(src,width_i,height_i);
    //判断预处理是否成功
    if(dst_p.empty())
    {
        qDebug()<<"图像预处理失败!";
        return;
    }

    //裁切
    cv::Mat dst_c;
    dst_c = sm::cutBorder(dst_p,20);

    //后处理
    std::vector rects;
    rects = sm::postTreatment(dst_c);

    //打印结果
    std::vector> box(rects.size());

    for (uint i=0;itextBrowser->append(str);

        //获得矩形顶点
        cv::Point2f ps[4];
        rects[i].points(ps);
        box[i].push_back(ps[0]);
        box[i].push_back(ps[1]);
        box[i].push_back(ps[2]);
        box[i].push_back(ps[3]);

        //绘制矩形
        polylines(dst_c,box[i],1,cv::Scalar(0,255,0),2,8,0);

    }
    //打印空行
    ui->textBrowser->append(" ");
    //显示
    cv::imshow("dst_c",dst_c);
}

void Widget::on_pushButton_2_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,"选择一个图片",".","*.jpg *.png *.bmp");
    if(!fileName.isEmpty())
    {
        ui->lineEdit->setText(fileName);
        ui->label_6->setPixmap(fileName);
    }

}

widget.ui

Qt-OpenCV学习笔记--识别图形轮廓并计算图形坐标_第1张图片

测试结果

Qt-OpenCV学习笔记--识别图形轮廓并计算图形坐标_第2张图片

 

你可能感兴趣的:(学习,opencv,qt)