2013-08-02 2 views
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); 

} 

trainwindow.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 

지금, 내가 표시되는 프레임에 텍스트를 추가 할 수 있습니다. 어떻게해야합니까?

// 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 : 매트) :이 같은 이전 인터페이스 사용 무언가를

답변

1

여기 는 "Display random text example"

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; 
} 
에서 코드
관련 문제