/
grabvideo.cpp
executable file
·74 lines (73 loc) · 2.86 KB
/
grabvideo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "grabvideo.h"
/*不可重入线程*/
GrabVideo::GrabVideo(QObject *parent) :
QThread(parent)
{
/*功能编程*/
capture = cvCreateCameraCapture(0); //初始化摄像头读取视频
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH,320);
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT,240);
cvSetCaptureProperty(capture,CV_CAP_PROP_FPS,20);
}
void GrabVideo::run(){
while(true){
pImg = cvQueryFrame(capture);
uchar *qImageBuffer = NULL;
int x ,y = 0;
int width = pImg->width;
int height = pImg->height;
int widthStep = pImg->widthStep;
/*struct IplImage to QImage*/
switch(pImg->depth){
case IPL_DEPTH_8U: //OpenCV 图像深度8U
qDebug("IplImage Depth is IPL_DEPTH_8U");
if(pImg->nChannels == 1){
qDebug("IplImage nChannel is 1");
qImageBuffer = (uchar*)malloc(width * height * sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uchar *IplImagePtr = (const uchar*)pImg->imageData;
for(y = 0;y < height;y++){
memcpy(QImagePtr,IplImagePtr,width);
QImagePtr += width;
IplImagePtr += widthStep;
}
tempImg = new QImage(qImageBuffer,width,height,QImage::Format_Indexed8);
}else if(pImg->nChannels == 3){
qDebug("IplImage nChannel is 3");
qImageBuffer = (uchar*)malloc(4 * width * height * sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uchar *IplImagePtr = (const uchar*)pImg->imageData;
for(y = 0;y < height;y++){
for(x = 0;x < width;x++){
QImagePtr[0] = IplImagePtr[0];
QImagePtr[1] = IplImagePtr[1];
QImagePtr[2] = IplImagePtr[2];
QImagePtr[3] = 0;
QImagePtr += 4;
IplImagePtr += 3;
}
IplImagePtr += widthStep-3*width;
}
tempImg = new QImage(qImageBuffer,width,height,QImage::Format_RGB32);
}else{
qDebug("Camera IplImage channels can not support");
}
break;
case IPL_DEPTH_16U: //OpenCV 图像深度16U
qDebug("IplImage Depth is IPL_DEPTH_16U");
break;
case IPL_DEPTH_32F://OpenCV 图像深度32U
qDebug("IplImage Depth is IPL_DEPTH_16U");
break;
case IPL_DEPTH_64F://OpenCV 图像深度64U
qDebug("IplImage Depth is IPL_DEPTH_16U");
break;
}
emit sendQImage(tempImg);
}
}
GrabVideo::~GrabVideo(){
delete pImg;
cvReleaseCapture(&capture);
delete tempImg;
}