-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopencvwidget.cpp
More file actions
208 lines (161 loc) · 6.24 KB
/
Copy pathopencvwidget.cpp
File metadata and controls
208 lines (161 loc) · 6.24 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Author: Alberto G. Lagos (Kronen)
Copyright (C) 2010 Alberto G. Lagos (Kronen)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "opencvwidget.h"
#include <QDebug>
#include <QFileInfo>
OpenCVWidget::OpenCVWidget(QWidget *parent) : QWidget(parent) {
mDetectingFaces = false;
mTrackingFace = false;
mFlipV = mFlipH = false;
mVideoWriter = 0;
mFps = 16;
mCvRect = cvRect(-1, -1, 0, 0);
// Camera Initialization
mCamera = cvCaptureFromCAM(CV_CAP_ANY);
if(mCamera) {
// Get a query frame to initialize the capture and to get the frame's dimensions
IplImage* frame = cvQueryFrame(mCamera);
this->setMinimumSize(frame->width, frame->height);
// QImage to draw on paint event
mImage = QImage(QSize(frame->width, frame->height), QImage::Format_RGB888);
// IplImage * to work with OpenCV functions
mCvImage = cvCreateImageHeader(cvSize(frame->width, frame->height), 8, 3);
// Share the buffer between QImage and IplImage *
mCvImage->imageData = (char *)mImage.bits();
// Init Face Detection and Face Tracking
mFaceDetect = new FaceDetect();
mFaceDetect->setFlags(CV_HAAR_FIND_BIGGEST_OBJECT); // default
mCamShift = new CamShift(cvSize(frame->width, frame->height));
// Try to load a default cascade file
QFileInfo cascadeFile("haarcascades/haarcascade_frontalface_alt2.xml");
if(cascadeFile.exists()) mFaceDetect->setCascadeFile(cascadeFile.absoluteFilePath());
// We call queryFrame 'mFps' times per second
mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()), this, SLOT(queryFrame()));
mTimer->start(1000/mFps);
}
}
OpenCVWidget::~OpenCVWidget() {
if(mFaceDetect) delete mFaceDetect;
if(mCamShift) delete mCamShift;
cvReleaseCapture(&mCamera);
}
bool OpenCVWidget::isCaptureActive() const {
return bool(mCamera);
}
bool OpenCVWidget::isFaceDetectAvalaible() const {
return !mFaceDetect->cascadeFile().isEmpty();
}
void OpenCVWidget::queryFrame() {
IplImage* frame = cvQueryFrame(mCamera);
if(!frame) return;
// We copy the frame to our buffer(fliping it if necessary)
if(!(mFlipV ^ (frame->origin == IPL_ORIGIN_TL))) cvFlip(frame, mCvImage, 0);
else cvCopy(frame, mCvImage, 0);
if(mFlipH) cvFlip(mCvImage, mCvImage, 1);
if(mVideoWriter) cvWriteFrame(mVideoWriter, frame);
if(mDetectingFaces) mListRect = mFaceDetect->detectFaces(mCvImage);
if(mTrackingFace) {
// Check if we have a valid rect. If we have a valid one, we track the face,
// if not we get a face rect first
if(!(mCvRect.width > 0 && mCvRect.height > 0)) {
// Detect the Face
QVector<QRect> listRect = mFaceDetect->detectFaces(mCvImage);
if(!listRect.isEmpty()) {
QRect trackRect = listRect.at(0);
mCvRect = cvRect(trackRect.x(), trackRect.y(), trackRect.width(), trackRect.height());
mCamShift->startTracking(mCvImage, mCvRect);
}
} else {
// Track the Face
mCvBox = mCamShift->trackFace(mCvImage);
cvEllipseBox(mCvImage, mCvBox, CV_RGB(255,0,0), 3, CV_AA, 0);
}
}
// Convert it from BGR to RGB. QImage works with RGB and cvQueryFrame returns a BGR IplImage
cvCvtColor(mCvImage, mCvImage, CV_BGR2RGB);
update();
}
void OpenCVWidget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
if(!mImage.isNull()) painter.drawPixmap(0, 0, QPixmap::fromImage(mImage));
if(!mListRect.empty()) {
QPen pen(palette().dark().color(), 4, Qt::SolidLine, Qt::FlatCap, Qt::BevelJoin);
painter.setPen(pen);
foreach(QRect rect, mListRect) painter.drawEllipse(rect);
// Clean the list when we have painted the rects
mListRect.clear();
}
}
void OpenCVWidget::saveScreenshot() {
int i = 0;
QString filename = QString("webcamPic%1.jpg").arg(i);
while(QFileInfo(filename).exists())
filename = QString("webcamPic%1.jpg").arg(i++);
if(!mImage.isNull()) mImage.save(filename, "JPG", 80);
}
void OpenCVWidget::videoWrite() {
int i = 1;
QString filename = QString("webcamVid%1.avi").arg(i);
while(QFileInfo(filename).exists())
filename = QString("webcamVid%1.avi").arg(i++);
CvSize size = cvGetSize(mCvImage);
// It seems that my camera don't get more than 8 fps at 640x480
mVideoWriter = cvCreateVideoWriter(filename.toUtf8(), CV_FOURCC('D','I','V','X'), 8, size);
}
void OpenCVWidget::videoStop() {
cvReleaseVideoWriter(&mVideoWriter);
}
void OpenCVWidget::setDetectFaces(bool detect) {
mDetectingFaces = detect;
}
void OpenCVWidget::setTrackFace(bool track) {
mTrackingFace = track;
if(!mTrackingFace) mCvRect = cvRect(-1, -1, 0, 0);
}
void OpenCVWidget::setFaceDetectFlags(int flags) {
mFaceDetect->setFlags(flags);
}
void OpenCVWidget::switchFlipH() {
mFlipH = !mFlipH;
}
void OpenCVWidget::switchFlipV() {
mFlipV = !mFlipV;
}
bool OpenCVWidget::flipH() const {
return mFlipH;
}
bool OpenCVWidget::flipV() const {
return mFlipV;
}
void OpenCVWidget::setCascadeFile(QString filename) {
mFaceDetect->setCascadeFile(filename);
}
QString OpenCVWidget::cascadeFile() const {
if(mFaceDetect) return mFaceDetect->cascadeFile();
else return "";
}
void OpenCVWidget::setCamShiftVMin(int vMin) {
mCamShift->setVMin(vMin);
}
void OpenCVWidget::setCamShiftSMin(int sMin) {
mCamShift->setSMin(sMin);
}
int OpenCVWidget::camshiftVMin() const {
return mCamShift->vMin();
}
int OpenCVWidget::camshiftSMin() const{
return mCamShift->sMin();
}