机器视觉标定课设QT+OPENCV

gg.pro

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = gg
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11
SOURCES += \
        about.cpp \
        main.cpp \
        mainwindow.cpp \
        measure_circle.cpp \
        measure_rectangle.cpp \
        measure_simple.cpp
HEADERS += \
        about.h \
        mainwindow.h \
        measure_circle.h \
        measure_rectangle.h \
        measure_simple.h
FORMS += \
        about.ui \
        mainwindow.ui
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
INCLUDEPATH += F:\programme\cvlink\include\opencv
INCLUDEPATH += F:\programme\cvlink\include\opencv2
INCLUDEPATH += F:\programme\cvlink\include
LIBS += F:\programme\cvlink\lib\libopencv_*.a
RC_FILE += myico.rc

about.h

#ifndef ABOUT_H
#define ABOUT_H

#include 

namespace Ui {
class about;
}

class about : public QWidget
{
    Q_OBJECT

public:
    explicit about(QWidget *parent = nullptr);
    ~about();

private:
    Ui::about *ui;
};

#endif // ABOUT_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void onBtn1();
    void onBtn2();
    void onBtn3();
    void act_exit();
    void act_about();
    void act_save();
    void act_load();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

measure_circle.h

#ifndef MEASURE_CIRCLE_H
#define MEASURE_CIRCLE_H

#endif // MEASURE_CIRCLE_H

void measure_circle(double &radius,double p);

measure_rectangle.h

#ifndef MEASURE_RECTANGLE_H
#define MEASURE_RECTANGLE_H

#endif // MEASURE_RECTANGLE_H

void measure_rectangle(double &length,double &width,double p);

measure_simple.h

#ifndef MEASURE_SIMPLE_H
#define MEASURE_SIMPLE_H

#endif // MEASURE_SIMPLE_H

#include
using namespace cv;
void measure_simple(double &proportion);
void ImgSkeletonization(Mat &input_src,Mat &output_dst,int number=20);//@param  number :表示水平方向和垂直方向上“细化的次数”
void ImgSkeletonization_H(Mat &input_src,int *search_arr);//水平方向细化
void ImgSkeletonization_V(Mat &input_src,int *search_arr);//垂直方向细化

about.cpp

#include "about.h"
#include "ui_about.h"

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

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

main.cpp

#include "mainwindow.h"
#include 
int main(int argc, char *argv[]){
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "about.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "measure_simple.h"
#include "measure_circle.h"
#include "measure_rectangle.h"
#include 
#include 
#include 
#include 
#include 
static double p=321;
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow){
    ui->setupUi(this);
    this->setWindowTitle("机器视觉课程设计");
    connect(ui->pushButton_1,SIGNAL(clicked(bool)),this,SLOT(onBtn1()));
    connect(ui->pushButton_2,SIGNAL(clicked(bool)),this,SLOT(onBtn2()));
    connect(ui->pushButton_3,SIGNAL(clicked(bool)),this,SLOT(onBtn3()));
    connect(ui->action__save,SIGNAL(triggered()),this,SLOT(act_save()));
    connect(ui->action__load,SIGNAL(triggered()),this,SLOT(act_load()));
    connect(ui->action__exit, SIGNAL(triggered()),this,SLOT(act_exit()));
    connect(ui->action__about, SIGNAL(triggered()),this,SLOT(act_about()));
}
MainWindow::~MainWindow(){
    delete ui;
}
void MainWindow::act_exit(){
    this->close();
}
void MainWindow::act_about(){
    about* win_about=new about();
    win_about->show();
}
void MainWindow::act_save(){
    FILE* fl=freopen("C:/Users/Shinelon/Desktop/qt_image/data.txt","w",stdout);
    std::cout<setText("ok!");
    ql->move(50,50);
    dialog->resize(100,100);
    dialog->setModal(true);//注意:只有dialog有
    dialog->show();
}
void MainWindow::act_load(){
    FILE* fl=freopen("C:/Users/Shinelon/Desktop/qt_image/data.txt","r",stdin);
    std::cin>>p;//注意运行一次只可读入一次
    char tmp[10];
    sprintf(tmp,"%f",p);
    ui->label_proportion->setText(tmp);
    fclose(fl);
}
void MainWindow::onBtn1(){
    double proportion=0;
    measure_simple(proportion);
    p=proportion;
    char tmp[10];
    sprintf(tmp,"%f",proportion);
    ui->label_proportion->setText(tmp);
}
void MainWindow::onBtn2(){
    char tmp[10];double length=0,width=0;
    if(p<0){
        QMessageBox::information(this,"Error","The proporthion should be updated first!");
        return ;
    }
    measure_rectangle(length,width,p);
    sprintf(tmp,"%f",length);ui->label_length->setText(tmp);
    sprintf(tmp,"%f",width);ui->label_width->setText(tmp);
}
void MainWindow::onBtn3(){
    char tmp[10];double radius=0,perimeter=0,square=0;
    if(p<0){
        QMessageBox::information(this,"Error","The proporthion should be updated first!");
        return ;
    }
    measure_circle(radius,p);
    perimeter=2*3.14*radius;square=3.14*radius*radius;
    sprintf(tmp,"%f",radius);ui->label_radius->setText(tmp);
    sprintf(tmp,"%f",perimeter);ui->label_perimeter->setText(tmp);
    sprintf(tmp,"%f",square);ui->label_square->setText(tmp);
}

