我制作了一个程序,用于在背景图像上生成移动矩形的视频。这种形状的速度由计时器功能控制。
正如imshow
所见,我得到的输出正确。
但是我有两个问题-
videowriter
录制视频时,它会跳过帧。当我尝试降低帧速率时,它会有所改善。有没有办法像我在imshow
中那样保存我的视线?我正在Linux中使用QT。 我的代码是:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
sprintf(filenameImg,"/home/Workspace/TestVideoMaker/BackImages/7.jpg");
ui->setupUi(this);
imageBack= imread(filenameImg);
outputVideo.open("outcpp.avi",CV_FOURCC('M','J','P','G'),24,Size(imageBack.cols,imageBack.rows),true);
}
void MainWindow::on_pushButton_Generate_clicked()
{
initialX=MainWindowPtr->ui->lineEdit_initialX->text().toInt();
initialY=MainWindowPtr->ui->lineEdit_initialY->text().toInt();
QTimer *timer = new QTimer;
// make the connection using the timer variable
connect(timer, &QTimer::timeout, this, &generate_video);
// start the timer object by first dereferencing that object first
timer->setInterval(100-MainWindowPtr->ui->horizontalSlider_speed->value());
timer->start();
}
void generate_video()
{
heightImg = MainWindowPtr->ui->lineEdit_Height->text().toInt();
widthImg = MainWindowPtr->ui->lineEdit_Width->text().toInt();
intensity=255;
imageBack= imread(filenameImg); //to load original background repeatedly so that it doesn't show a trail of rectangle
cvtColor(imageBack, imageBack, cv::COLOR_RGB2GRAY);
rectangle(imageBack,Point(initialX,initialY),Point(initialX+widthImg,initialY+heightImg),Scalar(intensity),-1,8,0);
imshow("image",imageBack);
if(initialX>640)
initialX=0;
else
initialX+=2;
if(recordFlag==1)
{
cv::cvtColor(imageBack, frame, cv::COLOR_GRAY2BGR);
outputVideo.write(frame);
}
}
void MainWindow::on_pushButton_stop_clicked()
{
outputVideo.release();
timer->stop();
}
void MainWindow::on_pushButton_Record_clicked()
{
recordFlag=1;
}