//Return health value percentage public static int Mana() { //Get matching x pixels of the health bar int value = ImageRecognition.MatchingXPixels("Game/mana.png", 15); //Get total pixels of health bar int total = PixelCache.GetWidth("Game/mana.png"); //Get percentage of 100 return((int)Math.Round((double)(100 * value) / total)); }
//Match pixels of an image on an X plane private static int MatchImageX(string filename, int resolution = 3) { //Convert images to pixel arrays int[] pixels = PixelCache.GetPixels(PixelCache.SCREENSHOT_IMAGE_NAME); int[] search = PixelCache.GetPixels(filename); //Loop through each pixel in screenshot for (int key = 0; key < pixels.Length; ++key) { //If this pixel matches the first pixel in our image if (pixels[key] == search[0]) { //Create a matched variable bool matched = true; //Foreach pixel in our resolution for (int i = 1; i < resolution; ++i) { //If the pixel does not match, unset the matched variable if (pixels[key + i] != search[i]) { matched = false; } } //If all pixels in resolution matched if (matched) { //Create the value var int value = 0; //Loop through each pixel in the first row of our loaded image for (int i = 0; i < PixelCache.GetWidth(filename); ++i) { //If this pixel matches, increment our value if (pixels[key + i] == search[i]) { value++; } } //Return the value return(value); } } } //No pixels matched return(0); }
public static void ReadText() { Console.WriteLine("Image Preprocessing..."); List <string> WLines = new List <string>(); List <Rectangle> WRects = new List <Rectangle>(); List <string> PLines = new List <string>(); List <Rectangle> PRects = new List <Rectangle>(); //Bitmap screenshot = PixelCache.GetScreenshot(); Bitmap screenshot = ImageHelper.InvertImage( ImageHelper.ContrastImage( ImageHelper.DesaturateImage( PixelCache.GetScreenshot() ) , 25)); Console.WriteLine("Engine Processing..."); var data = Engine.Process(screenshot, PageSegMode.SparseText ); WRects = data.GetSegmentedRegions(PageIteratorLevel.Word); PRects = data.GetSegmentedRegions(PageIteratorLevel.TextLine); //Clear text coords only after we've done engine work TextCache.Clear(); PhraseCache.Clear(); Console.WriteLine("Extracting Words..."); using (var iterator = data.GetIterator()) { string line = ""; iterator.Begin(); do { do { do { do { string word = iterator.GetText(PageIteratorLevel.Word).Trim(); if (word != "") { line = line + word + " "; WLines.Add(word.ToUpper().Trim()); } } while (iterator.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word)); if (line != "") { PLines.Add(line.ToUpper().Trim()); } line = ""; } while (iterator.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine)); } while (iterator.Next(PageIteratorLevel.Block, PageIteratorLevel.Para)); } while (iterator.Next(PageIteratorLevel.Block)); } data.Dispose(); screenshot.Dispose(); Console.WriteLine("Saving results..."); for (int i = 0; i < WLines.Count; ++i) { if (!TextCache.ContainsKey(WLines[i])) { TextCache.Add( WLines[i], new Point( Convert.ToInt32((WRects[i].X + (WRects[i].Width / 2)) * 1), Convert.ToInt32((WRects[i].Y + (WRects[i].Height / 2)) * 1) ) ); } } for (int i = 0; i < PLines.Count; ++i) { if (!PhraseCache.ContainsKey(PLines[i])) { PhraseCache.Add( PLines[i], new Point( Convert.ToInt32((PRects[i].X + (PRects[i].Width / 2)) * 1), Convert.ToInt32((PRects[i].Y + (PRects[i].Height / 2)) * 1) ) ); } } Console.WriteLine("Read Complete."); Console.WriteLine("-----------------------"); }
//Find X/Y coords of an image on screen public static Point FindImagePosition(string filename, int resolution = 3) { //Convert images to pixel arrays int[] pixels = PixelCache.GetPixels(PixelCache.SCREENSHOT_IMAGE_NAME); int[] search = PixelCache.GetPixels(filename); //Set X and Y pointer for search int x = 1; int y = 1; //Loop through each pixel in screenshot for (int key = 0; key < pixels.Length; ++key) { //If this pixel matches the first pixel in our image if (pixels[key] == search[0]) { //Create a matched variable bool matched = true; //Foreach X pixel in our resolution for (int i = 1; i < resolution; ++i) { //If the pixel does not match, unset the matched variable if (pixels[key + i] != search[i]) { matched = false; } } //If the first resolution is matched, move on if (matched) { //Foreach Y pixel in our resolution for (int i = 1; i < resolution; ++i) { //If the pixel does not match, unset the matched variable if (pixels[key + (PixelCache.GetWidth(PixelCache.SCREENSHOT_IMAGE_NAME) * i)] != search[(PixelCache.GetWidth(filename) * i)]) { matched = false; } } //If we have matched the resoltion again, move on if (matched) { //The starting pointer ( end of the iamge on screen ) int start = (key) + (PixelCache.GetWidth(PixelCache.SCREENSHOT_IMAGE_NAME) * (PixelCache.GetHeight(filename) - 1)) + PixelCache.GetWidth(filename); //Increment the amount of pixels to remove for (int i = 1; i < resolution; ++i) { //If the pixel does not match, unset the matched variable if (pixels[start - i] != search[(PixelCache.GetWidth(filename) * PixelCache.GetHeight(filename)) - i]) { matched = false; } } //Did we match the last X pixels of our image? if (matched) { //Finally, we will match the four center pixels of the image, to ensure this really is what we are looking for if (pixels[key + (PixelCache.GetWidth(PixelCache.SCREENSHOT_IMAGE_NAME) * (PixelCache.GetHeight(filename) / 2)) + (PixelCache.GetWidth(filename) / 2)] == search[(PixelCache.GetWidth(filename) * (PixelCache.GetHeight(filename) / 2) + (PixelCache.GetWidth(filename) / 2))] && pixels[key + (PixelCache.GetWidth(PixelCache.SCREENSHOT_IMAGE_NAME) * ((PixelCache.GetHeight(filename) / 2) + 1)) + (PixelCache.GetWidth(filename) / 2)] == search[(PixelCache.GetWidth(filename) * ((PixelCache.GetHeight(filename) / 2) + 1) + (PixelCache.GetWidth(filename) / 2))] && pixels[key + (PixelCache.GetWidth(PixelCache.SCREENSHOT_IMAGE_NAME) * (PixelCache.GetHeight(filename) / 2)) + (PixelCache.GetWidth(filename) / 2) + 1] == search[(PixelCache.GetWidth(filename) * (PixelCache.GetHeight(filename) / 2) + (PixelCache.GetWidth(filename) / 2)) + 1] && pixels[key + (PixelCache.GetWidth(PixelCache.SCREENSHOT_IMAGE_NAME) * ((PixelCache.GetHeight(filename) / 2) + 1)) + (PixelCache.GetWidth(filename) / 2) + 1] == search[(PixelCache.GetWidth(filename) * ((PixelCache.GetHeight(filename) / 2) + 1) + (PixelCache.GetWidth(filename) / 2)) + 1]) { //Return the coordinates of this image return(new Point(x, y)); } } } } } //If we are at the edge of the screen if (x == PixelCache.GetWidth(PixelCache.SCREENSHOT_IMAGE_NAME)) { //Increment the row y++; //Reset the X position x = 0; } //Increment the X position x++; } //No image detected, return 0,0 return(new Point(0, 0)); }