-
Notifications
You must be signed in to change notification settings - Fork 0
/
QtOpenCVTools.cpp
180 lines (167 loc) · 4.93 KB
/
QtOpenCVTools.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "QtOpenCVTools.h"
#include <QDebug>
using namespace cv;
cv::Mat QImageToMat(QImage image)
{
cv::Mat mat;
switch (image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return mat;
}
QImage MatToQImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if(mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for(int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for(int row = 0; row < mat.rows; row ++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if(mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if(mat.type() == CV_8UC4)
{
qDebug() << "CV_8UC4";
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
//左右摄像头载入编号,自己调一下
#define CAML 1
#define CAMR 2
//摄像头尺寸设置
#define WIDTH 352
#define HEIGHT 288
CvCapture *capture1 = NULL, *capture2 = NULL; //capture1为left, capture2为right
//Mat imageLeft;
//Mat imageRight;
IplImage* imageLeft;
IplImage* imageRight;
int cnt = 1;//拍照图片计数
char Leftname[25];//生成图像文件名
char Rightname[25];
bool JfzStereoImgGet(unsigned int cameraNum)
{
if(cameraNum == 0)
return true;
if(cameraNum == 1)
{
VideoCapture capture(0);
while (1)
{
Mat frame;
capture >> frame;
imshow("读取视频", frame);
char c = cvWaitKey(30);
if (c == 27)
{
break;
}
}
//载入摄像头
// capture1 = cvCaptureFromCAM(0);
// cvCreateCameraCapture();
// assert(capture1 != NULL);
// //设置画面尺寸 WIDTH、HEIGHT在宏定义中改
// cvSetCaptureProperty(capture1, CV_CAP_PROP_FRAME_WIDTH, WIDTH);
// cvSetCaptureProperty(capture1, CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
// qDebug() << "视频分辨率设置为:" << WIDTH << " x " << HEIGHT << endl;
imageLeft = cvQueryFrame(capture1);
qDebug() << "视频分辨率设置为:" << imageLeft->width << " x " << imageLeft->height << endl;
// while (true)
// {
// imageLeft = cvQueryFrame(capture1);
// imshow("Left", Mat(cvarrToMat(imageLeft)));
// char c = waitKey(100);
// if (c == '1') //按'g'保存照片
// {
// // sprintf(Leftname, "stereoData\\Left%02d.jpg", cnt);
// // sprintf(Rightname, "stereoData\\Right%02d.jpg", cnt);
// // imwrite(Leftname, imageLeft);
// // imwrite(Rightname, imageRight);
// cnt++;
// qDebug() << Leftname <<" 和 "<<Rightname << " 保存成功!" << endl;
// }
// if (c == 27) //按ESC键退出
// return true;
// }
}
if(cameraNum == 2)
{
//载入摄像头
capture1 = cvCaptureFromCAM(CAML);
assert(capture1 != NULL);
cvWaitKey(100);
capture2 = cvCaptureFromCAM(CAMR);
assert(capture2 != NULL);
//设置画面尺寸 WIDTH、HEIGHT在宏定义中改
cvSetCaptureProperty(capture1, CV_CAP_PROP_FRAME_WIDTH, WIDTH);
cvSetCaptureProperty(capture1, CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
cvSetCaptureProperty(capture2, CV_CAP_PROP_FRAME_WIDTH, WIDTH);
cvSetCaptureProperty(capture2, CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
qDebug() << "视频分辨率设置为:" << WIDTH << " x " << HEIGHT << endl;
imageLeft = cvQueryFrame(capture1);
imageRight = cvQueryFrame(capture2);
while (true)
{
imageLeft = cvQueryFrame(capture1);
imageRight = cvQueryFrame(capture2);
imshow("Left", Mat(cvarrToMat(imageLeft)));
imshow("Right", Mat(cvarrToMat(imageRight)));
char c = waitKey(100);
if (c == '1') //按'g'保存照片
{
// sprintf(Leftname, "stereoData\\Left%02d.jpg", cnt);
// sprintf(Rightname, "stereoData\\Right%02d.jpg", cnt);
// imwrite(Leftname, imageLeft);
// imwrite(Rightname, imageRight);
cnt++;
qDebug() << Leftname <<" 和 "<<Rightname << " 保存成功!" << endl;
}
}
char c = waitKey();
if (c == 27) //按ESC键退出
return true;
}
}