-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
231 lines (182 loc) · 6.64 KB
/
Copy pathSource.cpp
File metadata and controls
231 lines (182 loc) · 6.64 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <Windows.h>
#include <algorithm>
#include <chrono>
#include <thread>
#define MIN_RADIUS 1.9
#define PI 3.14159
using namespace cv;
class Target
{
public:
double Distance;
Point Position;
double Radius;
int Index;
};
bool CompareTargets(const Target& target1, const Target& target2)
{
if (target1.Distance < target2.Distance)
return true;
else if (target1.Distance > target2.Distance)
return false;
else {
if (std::abs(target1.Index - target2.Index) <= 1) {
return target1.Index < target2.Index;
}
else {
return false;
}
}
}
void clickMouse()
{
while (true)
{
if (GetAsyncKeyState('X') & 0x8000)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
Mat CaptureWindow(HWND hWND) {
HDC deviceContext = GetDC(hWND);
HDC memoryDeviceContext = CreateCompatibleDC(deviceContext);
RECT windowRect;
GetClientRect(hWND, &windowRect);
int height = windowRect.bottom;
int width = windowRect.right;
HBITMAP bitmap = CreateCompatibleBitmap(deviceContext, width, height);
SelectObject(memoryDeviceContext, bitmap);
BitBlt(memoryDeviceContext, 0, 0, width, height, deviceContext, 0, 0, SRCCOPY);
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = -height;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 1;
bi.biYPelsPerMeter = 2;
bi.biClrUsed = 3;
bi.biClrImportant = 4;
Mat mat = Mat(height, width, CV_8UC4);
GetDIBits(memoryDeviceContext, bitmap, 0, height, mat.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
DeleteObject(bitmap);
DeleteDC(memoryDeviceContext);
ReleaseDC(hWND, deviceContext);
return mat;
}
void printMatInfo(const cv::Mat& image)
{
std::cout << "Rows: " << image.rows << std::endl;
std::cout << "Columns: " << image.cols << std::endl;
std::cout << "Channels: " << image.channels() << std::endl;
std::cout << "Data Type: " << image.type() << std::endl;
std::cout << "Pixel Format: " << image.depth() << std::endl;
std::cout << "Element Size: " << image.elemSize() << " bytes" << std::endl;
std::cout << "Total Size: " << image.total() * image.elemSize() << " bytes" << std::endl;
std::cout << "Is Continuous: " << (image.isContinuous() ? "Yes" : "No") << std::endl;
}
void MoveCursorTowardsTarget(int targetX, int targetY, const cv::Mat& image, int moveAmount)
{
int screenWidth = image.cols;
int screenHeight = image.rows;
int centerX = screenWidth / 2;
int centerY = (screenHeight + 23) / 2;
int deltaX = targetX - centerX;
int deltaY = targetY - centerY;
double distance = sqrt(deltaX * deltaX + deltaY * deltaY);
double directionX = deltaX / distance;
double directionY = deltaY / distance;
int moveX = static_cast<int>(directionX * moveAmount);
int moveY = static_cast<int>(directionY * moveAmount);
mouse_event(MOUSEEVENTF_MOVE, moveX, moveY, 0, 0);
}
int main()
{
setUseOptimized(true);
std::string window_title = "aimlab_tb";
HWND hWND = FindWindow(NULL, window_title.c_str());
HANDLE cHandle = GetStdHandle(STD_OUTPUT_HANDLE);
hWND = GetDesktopWindow();
while (!hWND) {
SetConsoleCursorPosition(cHandle, {0, 0});
std::cout << "Waiting for " + window_title + "... \n ";
hWND = FindWindow(NULL, window_title.c_str());
Sleep(100);
}
namedWindow("Window", WINDOW_KEEPRATIO);
std::thread t(clickMouse);
std::vector<Target> targets;
while (true)
{
auto start = std::chrono::high_resolution_clock::now();
Mat image = CaptureWindow(hWND);
Mat red_mask, mat;
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
inRange(image, cv::Scalar(0, 0, 150), cv::Scalar(40, 40, 255), red_mask);
rectangle(red_mask, cv::Rect(static_cast<int>(899), 0, static_cast<int>(104), static_cast<int>(90)), cv::Scalar(0), -1);
std::vector<std::vector<cv::Point>> contours = {};
findContours(red_mask, contours, mat, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++)
{
double contour_area = contourArea(contours[i]);
double contour_radius = sqrt(contour_area / PI);
if (contour_radius < MIN_RADIUS) {
continue;
}
drawContours(image, contours, i, Scalar(0, 255, 0), 2);
std::vector<cv::Point> contour = contours[i];
Moments mo = moments(contour);
int cX = static_cast<int>(mo.m10 / mo.m00);
int cY = static_cast<int>(mo.m01 / mo.m00);
Point center = Point(image.cols / 2, (image.rows + 23) / 2);
Point circle_pos = Point(cX, cY);
int distance = static_cast<int>(std::round(std::sqrt(std::pow(circle_pos.x - center.x, 2) + std::pow(circle_pos.y - center.y, 2))));
Scalar line_color(255, 255, 255);
Scalar circle_color(255, 255, 255);
circle(image, circle_pos, contour_radius / 3 , circle_color, -1, LINE_AA);
line(image, center, circle_pos, line_color, 1, LINE_AA);
putText(image, "Radius: " + std::to_string(contour_radius).substr(0, 4), Point(cX - 20, cY - 10 - contour_radius), FONT_HERSHEY_DUPLEX, 0.5, Scalar(255, 255, 255), 1, LINE_AA);
putText(image, "Distance: " + std::to_string(distance).substr(0, 5), Point(cX - 20, cY - 30 - contour_radius), FONT_HERSHEY_DUPLEX, 0.5, Scalar(255, 255, 255), 1, LINE_AA);
putText(image, "Target: " + std::to_string(i + 1), Point(cX - 20, cY - 50 - contour_radius), FONT_HERSHEY_DUPLEX, 0.5, Scalar(255, 255, 255), 1, LINE_AA);
Target target{distance, circle_pos, contour_radius, i};
targets.push_back(target);
}
if (targets.size() > 0) {
Target closest = *std::min_element(targets.begin(), targets.end(), CompareTargets);
line(image, Point(image.cols / 2, (image.rows + 23) / 2) , closest.Position, Scalar(0, 255, 0), 1.1, LINE_AA);
circle(image, closest.Position, closest.Radius / 3, Scalar(0, 255, 0), -1, LINE_AA);
if (closest.Distance < 1)
{/*
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(1);
mouse_event(MOUSEEVENTF_LEFTUP , 0, 0, 0, 0);*/
}
else
{
MoveCursorTowardsTarget(closest.Position.x, closest.Position.y, image, (int)(3 * closest.Distance));
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
putText(image, "FPS: " + std::to_string(1000 / duration.count()).substr(0, 5), Point(10, 20), FONT_HERSHEY_DUPLEX, 0.5, Scalar(255, 255, 255), 1, LINE_AA);
cv::imshow("Window", image);
targets.clear();
if (waitKey(1) == 27) {
destroyAllWindows();
break;
};
}
return 0;
}