gpt4 book ai didi

qt - 如何在获得的框架上添加文本?

转载 作者:行者123 更新时间:2023-12-02 17:52:06 24 4
gpt4 key购买 nike

我正在开发一个应用程序,可以从网络摄像头捕获帧并显示它。我正在使用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::Mat):
这是 "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;
}

关于qt - 如何在获得的框架上添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18009413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com