您的位置:首页 > 编程语言 > Qt开发

qt5.1+ vs2013+opencv2.4.8制作视频图片读取界面

2017-12-08 12:33 411 查看
准备研究图像去雾这块,得用到界面,所以先制作这样一个界面备用,后续再改进

(1)    建立QT工程
(2)    在QT上绘制相应图形,改名,改对象名称(OpenImageButton,
OpenCameraButton, CloseCameraButton, DisplayFrame),布局,使用三个Push Button,一个Label,保存。



(3)    image_camera_read.h中添加

#include<opencv2/core/core.hpp>
#include<opencv2/imgp
4000
roc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
//这些是我习惯性添加的,有些可能不需要
using namespace std;
using namespace cv;

QImage Mat2Qimage(const Mat& mat);

bool LoopStop;

private slots:
void OpenImageSlot();
void OpenCameraSlot();
void CloseCameraSlot();




(4)image_camera_read.cpp中添加
void image_camera_read::OpenImageSlot()
{
Mat image = imread("elephant.jpg");//将图片存储在相应文件夹下
QImage img = Mat2Qimage(image);
ui.DisplayFrame->setPixmap(QPixmap::fromImage(img));
ui.DisplayFrame->resize(ui.DisplayFrame->pixmap()->size());//DisplayFrame适应图片大小
ui.DisplayFrame->show();
waitKey(1000);
}

void image_camera_read::OpenCameraSlot()
{
VideoCapture cap(0);//打开摄像头
if (cap.isOpened())
{
cout << "没有摄像头" << endl;
}
Mat frame;
LoopStop = false;//循环检查
while (!LoopStop)
{
cap >> frame;//frame接受摄像头的帧
imshow("camera", frame);
QImage img = Mat2Qimage(frame);//将Mat格式转换成QImage格式
ui.DisplayFrame->setPixmap(QPixmap::fromImage(img));//在DisplayFrame中显示
int t = waitKey(10);//每一帧间隔10ms
if (t >= 0)//任意键暂停
{
waitKey(0);
}
}
cvDestroyWindow("camera");
}
void image_camera_read::CloseCameraSlot()
{
LoopStop = true;
}

QImage image_camera_read::Mat2Qimage(const Mat& mat)//Mat格式转换成QImage格式网上有很详细的说明
{
if (mat.type() == CV_8UC1)
{
QVector<QRgb>colorTable;
for (int i = 0; i<256; i++)
colorTable.push_back(qRgb(i, i, i));
const uchar*qImageBuffer = (const uchar*)mat.data;
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
img.setColorTable(colorTable);
return img;
}
if (mat.type() == CV_8UC3)
{
const uchar*qImageBuffer = (const uchar*)mat.data;
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return img.rgbSwapped();

}
else
{
return QImage();
}
}


(5)连接信号与槽
QObject::connect(ui.OpenImageButton, SIGNAL(clicked()), this, SLOT(OpenImageSlot()));//"OpenCameraSlot()"
QObject::connect(ui.OpenCameraButton,SIGNAL(clicked()),this,SLOT(OpenCameraSlot()));
QObject::connect(ui.CloseCameraButton, SIGNAL(clicked()), this, SLOT(CloseCameraSlot()));




(6)结果如下
打开图片



打开摄像头

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