-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouseInput.cpp
236 lines (206 loc) · 6.01 KB
/
mouseInput.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <iostream>
using namespace std;
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
using namespace cv;
#include<Windows.h>
POINT t1;
int thresh=10;
int clickThreshR=0;
int clickThreshB=0;
bool nomove=true;
bool clickon=true;
int mouseon=0;
int screenHeight=GetSystemMetrics(SM_CYSCREEN)+200;
int screenWidth=GetSystemMetrics(SM_CXSCREEN)+300;
int camWidth=752;
int camHeight=416;
void thresFunction(int x,void* data)
{
*(int*)data = x;
}
void LeftClick()
{
INPUT Input={0};
// left down
Input.type= INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1,&Input,sizeof(INPUT));
// left up
::ZeroMemory(&Input,sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1,&Input,sizeof(INPUT));
}
void RightClick()
{
INPUT Input={0};
// Right down
Input.type= INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
::SendInput(1,&Input,sizeof(INPUT));
// Right up
::ZeroMemory(&Input,sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
::SendInput(1,&Input,sizeof(INPUT));
}
void detectClickR(double curArea, double lastArea)
{
if((curArea-lastArea)>clickThreshR)
{ clickon=false;
cout<<"Click!!! LEFT"<<endl;
if (mouseon)
{
LeftClick();
clickon=true;
}
}
else
cout<< "No Click! LEFT"<<endl;
}
void detectClickB(double curArea, double lastArea)
{
if((curArea-lastArea)>clickThreshB)
{ clickon=false;
cout<<"Click!!! RIGHT"<<endl;
if (mouseon)
{
RightClick();
clickon=true;
}
}
else
cout<< "No Click! RIGHT"<<endl;
}
void leftX()
{
nomove=false;
cout << "Left" <<endl;
if(mouseon) SetCursorPos(t1.x-mouseon,t1.y);
}
void rightX()
{
nomove=false;
cout << "Right" <<endl;
if(mouseon) SetCursorPos(t1.x+mouseon,t1.y);
}
void upY()
{ nomove=false;
cout << "Up" <<endl;
if(mouseon) SetCursorPos(t1.x,t1.y+mouseon);
}
void downY()
{
nomove=false;
cout << "Down" <<endl;
if(mouseon) SetCursorPos(t1.x,t1.y-mouseon);
}
void checkmotionR(double curValX,double curValY, double lastValX,double lastValY)
{
if(mouseon && clickon)
{
if(abs(curValX-lastValX)>thresh)
SetCursorPos((curValX/camWidth)*screenWidth,(curValY/camHeight)*screenHeight);
if(abs(curValY-lastValY)>thresh)
SetCursorPos((curValX/camWidth)*screenWidth,(curValY/camHeight)*screenHeight);
}
}
/*
void checkmotion(double curValX,double curValY, double lastValX,double lastValY)
{
nomove=true;
GetCursorPos(&t1);
if(curValX>lastValX+thresh)
{rightX();}
if(curValY>lastValY+thresh)
{downY();}
if(curValX<lastValX-thresh)
{leftX();}
if(curValY<lastValY-thresh)
{upY();}
if(nomove)
cout<< "No mov" <<endl;
}
*/
int main(int argc, char** argv)
{
VideoCapture cam(0);
Moments momentR,momentB;
Mat frame,frameR,frameB;
double xmeanCurR,ymeanCurR,xmeanLastR=0,ymeanLastR=0,xmeanCurB,ymeanCurB,xmeanLastB=0,ymeanLastB=0;
double curAreaR,lastAreaR=0,curAreaB,lastAreaB=0;
int RLr=0,RHr=0,GLr=0,GHr=0,BLr=0,BHr=0;
int RLb=0,RHb=0,GLb=0,GHb=0,BLb=0,BHb=0;
namedWindow("WinR");namedWindow("WinB");
namedWindow("RedThreshold");
namedWindow("BlueThreshold");
namedWindow("Adjustments");
CV_Assert(cam.isOpened());
cam.set(CV_CAP_PROP_FRAME_WIDTH,camWidth);
cam.set(CV_CAP_PROP_FRAME_HEIGHT,camHeight);
/* FOR CAMERA PROBLEM */
do
{cam.read(frame);
}while(frame.empty());
/* FOR CAMERA PROBLEM */
int sizethreshinit = 150;
createTrackbar("pxThresh","Adjustments",NULL,255,thresFunction,&thresh);
createTrackbar("sizeThreshR","Adjustments",&sizethreshinit,2000,thresFunction,&clickThreshR);
createTrackbar("sizeThreshB","Adjustments",&sizethreshinit,2000,thresFunction,&clickThreshB);
createTrackbar("MouseMove","Adjustments",NULL,100,thresFunction,&mouseon);
createTrackbar("R_HIGHr","RedThreshold",&RHr,255,thresFunction,&RHr);
createTrackbar("R_LOWr","RedThreshold",&RLr,255,thresFunction,&RLr);
createTrackbar("G_HIGHr","RedThreshold",&GHr,255,thresFunction,&GHr);
createTrackbar("G_LOWr","RedThreshold",&GLr,255,thresFunction,&GLr);
createTrackbar("B_HIGHr","RedThreshold",&BHr,255,thresFunction,&BHr);
createTrackbar("B_LOWr","RedThreshold",&BLr,255,thresFunction,&BLr);
createTrackbar("R_HIGHb","BlueThreshold",&RHb,255,thresFunction,&RHb);
createTrackbar("R_LOWb","BlueThreshold",&RLb,255,thresFunction,&RLb);
createTrackbar("G_HIGHb","BlueThreshold",&GHb,255,thresFunction,&GHb);
createTrackbar("G_LOWb","BlueThreshold",&GLb,255,thresFunction,&GLb);
createTrackbar("B_HIGHb","BlueThreshold",&BHb,255,thresFunction,&BHb);
createTrackbar("B_LOWb","BlueThreshold",&BLb,255,thresFunction,&BLb);
while(true)
{
cam.read(frame); // Main camera feed
// INITIAL IMAGE OPERATION
GaussianBlur(frame,frame,Size(11,11),2); // Smoothing
inRange(frame,Scalar(CV_RGB(RLr,GLr,BLr)),Scalar(CV_RGB(RHr,GHr,BHr)),frameR);
// Red marker separated
inRange(frame,Scalar(CV_RGB(RLb,GLb,BLb)),Scalar(CV_RGB(RHb,GHb,BHb)),frameB);
// Blue marker separated
dilate(frameR,frameR,getStructuringElement(MORPH_ELLIPSE,Size(4,4)));
dilate(frameB,frameB,getStructuringElement(MORPH_ELLIPSE,Size(4,4)));
// INITIAL IMAGE OPERATION
// PARAMETER CALCULATION FOR RED MARKER
momentR = moments(frameR,true);
xmeanCurR = momentR.m10/momentR.m00;
ymeanCurR = momentR.m01/momentR.m00;
curAreaR = momentR.m00;
// PARAMETER CALCULATION FOR RED MARKER
// PARAMETER CALCULATION FOR BLUE MARKER
momentB = moments(frameB,true);
xmeanCurB = momentB.m10/momentB.m00;
ymeanCurB = momentB.m01/momentB.m00;
curAreaB = momentB.m00;
// PARAMETER CALCULATION FOR BLUE MARKER
// MAIN MOUSE EVENTS
checkmotionR(xmeanCurR,ymeanCurR,xmeanLastR,ymeanLastR);
detectClickR(curAreaR,lastAreaR);
detectClickB(curAreaB,lastAreaB);
// MAIN MOUSE EVENTS
// UPDATIONS
xmeanLastR=xmeanCurR;
ymeanLastR=ymeanCurR;
lastAreaR = curAreaR;
lastAreaB = curAreaB; // FOR RIGHT CLICK 'SIZETHRESH'
// UPDATIONS
// SHOW WINDOWS
imshow("WinR",frameR);waitKey(10);
imshow("WinB",frameB);waitKey(10);
// SHOW WINDOWS
}
return 0;
}