Skip to content

Commit

Permalink
Trying to use mouse for input
Browse files Browse the repository at this point in the history
Just spent 10 straight hours on this and accomplished nothing

just trying to get it to work first, then will work on diff
calculations.
  • Loading branch information
zappybiby committed Feb 18, 2017
1 parent 61f1845 commit 56d1a44
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 81 deletions.
Binary file modified ets2_auto_driving.sdf
Binary file not shown.
Binary file modified ets2_auto_driving.v12.suo
Binary file not shown.
138 changes: 57 additions & 81 deletions ets2_auto_driving/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,111 +22,107 @@ using namespace std;

void Thinning(Mat input, int row, int col);


int main() {

// cudaf();
//cudaf();


long long int sum = 0;
long long int i = 0;

while (true) {
// Used to calculate FPS / Performance
// auto begin = chrono::high_resolution_clock::now();

// This finds the game window and sets the source for hWnd
auto begin = chrono::high_resolution_clock::now();
// Grab the game window
HWND hWnd = FindWindow("prism3d", NULL);

// Creates Mats "image" and "outputImg"
Mat image, outputImg;
hwnd2mat(hWnd).copyTo(image);

// This stuff would use GPUMat
// Mat to GpuMat
/*
cuda::GpuMat imageGPU;
imageGPU.upload(image);
medianBlur(image, image, 3);
bilateralFilter(imageGPU, imageGPU, 15, 80, 80);
cv::cuda::bilateralFilter(imageGPU, imageGPU, );
*/

// Apply Median Blur
medianBlur(image, image, 3);

int width = 0, height = 0;

RECT windowsize;
GetClientRect(hWnd, &windowsize);

// Change this to whatever size you want to resize to
// change this to whatever size you want to resize to
height = windowsize.bottom;
width = windowsize.right;

// Obtain the 4-points of the input image
// The 4-points at the input image
vector<Point2f> origPoints;

origPoints.push_back(Point2f(0, (height - 50)));
origPoints.push_back(Point2f(width, (height - 50)));
origPoints.push_back(Point2f((width / 2 + 125), (height / 2 + 30)));
origPoints.push_back(Point2f((width / 2 - 125), (height / 2 + 30)));
origPoints.push_back(Point2f(width, height - 50));
origPoints.push_back(Point2f(width / 2 + 125, height / 2 + 30));
origPoints.push_back(Point2f(width / 2 - 125, height / 2 + 30));

// The correspsonding 4-points in the destination image
vector<Point2f> dstPoints;

// The 4-points correspondences in the destination image
vector<Point2f> dstPoints;
dstPoints.push_back(Point2f(0, height));
dstPoints.push_back(Point2f(width, height));
dstPoints.push_back(Point2f(width, 0));
dstPoints.push_back(Point2f(0, 0));

// IPM (Inverse Perspective Mapping) object
// create an IPM object that will perform the Inverse Perspective Mapping.
IPM ipm(Size(width, height), Size(width, height), origPoints, dstPoints);

// Uses the 4-points to apply Homography
// Process
// clock_t begin = clock();
ipm.applyHomography(image, outputImg);

// clock_t end = clock();
// double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
// printf("%.2f (ms)\r", 1000 * elapsed_secs);
// ipm.drawPoints(origPoints, image);

// More GPUMat stuff
/*
imageGPU.download(image);
Mat row = outputImg.row[0];
*/
// Mat row = outputImg.row[0];

// Creating Mats "gray", "blur", "sobel", and "contours"
cv::Mat gray;
cv::Mat blur;
cv::Mat sobel;
cv::Mat contours;

// Resizing the outputImg and making it gray (for performance reasons)
// Resize the `outputImg` to 320x240 for the smaller window.
cv::resize(outputImg, outputImg, cv::Size(320, 240));

// Convert `outputImg` to grayscale, storing the result in `gray`
cv::cvtColor(outputImg, gray, COLOR_RGB2GRAY);

// Applying blur, sobel, and threshold filters to image
// Reduce Noise
cv::blur(gray, blur, cv::Size(10, 10));

// Get Outlines
cv::Sobel(blur, sobel, blur.depth(), 1, 0, 3, 0.5, 127);

// Filter out artifacts
cv::threshold(sobel, contours, 145, 255, CV_THRESH_BINARY);

// Other effects we are not currently using
/*
Thinning(contours, contours.rows, contours.cols);
cv::Canny(gray, contours, 125, 350);
*/
// Thinning(contours, contours.rows, contours.cols);
// cv::Canny(gray, contours, 125, 350);

LineFinder ld;

// LineFinder parameters
ld.setLineLengthAndGap(20, 120);
ld.setMinVote(55);

// Detect Lines
// Uses Proabablistic Hough Transform to return a vector of lines
std::vector<cv::Vec4i> li = ld.findLines(contours);

// Draw the detected lines
ld.drawDetectedLines(contours);

// Display what the computer sees
// cv::cvtColor(contours, contours, COLOR_GRAY2RGB);
imshow("Test", contours);
waitKey(1);
// cv::cvtColor(contours, contours, COLOR_GRAY2RGB);

// WORK IN PROGRESS FOR INPUT IMPLEMENTATION //
/*
unsigned char row_center = gray.at<uchar>(10, 160);

unsigned char row_left = 0;
Expand All @@ -136,8 +132,10 @@ int main() {
int right = 0;
int i = 0;
int row_number = 5;
while (i < 150) {
if (i == 149) {
while (i < 150)
{
if (i == 149)
{
i = 0;
row_left = 0;
row_right = 0;
Expand All @@ -151,68 +149,46 @@ int main() {
break;
}
if (row_left != 255) {
row_left = gray.at<uchar>(row_number, 159 + left);
row_left = gray.at<uchar>(row_number, 159 + left);
left--;

}
if (row_right != 255) {
row_right = gray.at<uchar>(row_number, 159 + right);
row_right = gray.at<uchar>(row_number, 159 + right);
right++;

}
i++;

}
SetActiveWindow(hWnd);

SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
SetActiveWindow(hWnd);
POINT pt;
GetCursorPos(&pt);
int average = (left == -150 || right == 150) ? 0 : left + right;
if (left + right < -50)
{
cout << "go left ";
INPUT input[2];
input[0].type = INPUT_KEYBOARD;
// Translating 'A' to Scan Code, then pressing down
input[0].ki.wScan = MapVirtualKey(0x41, MAPVK_VK_TO_VSC);
input[0].ki.dwFlags = KEYEVENTF_SCANCODE;
// Translating 'A' to Scan Code, then releasing key
input[1].ki.wScan = MapVirtualKey(0x41, MAPVK_VK_TO_VSC);
input[1].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
GetCursorPos(&pt);
SetCursorPos(pt.x - 1, pt.y);
cout << "current mouse pos: " << "x: " << pt.x << "y: " << pt.y << endl;
}
else if (left + right > -50 && left + right < 50){
cout << "go straight ";
for (int x = 0, y = 0; x < 700 && y < 700; x += 10, y += 10)
{
/*
INPUT input; // Using SendInput to send input commands
input.type = INPUT_KEYBOARD;
// Translating 'A' to Scan Code, then releasing key
input[1].ki.wScan = MapVirtualKey(0x41, MAPVK_VK_TO_VSC);
input[1].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
// Translating 'D' to Scan Code, then releasing key
input[1].ki.wScan = MapVirtualKey(0x44, MAPVK_VK_TO_VSC);
input[1].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
{
//SetCursorPos((windowsize.right - windowsize.left) / 2, (windowsize.bottom - windowsize.top) / 2);
SetCursorPos(512, 384);
}
}
/* else{
else{
cout << "go right ";
{
INPUT input[2];
input[0].type = INPUT_KEYBOARD;
// Translating 'D' to Scan Code, then pressing down
input[0].ki.wScan = MapVirtualKey(0x44, MAPVK_VK_TO_VSC);
input[0].ki.dwFlags = KEYEVENTF_SCANCODE;
// Translating 'D' to Scan Code, then releasing key
input[1].ki.wScan = MapVirtualKey(0x44, MAPVK_VK_TO_VSC);
input[1].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
}
GetCursorPos(&pt);
SetCursorPos(pt.x + 1, pt.y);
cout << "current mouse pos: " << "x: " << pt.x << "y: " << pt.y << endl;
}
cout << "left: " << left << ", right: " << right << ", average: " << average << endl;
*/
cout << "left: " << left << ", right: " << right << ", average: " << average << endl;
}
return 0;
}
Binary file modified ets2_auto_driving/vc120.pdb
Binary file not shown.

0 comments on commit 56d1a44

Please sign in to comment.