您的位置:首页 > 运维架构

opencv读取多个摄像头并保存当前视频流图片

2017-09-12 14:45 489 查看
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <ctime>
#include <QDebug>
#include <string.h>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{

capture();

}
int MainWindow::capture(){

//initialize and allocate memory to load the video stream from camera
cv::VideoCapture camera0;
camera0.open(0);
camera0.set(CV_CAP_PROP_FRAME_WIDTH,640);
camera0.set(CV_CAP_PROP_FRAME_HEIGHT,480);

cv::VideoCapture camera1;
camera1.open(1);
camera1.set(CV_CAP_PROP_FRAME_WIDTH,640);
camera1.set(CV_CAP_PROP_FRAME_HEIGHT,480);

cv::VideoCapture camera2;
camera2.open(2);
camera2.set(CV_CAP_PROP_FRAME_WIDTH,640);
camera2.set(CV_CAP_PROP_FRAME_HEIGHT,480);

cv::VideoCapture camera3;
camera3.open(3);
camera3.set(CV_CAP_PROP_FRAME_WIDTH,640);
camera3.set(CV_CAP_PROP_FRAME_HEIGHT,480);

if( !camera0.isOpened() ) return 1;
if( !camera1.isOpened() ) return 1;
if( !camera2.isOpened() ) return 1;
if( !camera3.isOpened() ) return 1;

while(!stop) {
//grab and retrieve each frames of the video sequentially
// cv::Mat3b frame0;
camera0 >> frame0;
// cv::Mat3b frame1;
camera1 >> frame1;
camera2 >> frame2;
camera3 >> frame3;

cv::imshow("Video0", frame0);
cv::imshow("Video1", frame1);
cv::imshow("Video2", frame2);
cv::imshow("Video3", frame3);
//      std::cout << frame1.rows() << std::endl;
//wait for 40 milliseconds
int c = cvWaitKey(40);

//exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27)
if(27 == char(c)) break;
}

return 0;
}

QImage  MainWindow::Mat2QImage(const cv::Mat &mat)
{
//8-bitsunsigned,NO.OFCHANNELS=1
if(mat.type()==CV_8UC1)
{
//cout<<"1"<<endl;
//Setthecolortable(usedtotranslatecolourindexestoqRgbvalues)
QVector<QRgb>colorTable;
for(int i=0;i<256;i++)
colorTable.push_back(qRgb(i,i,i));
//CopyinputMat
const uchar*qImageBuffer=(const uchar*)mat.data;
//CreateQImagewithsamedimensionsasinputMat
QImage img(qImageBuffer,mat.cols,mat.rows,mat.step,QImage::Format_Indexed8);
img.setColorTable(colorTable);
return img;
}
//8-bitsunsigned,NO.OFCHANNELS=3
if(mat.type()==CV_8UC3)
{
//cout<<"3"<<endl;
//CopyinputMat
const uchar*qImageBuffer=(const uchar*)mat.data;
//CreateQImagewithsamedimensionsasinputMat
QImage img(qImageBuffer,mat.cols,mat.rows,mat.step,QImage::Format_RGB888);
return  img.rgbSwapped();

}
else
{
//qDebug()<<"ERROR:MatcouldnotbeconvertedtoQImage.";
return QImage();
}
}

void MainWindow::on_pushButton_2_clicked()
{
QImage tu1 = Mat2QImage(frame0);
char str1[1000] = "D:\\qtTest\\build-qtcaitu-Desktop_Qt_5_8_0_MSVC2013_32bit-Debug\\tu\\";
int t1;
t1 = time(NULL);
char s1[1000];
itoa(t1,s1,10);
char* s11 = s1;
strcat(str1,s11);
strcat(str1,"1.jpg");
tu1.save(str1,"jpg");
ui->label_tu1->setPixmap(QPixmap::fromImage(tu1));//将结果在label上显示
ui->label_tu1->setScaledContents(true);//使图像尺寸与label大小匹配
ui->label_tu1->setAutoFillBackground(true) ;

QImage tu2 = Mat2QImage(frame1);
char str2[1000] = "D:\\qtTest\\build-qtcaitu-Desktop_Qt_5_8_0_MSVC2013_32bit-Debug\\tu\\";
int t2;
t2 = time(NULL);
char s2[1000];
itoa(t2,s2,10);
char* s22 = s2;
strcat(str2,s22);
strcat(str2,"2.jpg");
tu2.save(str2,"jpg");
ui->label_tu2->setPixmap(QPixmap::fromImage(tu2));//将结果在label上显示
ui->label_tu2->setScaledContents(true);//使图像尺寸与label大小匹配
ui->label_tu2->setAutoFillBackground(true) ;

QImage tu3 = Mat2QImage(frame2);
char str3[1000] = "D:\\qtTest\\build-qtcaitu-Desktop_Qt_5_8_0_MSVC2013_32bit-Debug\\tu\\";
int t3;
t3 = time(NULL);
char s3[1000];
itoa(t3,s3,10);
char* s33 = s3;
strcat(str3,s33);
strcat(str3,"3.jpg");
tu3.save(str3,"jpg");
ui->label_tu3->setPixmap(QPixmap::fromImage(tu3));//将结果在label上显示
ui->label_tu3->setScaledContents(true);//使图像尺寸与label大小匹配
ui->label_tu3->setAutoFillBackground(true) ;

QImage tu4 = Mat2QImage(frame3);
char str4[1000] = "D:\\qtTest\\build-qtcaitu-Desktop_Qt_5_8_0_MSVC2013_32bit-Debug\\tu\\";
int t4;
t4 = time(NULL);
char s4[1000];
itoa(t4,s4,10);
char* s44 = s4;
strcat(str4,s44);
strcat(str4,"4.jpg");
tu4.save(str4,"jpg");
ui->label_tu4->setPixmap(QPixmap::fromImage(tu4));//将结果在label上显示
ui->label_tu4->setScaledContents(true);//使图像尺寸与label大小匹配
ui->label_tu4->setAutoFillBackground(true) ;

}

void MainWindow::on_pushButton_3_clicked()
{
stop = true;
exit(1);

}


[/code]


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ opencv qt