0

我正在开发一个应用程序,它从网络摄像头捕获一帧并显示它。我正在使用 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

现在,我想在正在显示的框架上添加一个文本。我该怎么做?

4

1 回答 1

1

对于旧界面,请使用以下内容:

// init font (you need do it once)
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5);
...
// then you can use it fo uot your text
cvPutText(img, "Hello!", cvPoint(text_x_position, text_y_position), &font, cvScalar(255));

对于新界面(cv::Mat):这是来自“显示随机文本示例”的代码

int Displaying_Random_Text( Mat image, char* window_name, RNG rng )
{
  int lineType = 8;

  for ( int i = 1; i < NUMBER; i++ )
  {
    Point org;
    org.x = rng.uniform(x_1, x_2);
    org.y = rng.uniform(y_1, y_2);

    putText( image, "Testing text rendering", org, rng.uniform(0,8),
             rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);

    imshow( window_name, image );
    if( waitKey(DELAY) >= 0 )
      { return -1; }
  }

  return 0;
}
于 2013-08-02T06:14:39.323 回答