qt-opencv图像分割之Otsu算法实例
1 .pro项目文件配置
添加
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv
/usr/local/include/opencv2
LIBS += /usr/local/lib/libopencv_* \
2 ui设计
3 代码
3.1 mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
cv::Mat src; //左边QLabel对应的mat
QImage qimgs; //左边QLabel对应的qimg
cv::VideoCapture cap;
cv::Mat otsu; //右边QLabel对应的mat
QImage qimgo; //右边QLabel对应的qimg
private slots:
void on_closeAct_triggered();
private:
Ui::MainWindow *ui;
};
3.2 mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QDebug"
#include<QFileDialog>
using namespace cv;
void mat2qimg(Mat &src,QImage &qimg);
int getOtsuThreshold(long* histo, long totals);
void getHistogram(Mat& src,long* histo);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
int rows = ui->bgrLabel->height();
int cols = ui->bgrLabel->width();
//打开图片
connect(ui->imgAct,&QAction::triggered,this,[=](){
//打开文件对话框,返回文件路径
QString fp = QFileDialog::getOpenFileName(