Reputation: 519
I'm developing an app which captures a frame from the webcam and displays it. I'm using Qt and opencv to do so. The code for this is as follows:
#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
Now, I want to add a text on the frame which is being displayed. How do I do it?
Upvotes: 0
Views: 1187
Reputation: 10850
For old interface use something like this:
// 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));
For new interface (cv::Mat): Here is code from "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;
}
Upvotes: 1