龙空技术网

使用OpenCV库操作摄像头拍照、调节参数和视频录制

图像算法 978

前言:

当前朋友们对“javaopencv调用摄像头”大体比较珍视,同学们都需要学习一些“javaopencv调用摄像头”的相关资讯。那么小编也在网摘上汇集了一些有关“javaopencv调用摄像头””的相关文章,希望看官们能喜欢,姐妹们快快来学习一下吧!

需求

使用OpenCV做功能,播放摄像头(usb和网络),对摄像头设备进行参数调整(亮度、对比度、饱和度、色调、增益、曝光度)调节,拍照和录像。

原理

使用OpenCV打开摄像头(可打开USB和网路哦摄像头),渲染图像显示,可使用OpenCV属性调整摄像头的各项参数,使用拍照可以将当前图片拍照,使用录像可以从当前时间点开始录像直至停止录像

注意

目前测试,即使PC上有编码器,但是OpenCV存储mat为对应的录像视频文件失败,出现:

录制完视频大小为200多B(基本为0),mp4格式时(查看入坑一)

录制完视频大小为6KB,avi格式时

录制avi传入图像mat,源码内部出现错误宕机

运行效果:

核心代码

打开摄像头代码

bool OpenCVManager::startCapture(int usb, int width, int height){ if(!_pVideoCapture->open(usb)) { qDebug() get(CV_CAP_PROP_FRAME_HEIGHT); _pVideoCapture->set(CV_CAP_PROP_FPS, 25); _brightness = _pVideoCapture->get(cv::CAP_PROP_BRIGHTNESS); _contrast = _pVideoCapture->get(cv::CAP_PROP_CONTRAST); _saturation = _pVideoCapture->get(cv::CAP_PROP_SATURATION); _hue = _pVideoCapture->get(cv::CAP_PROP_HUE); _gain = _pVideoCapture->get(cv::CAP_PROP_GAIN); _exposure = _pVideoCapture->get(cv::CAP_PROP_EXPOSURE); QTimer::singleShot(0, this, SLOT(slot_captrueFrame())); return true;}

调整属性代码

bool OpenCVManager::getShowProperty() const{ return _showProperty;}void OpenCVManager::setShowProperty(bool value){ _showProperty = value;}double OpenCVManager::getBrightness(){ return _brightness;}void OpenCVManager::setBrightness(double value){ _brightness = value; if(_pVideoCapture) { _pVideoCapture->set(cv::CAP_PROP_BRIGHTNESS, _brightness); }}double OpenCVManager::getContrast() const{ return _contrast;}void OpenCVManager::setContrast(double value){ _contrast = value; if(_pVideoCapture) { _pVideoCapture->set(cv::CAP_PROP_CONTRAST, _contrast); }}double OpenCVManager::getSaturation() const{ return _saturation;}void OpenCVManager::setSaturation(double value){ _saturation = value; if(_pVideoCapture) { _pVideoCapture->set(cv::CAP_PROP_SATURATION, _saturation); }}double OpenCVManager::getHue() const{ return _hue;}void OpenCVManager::setHue(double value){ _hue = value; if(_pVideoCapture) { _pVideoCapture->set(cv::CAP_PROP_HUE, _hue); }}double OpenCVManager::getGain() const{ return _gain;}void OpenCVManager::setGain(double value){ _gain = value; if(_pVideoCapture) { _pVideoCapture->set(cv::CAP_PROP_GAIN, _gain); }}double OpenCVManager::getExposure() const{ return _exposure;}void OpenCVManager::setExposure(double value){ _exposure = value; if(_pVideoCapture) { _pVideoCapture->set(cv::CAP_PROP_EXPOSURE, _exposure); }}

拍照代码

void PhotoAndRecordWidget::on_pushButton_photo_clicked(){ QString timeStr = QDateTime::currentDateTime().toString("yyyy-MM-hh hh_mm_ss"); QString dirName = "photos"; if(!QFile::exists(dirName)) { QDir dir; if(!dir.mkdir(dirName)) { ui->label_state->setText("创建文件夹 " + dirName + "失败!!!"); } } QString filePath = QString("%1/%2.png").arg(dirName).arg(timeStr); if(_image.save(filePath)) { ui->label_state->setText("保存照片至: " + filePath); }else{ ui->label_state->setText("保存照片失败!!!"); }}

录像代码

开启录像

void OpenCVManager::startRecord(QString pathFile){ // 多线程处理 QMetaObject::invokeMethod(this, "slot_startRecord", Qt::DirectConnection, Q_ARG(QString, pathFile));}void OpenCVManager::slot_startRecord(QString filePath){ if(_pVideoWrite) { qDebug()

标签: #javaopencv调用摄像头 #摄像头算法 底库 #摄像头算法 底库怎么设置 #opencv 多摄像头 #opencv打开多个usb摄像头