/* * Searches for a small image, in a big image and returns the matches * --- * Screen = image to search in * Sample = image to search for * Offset = where to START and END on the screen we're searching in * minMatch = minimum match in percent to even consider a beginning match (from 0.01 to 1) usually * i use 0.70. */ public static PixelMatchList SearchForSamples(Color[,] screen, Color[,] sample, Square offset, float minMatch) { PixelMatchList matches = new PixelMatchList(); // search the screen for the sample int screen_rows = screen.GetLength(0); int screen_colums = screen.GetLength(1); // count sample rows int sample_rows = sample.GetLength(0); int sample_colums = sample.GetLength(1); // Det er her på Screenet vi starter vores søgning int i = offset.startx; int j = offset.starty; int k = 0; int l = 0; while (i < offset.endx) { while (j < offset.endy) { // Vi har en start match, så herfra leder vi videre uden løkken kører videre //if (screen[i, j].Equals(sample[k, l])) if (Vemod.Image.rgbPercentCompare(screen[i, j], sample[k, l]) > minMatch) { // Pixelmatches: int pixelmatches = 0; float pixelmatchpercent = 0; float pixelmatchpercenttotal = 0; // Initial match! int start_match_x = i; int start_match_y = j; // subsearch // Vi vil ikke påvirke det omkringliggende loop int _i = i; int _j = j; // Vi itererer over vores sample while (k < sample_rows) { while (l < sample_colums) { // Tjek for om _i og _j er inden for vores "screen" if (_i < offset.endx && _j < offset.endy) { float match = Vemod.Image.rgbPercentCompare(screen[_i, _j], sample[k, l]); if (pixelmatchpercent == 0) { pixelmatchpercent = match; pixelmatchpercenttotal += match; pixelmatches++; } else { pixelmatchpercent = ((pixelmatchpercent * pixelmatches) + match) / (pixelmatches + 1); pixelmatchpercenttotal += match; pixelmatches++; } if (k == sample.GetLength(0) - 1 && l == sample.GetLength(1) - 1) { // Found the image! if (pixelmatchpercent > 0.70) { matches.Add(new PixelMatch(start_match_x, start_match_y, _i, _j, pixelmatchpercent)); } k = sample_rows + 1; // Grimt grimt hack, men vi sparer en goto pixelmatchpercent = 0; pixelmatchpercenttotal = 0; pixelmatches = 0; break; } else if (l == sample.GetLength(1) - 1) { // Hvis vi er for "enden" så reset, og hop til næste row k++; l = 0; // Row jump på screenen også _i = _i + 1; _j = start_match_y; } else { // Hop en enkelt column både på screen og på sample l++; _j++; } } else { k = sample_rows + 1; // Grimt grimt hack, men vi sparer en goto pixelmatchpercent = 0; pixelmatchpercenttotal = 0; pixelmatches = 0; break; } } } // resets the sample search k = 0; l = 0; } else { // Ingen match, så kan dette ikke være vores sample, led videre! k = 0; l = 0; } j++; } j = offset.starty; i++; } return matches; }
/* * Cuts off a image inside a bigger picture * --- * Screen = image to search in * Offset = where to START and END on the screen we're searching in */ public static Color[,] subImage(Color[,] screen, Square offset) { // search the screen for the sample int screen_rows = screen.GetLength(0); int screen_colums = screen.GetLength(1); Color[,] subImage = new Color[offset.endx - offset.startx, offset.endy - offset.starty]; // Det er her på vores image vi starter vores søgning int i = offset.startx; // x int j = offset.starty; // y // new image int ni = 0; int nj = 0; while (j < offset.endy) { while (i < offset.endx) { // Vi itererer over vores sample subImage[ni, nj] = screen[i, j]; Console.WriteLine("(" + ni + ";" + nj + " -> (" + i + ";" + j + ")"); ni++; i++; } ni = 0; i = offset.startx; nj++; j++; } return subImage; }
public static PixelMatchList SearchForSamples(Color[,] screen, Color[,] sample, Square offset) { return Vemod.Screen.SearchForSamples(screen, sample, offset, (float)0.70); }
public static Color[,] shrinkImage(Color[,] image) { // search the screen for the sample int image_rows = image.GetLength(0); int image_colums = image.GetLength(1); int rowsToRemoveLeft = 0; int k = 0; int l = 0; // Vi itererer over vores image while (k < image_rows) { bool canRemoveColumn = true; while (l < image_colums) { Color c = image[k, l]; if ((int)c.A == 0 && (int)c.R == 0 && (int)c.G == 0 && (int)c.B == 0) { } else { canRemoveColumn = false; break; } l++; } if (canRemoveColumn) { rowsToRemoveLeft++; } else { break; } l = 0; k++; } int rowsToRemoveRight = 0; k = image_rows - 1; l = image_colums - 1; // Vi itererer over vores image while (k > 0) { bool canRemoveColumn = true; while (l > 0) { Color c = image[k, l]; if ((int)c.A == 0 && (int)c.R == 0 && (int)c.G == 0 && (int)c.B == 0) { } else { canRemoveColumn = false; break; } l--; } if (canRemoveColumn) { rowsToRemoveRight++; } else { break; } l = image_colums - 1; k--; } int columsToRemoveTop = 0; k = 0; l = 0; // Vi itererer over vores image while (l < image_colums) { bool canRemoveRow = true; while (k < image_rows) { Color c = image[k, l]; if ((int)c.A == 0 && (int)c.R == 0 && (int)c.G == 0 && (int)c.B == 0) { } else { canRemoveRow = false; break; } k++; } if (canRemoveRow) { columsToRemoveTop++; } else { break; } k = 0; l++; } int columsToRemoveBottom = 0; k = image_rows - 1; l = image_colums - 1; // Vi itererer over vores image while (l > 0) { bool canRemoveRow = true; while (k > 0) { Color c = image[k, l]; if ((int)c.A == 0 && (int)c.R == 0 && (int)c.G == 0 && (int)c.B == 0) { } else { canRemoveRow = false; break; } k--; } if (canRemoveRow) { columsToRemoveBottom++; } else { break; } k = image_rows - 1; l--; } Square s = new Square(rowsToRemoveLeft, columsToRemoveTop, (image_rows - rowsToRemoveRight), (image_colums - columsToRemoveBottom)); Color[,] returnImage = Image.subImage(image, s); Console.WriteLine("({0};{1}) - ({2};{3})", columsToRemoveTop, rowsToRemoveLeft, (image_rows - columsToRemoveBottom), (image_colums - rowsToRemoveRight)); Console.WriteLine("Rows top: " + columsToRemoveTop); Console.WriteLine("Rows bottom: " + columsToRemoveBottom); Console.WriteLine("Colum left: " + rowsToRemoveLeft); Console.WriteLine("Colum right: " + rowsToRemoveRight); return returnImage; }