Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding > Coding Tutorials
You last visited: Today at 05:06

  • Please register to post and access all features, it's quick, easy and FREE!


Using GetPixel to identify objects inside a game window

Discussion on Using GetPixel to identify objects inside a game window within the Coding Tutorials forum part of the General Coding category.

Reply
 
Old   #1
 
elite*gold: 650
Join Date: Sep 2013
Posts: 223
Received Thanks: 11
Arrow Using GetPixel to identify objects inside a game window

Hello friends,

This Tutorial is meant to be beginner friendly, provide information while not being dry as a bone

You are probably familiar with the concept of reading pixel color at a position on the screen. This tutorial goes one step further and looks for an "object" of unknown location which can not be identified by a single pixel color.

I also uploaded a video yesterday which includes this example:

feedback would be very much appreciated (auch gerne auf Deutsch)

Lets get the basics out of the way:
The following funciton provides pixel color at position x and y.
Code:
COLORREF GetPixel(HDC hdc,int x,int y);
As a third parameter a handle to the device context is needed (hdc).
Since we want to look for something inside a game window we first get a handle to the window and then the desired device context like so:
Code:
HWND hWND = FindWindow(NULL, L"Zuma Deluxe 1.1.0.0");
HDC hDC = GetDC(hWND); //handle to the device context
//Do stuff with hDC
ReleaseDC(hWND,hDC); //make sure to release after use!
Coordinates on the Screen are given like so: Top left is the origin. Going right increases x and going down increases y. Our origin is top left of the game window. We still need the left and bottom border. These we can get with:
Code:
RECT rect = {};
GetClientRect(hWND, &rect);
int right_border = rect.right;
int bottom_border = rect.bottom;
Those are all the basics required.

Now to a practical example:
As my target I picked the game Zuma. What I wanna know is the location of the frog. Starring my enemy right into the eye I realized that those will probably be decent pixels to search for. Turns out that only within those dead eyes there are 2 pixels next to each other having a specific color code:



After having found the first eye we already kinda know the frogs position but since the frog can turn 360° I wanted to get a good estimate of the center of the frog. So the idea is to look for the second eye as well then add those two positions up and divide by two to get the center position between those two eyes. The search area for the second eye is restricted:
Since we started top left going down and then right we only need to search to the right of the first eye as well as not too close and not too far away:



Equipped with the basics as well as plan for action all that is left to do is write it all down:
Code:
HWND hWND = FindWindow(NULL, L"Zuma Deluxe 1.1.0.0");
std::cout << "looking for frog start" << std::endl;
HDC hDC = GetDC(hWND);
COLORREF color;
COLORREF characteristic_color = 15398653; // Color at the Shiny Spot of the eye
int first_eye_x = 0;
int first_eye_y = 0;
RECT rect = {};
GetClientRect(hWND, &rect); // Stores info about the game window size
for (int i = 0; i < rect.right; i++) { // loop through x coordinates starting at 0
   for (int j = 0; j < rect.bottom; j++) { // loop through y coordinates starting at 0
      color = GetPixel(hDC, i, j);
      if (color == characteristic_color) { // ist this an eye?!
         color = GetPixel(hDC, i, j + 1);
         if (color == characteristic_color) { // same color in the next pixel!! we got an eye
            first_eye_x = i;
            first_eye_y = j;
            std::cout << "first eye: x: " << first_eye_x << "   y:" << first_eye_y << std::endl;
            for (int k = first_eye_x - 35; k < first_eye_x + 35; k++) { // search around first eye for second eye
               for (int l = first_eye_y - 35; l < first_eye_y + 35; l++) {
                  color = GetPixel(hDC, k, l);
                  if (abs(k - first_eye_x) > 10 && abs(l - first_eye_y) > 10 && color == characteristic_color) { // found the second eye!!
                     std::cout << "second eye: x: " << k << "   y:" << l << std::endl;
                     std::cout << "frog center at: x: " << (abs(first_eye_x + k) / 2) << " | y:" << (abs(first_eye_y + l) / 2) << std::endl;
                     return 0;
                  }
               }
            }
         }
      }
   }
}
ReleaseDC(hWND, hDC);
Warning:
This does work BUT I want to give a bit of a warning: GetPixel is a very slow function. So if you need information fast and/or your search area is big this method is probably not a good choice!



Black_and_White is offline  
Old 04/19/2019, 20:06   #2
 
elite*gold: 0
Join Date: Nov 2018
Posts: 227
Received Thanks: 396
A method like this where you compare single pixel values seems to be really error-prone compared to more widespread approaches such as .


