我使用 Qt Creator 和 opencv 在我的 MainWindow 中创建了一些QLabel
,但我有这个错误:
错误:未在此范围内声明“ui”
在函数中filter_image()
。我想在进行一些处理之前和之后使用标签来显示图像。
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include<QImage>
#include<QLabel>
static QImage IplImage2QImage(const IplImage *iplImage) {
int height = iplImage->height;
int width = iplImage->width;
if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
{
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
return img.rgbSwapped();
}
else if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1)
{
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);
QVector<QRgb> colorTable;
for (int i = 0; i < 256; i++)
{
colorTable.push_back(qRgb(i, i, i));
}
img.setColorTable(colorTable);
return img;
}
else
{
std::cout << "Image cannot be converted.";
return QImage();
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void filter_image(IplImage* img) {
IplImage* roi = cvCreateImage(cvGetSize(img), 8, 3);
cvCvtColor( img, roi, CV_RGB2GRAY );
cvSmooth(img,roi,CV_BLUR, 5, 0, 0, 0);
cvThreshold(roi,roi,100,255,CV_THRESH_BINARY);
IplImage *img_de = cvCloneImage(roi);
QImage qt_i = IplImage2QImage(img_de);
QLabel label_2;
// display on label
ui->label_2->setPixmap(QPixmap::fromImage(qt_i));//error line
// resize the label to fit the image
ui->label_2->resize(ui->label_2->pixmap()->size());
label_2.show();
}
void MainWindow::on_actionOpen_triggered() {
IplImage *frame = cvLoadImage(
QFileDialog::getOpenFileName(this,
"Ouvrir un fichier",
"/../../Fichiers Image",
"Image (*.jpg *.bmp *.jpeg)")
.toStdString().c_str(),3);
IplImage *img_des = cvCloneImage(frame);
QImage qt_im = IplImage2QImage(img_des);
QLabel label;
// display on label
ui->label->setPixmap(QPixmap::fromImage(qt_im));
// resize the label to fit the image
ui->label->resize(ui->label->pixmap()->size());
label.show();
filter_image(frame);
}
主窗口.h
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
public:
Ui::MainWindow *ui;
public slots:
void on_actionOpen_triggered();
};
ui_mainwindow.h
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow{
public:
QAction *actionOpen;
QWidget *centralWidget;
QLabel *label;
QLabel *label_2;
QMenuBar *menuBar;
QMenu *menuFile;
QToolBar *mainToolBar;
QStatusBar *statusBar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(600, 400);
actionOpen = new QAction(MainWindow);
actionOpen->setObjectName(QString::fromUtf8("actionOpen"));
centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
label = new QLabel(centralWidget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(290, 30, 271, 301));
label_2 = new QLabel(centralWidget);
label_2->setObjectName(QString::fromUtf8("label_2"));
label_2->setGeometry(QRect(20, 20, 281, 331));
MainWindow->setCentralWidget(centralWidget);
menuBar = new QMenuBar(MainWindow);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 600, 21));
menuFile = new QMenu(menuBar);
menuFile->setObjectName(QString::fromUtf8("menuFile"));
MainWindow->setMenuBar(menuBar);
mainToolBar = new QToolBar(MainWindow);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(MainWindow);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
MainWindow->setStatusBar(statusBar);
menuBar->addAction(menuFile->menuAction());
menuFile->addAction(actionOpen);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
actionOpen->setText(QApplication::translate("MainWindow", "Open", 0, QApplication::UnicodeUTF8));
label->setText(QString());
label_2->setText(QString());
menuFile->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
} // retranslateUi};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui