我正在开发一个应用程序,它从网络摄像头捕获一帧并显示它。我正在使用 Qt 和 opencv 来做到这一点。代码如下:
#include "trainwindow.h"
#include <QtWidgets>
#include <QFormLayout>
#include <iostream>
#include <fstream>
#include <sstream>
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
TrainWindow::TrainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget * wdg = new QWidget(this);
label = new QLabel("Camera");
label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
label->setScaledContents(true);
layout = new QVBoxLayout();
layout->addWidget(label);
mainlayout = new QVBoxLayout();
mainlayout->addLayout(layout,1000);
formLayout = new QFormLayout;
nameLineEdit = new QLineEdit();
emailLineEdit = new QLineEdit();
formLayout->addRow(tr("&Name:"), nameLineEdit);
formLayout->addRow(tr("&Department:"), emailLineEdit);
mainlayout->addLayout(formLayout);
train = new QPushButton(wdg);
train->setText(tr("Train"));
mainlayout->addWidget(train);
back = new QPushButton(wdg);
back->setText(tr("Back"));
connect (back, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
mainlayout->addWidget(back);
wdg->setLayout(mainlayout);
setCentralWidget(wdg);
// mainlayout->addLayout(sub_layout);
capture = cvCreateCameraCapture(-1);
// Capture a frame
frame = cvQueryFrame(capture);
// Point to the same frame
source_image = frame;
// Resize Image
cv::resize(source_image, source_image, cv::Size(128,128) , 0, 0);
// Change to RGB format
cv::cvtColor(source_image,source_image,CV_BGR2RGB);
// Convert to QImage
QImage qimg = QImage((const unsigned char*) source_image.data, source_image.cols,source_image.rows, QImage::Format_RGB888); // convert to QImage
// Display on Input Label
label->setPixmap(QPixmap::fromImage(qimg));
// Resize the label to fit the image
label->resize(label->pixmap()->size());
}
TrainWindow::~TrainWindow()
delete ui;
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
}
火车窗口.h:
#ifndef TRAINWINDOW_H
#define TRAINWINDOW_H
#include <QMainWindow>
#include <iostream>
#include <QtWidgets>
#include <fstream>
#include <QFormLayout>
#include <sstream>
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/contrib/contrib.hpp"
using namespace cv;
using namespace std;
namespace Ui {
class TrainWindow;
}
class TrainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit TrainWindow(QWidget *parent = 0);
~TrainWindow();
private:
Ui::TrainWindow *ui;
CvCapture *capture; // OpenCV Video Capture Variable
IplImage *frame; // Variable to capture a frame of the input video
cv::Mat source_image; // Variable pointing to the same input frame
cv::Mat dest_image; // Variable to output a frame of the processed video
QTimer *imageTimer;
QLabel *label;
QVBoxLayout *layout;
QHBoxLayout *button_layout;
QVBoxLayout *sub_layout;
QVBoxLayout *mainlayout;
QFormLayout *formLayout;
QLineEdit *nameLineEdit;
QLineEdit *emailLineEdit;
QPushButton *train;
QPushButton *back;
public slots:
void on_button_clicked();
};
#endif // TRAINWINDOW_H
现在,我想在正在显示的框架上添加一个文本。我该怎么做?