measure_circle.cpp

#include
#include
using namespace std;
using namespace cv;
void measure_circle(double &radius,double p){
    Mat frame;int key;
    namedWindow("frame");
    VideoCapture capture(0);
    while(1){
        key=waitKey(30);
        if(key==' ')break;
        capture>>frame;
        imshow("frame",frame);
    }
    imwrite("C:/Users/Shinelon/Desktop/qt_image/src3.jpg",frame);
    Mat src=imread("C:/Users/Shinelon/Desktop/qt_image/src3.jpg",0);
    GaussianBlur(src,src,Size(9,9),2,2);
    vectorcircles;
    HoughCircles(src,circles,HOUGH_GRADIENT,1.1,100,200,100,0,0);
    for(size_t i=0;i

measure_rectangle.cpp

#include
#include
using namespace std;
using namespace cv;
void measure_rectangle(double &length,double &width,double p){
    Mat frame;int key;
    namedWindow("frame");
    VideoCapture capture(0);
    while(1){
        key=waitKey(30);
        if(key==' ')break;
        capture>>frame;
        imshow("frame",frame);
    }
    imwrite("C:/Users/Shinelon/Desktop/qt_image/src2.jpg",frame);
    Mat src=imread("C:/Users/Shinelon/Desktop/qt_image/src2.jpg",0);
    vector corners;
    goodFeaturesToTrack(src,//输入图像
        corners,//检测到的角点的输出向量
        4,//角点的最大数量
        0.01,//角点检测可接受的最小特征值
        10,//角点之间的最小距离
        Mat(),//感兴趣区域
        3,//计算导数自相关矩阵时指定的邻域范围
        false,//不使用Harris角点检测
        0.04 );//权重系数
    double dis[4],min_dis=100000,mid_dis=100000;RNG g_rng(12345);
    for(unsigned long long i = 0; i < corners.size(); i++ ){
        cout<

measure_simple.cpp

#include
#include
#include
using namespace std;
using namespace cv;
static int video_flag=0;
static void on_mouse(int event,int x,int y,int flags,void*){
    if(event==EVENT_LBUTTONDOWN)
        if(video_flag==0)
            video_flag++;
}
void measure_simple(double &proportion){
    Mat frame;
    namedWindow("frame");
    VideoCapture capture(0);
    setMouseCallback("frame",on_mouse);
    video_flag=0;
    while(video_flag==0){
        waitKey(30);
        capture>>frame;
        imshow("frame",frame);
    }
    imwrite("C:/Users/Shinelon/Desktop/qt_image/src1.jpg",frame);
    Mat src=imread("C:/Users/Shinelon/Desktop/qt_image/src1.jpg",0);
    threshold(src,src,100,255,0);
    Mat dst;ImgSkeletonization(src, dst, 30);
    src=dst.clone();
    int a=10,b=10,c=10,d=10;
    for(int i=60;i<420;i++){
        for(int j=1;j<640;j++){
            if(src.at(i,j)==0){
                a=j;
                b=i;
                break;
            }
        }
        if(a!=10)break;
    }
    for(int i=420;i>60;i--){
        for(int j=639;j>0;j--){
            if(src.at(i,j)==0){
                c=j;
                d=i;
                break;
            }
        }
        if(c!=10)break;
    }
    circle(src,Point2i(a,b),10,Scalar::all(0));
    circle(src,Point2i(c,d),10,Scalar::all(0));
    imshow("src",src);
    circle(frame,Point2i(a,b),10,Scalar::all(0));
    circle(frame,Point2i(c,d),10,Scalar::all(0));
    imshow("frame",frame);
    waitKey(0);
    destroyAllWindows();
    proportion=10/sqrt((a-c)*(a-c)+(b-d)*(b-d));
    cout<<'('<(i - 1, j) + input_src.at(i, j) + input_src.at(i + 1, j);
                else
                    judge_value = 1;
                if (input_src.at(i, j) == 0 && judge_value != 0){
                    int a[9] = { 1,1,1,1,1,1,1,1,1};
                    for (size_t m = 0; m < 3; m++){
                        for (size_t n = 0; n < 3; n++){
                            if ((0 <= (i - 1 + m) < h) && (0 <= (j - 1 + n) < w) && input_src.at(i - 1 + m, j - 1 + n) == 0)
                                a[m * 3 + n] = 0;
                        }
                    }
                    int sum_value = a[0] * 1 + a[1] * 2 + a[2] * 4 + a[3] * 8 + a[5] * 16 + a[6] * 32 + a[7] * 64 + a[8] * 128;
                    input_src.at(i, j) = search_arr[sum_value] * 255;
                    if (search_arr[sum_value] == 1)
                        NEXT = false;
                }
            }
        }
    }
}
void ImgSkeletonization_V(Mat &input_src, int *search_arr){
    int h = input_src.rows;
    int w = input_src.cols;
    bool NEXT = true;
    for (size_t i = 1; i < h - 1; i++){//注意边界问题!!!!!!
        for (size_t j = 1; j < w - 1; j++){
            if (!NEXT)
                NEXT = true;
            else{
                int judge_value;
                if (1 < j (i, j - 1) + input_src.at(i, j) + input_src.at(i, j + 1);
                else
                    judge_value = 1;
                if (input_src.at(i, j) == 0 && judge_value != 0){
                    int a[9] = {1,1,1,1,1,1,1,1,1 };
                    for (size_t m = 0; m < 3; m++){
                        for (size_t n = 0; n < 3; n++){
                            if ((0 <= (i - 1 + m) < h) && (0 <= (j - 1 + n) < w) && input_src.at(i - 1 + m, j - 1 + n) == 0)
                                a[m * 3 + n] = 0;
                        }
                    }
                    int sum_value = a[0] * 1 + a[1] * 2 + a[2] * 4 + a[3] * 8 + a[5] * 16 + a[6] * 32 + a[7] * 64 + a[8] * 128;
                    input_src.at(i, j) = search_arr[sum_value] * 255;
                    if (search_arr[sum_value] == 1)
                        NEXT = false;
                }
            }
        }
    }
}

about.ui

机器视觉标定课设QT+OPENCV_第1张图片

mainwindow.ui

机器视觉标定课设QT+OPENCV_第2张图片

 

你可能感兴趣的:(图像处理(机器视觉项目))