sk8land​ is offline  
Thanks
1 User
Old 04/19/2019, 20:26   #3
 
elite*gold: 650
Join Date: Sep 2013
Posts: 223
Received Thanks: 11
Quote:
Originally Posted by sk8land​ View Post
A method like this where you compare single pixel values seems to be really error-prone compared to more widespread approaches such as .
Thanks a lot for the hint. I just had a brief look into at and I must agree It would surely be a safer bet.
Seems like a very good approach for my goal (bot for Zuma) not sure about the required runtime yet. I was planing on looking at loading a "snapshot" of the window into a bitmap next since as far as I know searching through bitmaps in memory is very fast
Black_and_White is offline  
Old 04/19/2019, 20:55   #4
 
elite*gold: 0
Join Date: Nov 2018
Posts: 227
Received Thanks: 396
Quote:
Originally Posted by Black_and_White View Post
Thanks a lot for the hint. I just had a brief look into at and I must agree It would surely be a safer bet.
Seems like a very good approach for my goal (bot for Zuma) not sure about the required runtime yet. I was planing on looking at loading a "snapshot" of the window into a bitmap next since as far as I know searching through bitmaps in memory is very fast
Yes, loading everything into memory first would probably save a lot of time. The advantage of template matching would be that it's more robust. If the target looks a little different it will still be detected; precise pixel values are not that important.

The downside would be that if the target can rotate you would need to match several rotated versions of the same template. That should not be a big problem though since template matching is quite fast.
sk8land​ is offline  
Reply



« [Android][Xamarin] Tutorial-Reihe | - »

Similar Threads
how can i open nfs world in 3 window ( using 3 accounts ) using sandboxie ?
02/03/2014 - Need for Speed World - 15 Replies
i can run the game using 2 accounts but i can't run 3rd account it said it is impossible to run the launcher while the game is running .. i need to know how can i open 3 accounts in the same time .. any help ?
GetPixel mit Assembler
01/17/2013 - General Coding - 5 Replies
Hallo Leute, ich will versuchen einfache Api´s via Assembler anzusprechen. mit ner msgbox sowie beep war ich schon erfolgreich, nun möchte ich gerne die Bildschirmfarbe mit der GetPixel function herraus bekommen. Alllerdings benötige ich dazu ... COLORREF GetPixel( _In_ HDC hdc, _In_ int nXPos, _In_ int nYPos );
[C++] GetPixel gibt falsche Werte zurück
10/15/2009 - C/C++ - 26 Replies
Hi Leute ^^ Ich hab heute mal versucht für Combat Arms einen kleinen "Aimbot" (eig kann mans so nicht nennen) zu schreiben, der jedes mal wenn jemand durch mein Fadenkreuz läuft schießt. Dafür muss ich natürlich still stehen und darf die Waffe nicht bewgen. Soweit, sogut. Jetzt kommen wir zu meinem Problem: Sobald ich jedoch ingame gehe (also sobald die 3D Engine von CA läuft (im Menü gehts noch)), gibt GetPixel immer den selben Wert zurück, welcher jedoch in keinster Weise passt. Bei...
[C++] Pixel Suchen aber wie? mit GetPixel()?
07/07/2009 - C/C++ - 16 Replies
Hi leute, suche was zum Pixel suchen! hatte gegoogle und kam bis jetzt nur auf die Funktion GetPixel() aber ich habe nichts gutes darüber gehört könnte mir jemand ne bessere besser Funktion zur Pixel suche zeigen mit beispiel bez Erklärung da ich mit den Sourcecodes teilweise nicht mitkomme den ich zu GetPixel() gefunden! Grund Kenntnisse in C++ habe ich mehr als genug plos noch nicht sowas und ich will mich langsam an sowas ran arbeiten hatte das hier gefunden plos weiß ich...
GetPixel Download?
08/14/2007 - Main - 3 Replies
So...ich hab keine Ahnung, ob das hier richtig ist (und wenn nicht, dann verscheibt es bitte), aber ich wollte mal fragen wo man GetPixel runterladen kann? Ich hab jetz ca. 30 Minuten gegoogelt, aber nix gefunden, Danke shconmal im Voraus ;)



All times are GMT +2. The time now is 05:06.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

BTC: 3KeUpz52VCbhmLwuwydqxu6U1xsgbT8YT5
ETH: 0xc6ec801B7563A4376751F33b0573308aDa611E05

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2019 elitepvpers All Rights Reserved.