-
Notifications
You must be signed in to change notification settings - Fork 0
/
CannyWebcamWithQtGUI.cpp
139 lines (101 loc) · 5.93 KB
/
CannyWebcamWithQtGUI.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
CannyWebcamQt
this is not an actual C++ file !!!
this file is 2 files in one:
-the main form .h file (ex frmmain.h)
-the main form .cpp file (ex frmmain.cpp)
follow the video to create the project, edit the .pro file, 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)
///////////////////////////////////////////////////////////////////////////////////////////////////
// frmmain.h (1 of 2) /////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#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
QTimer* qtimer; // timer for processFrameAndUpdateGUI()
QImage frmMain::convertOpenCVMatToQtQImage(cv::Mat mat); // function prototype
void frmMain::exitProgram(); // function prototype
};
#endif // FRMMAIN_H
///////////////////////////////////////////////////////////////////////////////////////////////////
// frmmain.cpp (2 of 2) ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#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() {
cv::Mat imgOriginal; // input image
cv::Mat imgGrayscale; // grayscale of input image
cv::Mat imgBlurred; // intermediate blured image
cv::Mat imgCanny; // Canny edge image
bool blnFrameReadSuccessfully = capWebcam.read(imgOriginal); // get next frame from the webcam
if (!blnFrameReadSuccessfully || imgOriginal.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(imgOriginal, imgGrayscale, CV_BGR2GRAY); // convert to grayscale
cv::GaussianBlur(imgGrayscale, imgBlurred, cv::Size(5, 5), 1.8); // blur
cv::Canny(imgBlurred, imgCanny, 50, 100); // get Canny edges
QImage qimgOriginal = convertOpenCVMatToQtQImage(imgOriginal); // convert from OpenCV Mat to Qt QImage
QImage qimgCanny = convertOpenCVMatToQtQImage(imgCanny); //
ui->lblOriginal->setPixmap(QPixmap::fromImage(qimgOriginal)); // show images on form labels
ui->lblCanny->setPixmap(QPixmap::fromImage(qimgCanny)); //
}
///////////////////////////////////////////////////////////////////////////////////////////////////
QImage frmMain::convertOpenCVMatToQtQImage(cv::Mat mat) {
if(mat.channels() == 1) { // if grayscale image
return QImage((uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8); // declare and return a QImage
} else if(mat.channels() == 3) { // if 3 channel color image
cv::cvtColor(mat, mat, CV_BGR2RGB); // invert BGR to RGB
return QImage((uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888); // declare and return a QImage
} else {
qDebug() << "in convertOpenCVMatToQtQImage, image was not 1 channel or 3 channel, should never get here";
}
return QImage(); // return a blank QImage if the above did not work
}