-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCannyWebcamQt.cpp
More file actions
191 lines (141 loc) · 7.25 KB
/
CannyWebcamQt.cpp
File metadata and controls
191 lines (141 loc) · 7.25 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
CannyWebcamQt
this is not an actual C++ file !!!
this file is 3 files in one:
-the Qt project file (.pro)
-the main form .h file (ex frmmain.h)
-the main form .cpp file (ex frmmain.cpp)
follow the video to create the project, place widgets on your form,
and have Qt Creator write as much of the code for you as possible,
then copy/paste ONLY THE ADDITIONAL PORTIONS from the code below:
for this program the widgets you need to add are:
lblOriginal (QLabel)
lblCanny (QLabel)
####################################################################################################
## CannyWebcamQt.pro (1 of 3) ######################################################################
####################################################################################################
#-------------------------------------------------
#
# Project created by QtCreator 2015-05-24T23:14:43
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CannyWebcam1
TEMPLATE = app
SOURCES += main.cpp\
frmmain.cpp
HEADERS += frmmain.h
FORMS += frmmain.ui
INCLUDEPATH += C:\\OpenCV-2.4.11\\opencv\\build\\include
LIBS += -LC:\\OpenCV-2.4.11\\mybuild\\lib\\Debug \
-lopencv_calib3d2411d \
-lopencv_contrib2411d \
-lopencv_core2411d \
-lopencv_features2d2411d \
-lopencv_flann2411d \
-lopencv_gpu2411d \
-lopencv_haartraining_engined \
-lopencv_highgui2411d \
-lopencv_imgproc2411d \
-lopencv_legacy2411d \
-lopencv_ml2411d \
-lopencv_nonfree2411d \
-lopencv_objdetect2411d \
-lopencv_ocl2411d \
-lopencv_photo2411d \
-lopencv_stitching2411d \
-lopencv_superres2411d \
-lopencv_ts2411d \
-lopencv_video2411d \
-lopencv_videostab2411d
# Note: it is recommended to leave a blank line at the end of your .pro file
///////////////////////////////////////////////////////////////////////////////////////////////////
// frmmain.h (2 of 3) /////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef FRMMAIN_H
#define FRMMAIN_H
#include <QMainWindow>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace Ui {
class frmMain;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
class frmMain : public QMainWindow {
Q_OBJECT
public slots:
void processFrameAndUpdateGUI(); // function prototype
public:
explicit frmMain(QWidget *parent = 0);
~frmMain();
private:
Ui::frmMain *ui;
cv::VideoCapture capWebcam; // Capture object to use with webcam
cv::Mat matOriginal; // input image
cv::Mat matGrayscale; // grayscale of input image
cv::Mat matBlurred; // intermediate blured image
cv::Mat matCanny; // Canny edge image
QTimer* qtimer; // timer for processFrameAndUpdateGUI()
QImage frmMain::matToQImage(cv::Mat mat); // function prototype
void frmMain::exitProgram(); // function prototype
};
#endif // FRMMAIN_H
///////////////////////////////////////////////////////////////////////////////////////////////////
// frmmain.cpp (3 of 3) ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "frmmain.h"
#include "ui_frmmain.h"
#include<QtCore>
#include<QMessageBox>
// constructor ////////////////////////////////////////////////////////////////////////////////////
frmMain::frmMain(QWidget *parent) : QMainWindow(parent), ui(new Ui::frmMain) {
ui->setupUi(this);
capWebcam.open(0); // associate the capture object to the default webcam
if(capWebcam.isOpened() == false) { // if unsuccessful
QMessageBox::information(this, "", "error: capWebcam not accessed successfully \n\n exiting program\n"); // show error message
exitProgram(); // and exit program
return; //
}
qtimer = new QTimer(this); // instantiate timer
connect(qtimer, SIGNAL(timeout()), this, SLOT(processFrameAndUpdateGUI())); // associate timer to processFrameAndUpdateGUI
qtimer->start(20); // start timer, set to cycle every 20 msec (50x per sec), it will not actually cycle this often
}
// destructor /////////////////////////////////////////////////////////////////////////////////////
frmMain::~frmMain() {
delete ui;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void frmMain::exitProgram() {
if(qtimer->isActive()) qtimer->stop(); // if timer is running, stop timer
QApplication::quit(); // and exit program
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void frmMain::processFrameAndUpdateGUI() {
bool blnFrameReadSuccessfully = capWebcam.read(matOriginal); // get next frame from the webcam
if (!blnFrameReadSuccessfully || matOriginal.empty()) { // if we did not get a frame
QMessageBox::information(this, "", "unable to read from webcam \n\n exiting program\n"); // show error via message box
exitProgram(); // and exit program
return; //
}
cv::cvtColor(matOriginal, matGrayscale, CV_BGR2GRAY); // convert to grayscale
cv::GaussianBlur(matGrayscale,matBlurred, cv::Size(5, 5), 1.8); // blur
cv::Canny(matBlurred, matCanny, 50, 100); // get Canny edges
QImage qimgOriginal = matToQImage(matOriginal); // convert from OpenCV Mat to Qt QImage
QImage qimgCanny = matToQImage(matCanny); //
ui->lblOriginal->setPixmap(QPixmap::fromImage(qimgOriginal)); // show images on form labels
ui->lblCanny->setPixmap(QPixmap::fromImage(qimgCanny)); //
}
///////////////////////////////////////////////////////////////////////////////////////////////////
QImage frmMain::matToQImage(cv::Mat mat) {
if(mat.channels() == 1) { // if 1 channel (grayscale or black and white) image
return QImage((uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8); // return QImage
} else if(mat.channels() == 3) { // if 3 channel color image
cv::cvtColor(mat, mat, CV_BGR2RGB); // flip colors
return QImage((uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888); // return QImage
} else {
qDebug() << "in openCVMatToQImage, image was not 1 channel or 3 channel, should never get here";
}
return QImage(); // return a blank QImage if the above did not work
}