-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
181 lines (118 loc) · 4.56 KB
/
main.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
181
#pragma ide diagnostic ignored "EndlessLoop"
#include <opencv2/opencv.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <iostream>
#include <Windows.h>
#include <ctime>
#include "GUI/guiFuncs.h"
#include "GUI/draggableGUI.h"
#include "GUI/guiColors.h"
#include "tabletCalc.h"
#include "mouseFuncs.h"
#include "screenRes.h"
using namespace cv;
using namespace std;
vector<int> camPos = {20, 20};
vector<int> tabletCamPos = {-1,-1};
string windowName = "Webcam Mouse";
int frameCount = 0;
int lastFps = 0;
float avgFps = 0;
auto prevTime = clock();
void fpsCounter(Mat& canvas){
auto currTime = clock();
auto delta = currTime - prevTime;
prevTime = currTime;
avgFps += (float)(CLOCKS_PER_SEC / delta);
gF::drawTextOutline(canvas, to_string(lastFps) + " FPS", camPos[0]+1, camPos[1] + 14, 0, 255, 0);
if(frameCount % 10 == 0){
lastFps = (int) avgFps/10;
avgFps = 0;
}
}
int lastValProcess = 0;
float avgProcess = 0;
auto prevProcess = clock();
void startProcess(){
prevProcess = clock();
}
void drawProcessTime(Mat& canvas){
auto currTime = clock();
auto delta = currTime - prevProcess;
avgProcess += ((float)1000*delta / CLOCKS_PER_SEC);
gF::drawTextOutline(canvas, to_string(lastValProcess) + "ms process time", camPos[0]+1, camPos[1] + 33, 0, 255, 0);
if(frameCount % 10 == 0){
lastValProcess = (int) avgProcess / 10;
avgProcess = 0;
}
}
int main() {
VideoCapture cam(0, CAP_DSHOW);
cam.set(CAP_PROP_FPS, 60);
cam.set(CAP_PROP_FRAME_WIDTH, 640);
cam.set(CAP_PROP_FRAME_HEIGHT, 480);
int camWidth = cam.get(CAP_PROP_FRAME_WIDTH);
int camHeight = cam.get(CAP_PROP_FRAME_HEIGHT);
tabletCamPos = {camPos[0] + camWidth + 20, camPos[1]};
sR::getScreenResolution();
namedWindow(windowName);
setMouseCallback(windowName, mF::onMouse, NULL);
HWND hwnd = (HWND) cvGetWindowHandle(windowName.c_str());
Mat canvas(520, 960, CV_8UC3);
Mat webcamImg;
gF::initSlider();
while (IsWindowVisible(hwnd)) {
canvas.setTo(gC::BACKGROUND);
uint8_t *canvasPtr = (uint8_t *) canvas.data;
cam >> webcamImg;
startProcess();
resize(webcamImg, webcamImg, Size(640, 480), 0, 0, INTER_CUBIC);
uint8_t *pixelPtr = (uint8_t *) webcamImg.data;
dG::checkMoveTabletSurface(camWidth, camHeight);
Mat out = tC::warpedMat(dG::tabletArea[0], dG::tabletArea[1], dG::tabletArea[2], dG::tabletArea[3], webcamImg,
dG::tabletCamWidth,
dG::tabletCamHeight,
gF::tabletRotIndex,
gF::mirrorButton[2]);
uint8_t *tabletPtr = (uint8_t *) out.data;
tC::moveMouse(out, tabletPtr,
gF::baseColor,
gF::baseDistance,
gF::tipExtra,
gF::leftHandedButton[2],
gF::mouseEnabled[2],
dG::screenAreaWidth, dG::screenAreaHeight, dG::screenAreaPivot);
dG::checkMoveFullScreenSurface();
dG::checkMoveScreenSurface();
dG::screenSurfaceGUI(out, tabletCamPos);
dG::tabletCameraWidthGUI(canvas, tabletCamPos);
int windowBorder = 4;
rectangle(canvas,
Point_(tabletCamPos[0] - windowBorder, tabletCamPos[1] - windowBorder),
Point_(tabletCamPos[0] + out.cols + windowBorder, tabletCamPos[1] + out.rows + windowBorder),
gC::WINDOW_OUTLINE, -1);
out.copyTo(canvas(Rect(tabletCamPos[0], tabletCamPos[1], out.cols, out.rows)));
dG::tabletSurfaceGUI(webcamImg, pixelPtr, camPos);
rectangle(canvas,
Point_(camPos[0] - windowBorder, camPos[1] - windowBorder),
Point_(camPos[0] + webcamImg.cols + windowBorder, camPos[1] + webcamImg.rows + windowBorder),
gC::WINDOW_OUTLINE, -1);
webcamImg.copyTo(canvas(Rect(camPos[0], camPos[1], webcamImg.cols, webcamImg.rows)));
gF::setGUIPos(camPos, camWidth, dG::tabletCamHeight);
gF::drawSliderGUI(canvas);
gF::drawButtonGUI(canvas);
fpsCounter(canvas);
drawProcessTime(canvas);
imshow(windowName, canvas);
if (mF::justClicked) mF::justClicked = false;
mF::checkHotKey();
if (mF::abortHotKey==1) {
gF::mouseEnabled[2] = !gF::mouseEnabled[2];
}
frameCount++;
int pressed = waitKey(1) & 0xFF;
}
cam.release();
return 0;
}