private Candidate findCandidate(Image test) { double bestMSE = double.MaxValue; CharPattern bestCandidate = patterns[0]; foreach(CharPattern train in patterns) { double mse = calcMSE(train, test); if(mse < bestMSE) { bestMSE = mse; bestCandidate = train; } if(mse == 0) { break; } } return new Candidate(bestMSE, bestCandidate); }
public static List <CharPattern> readCharsFromResources(string section) { // result List <CharPattern> patterns = new List <CharPattern>(); // get a reference to the current assembly Assembly ass = Assembly.GetExecutingAssembly(); // read from assembly Stream stream = getAssemblyStream(ass, "mapping.config"); // config XmlDocument doc = new XmlDocument(); doc.Load(stream); // parse XmlNode parent = doc.GetElementsByTagName(section).Item(0); foreach (XmlNode node in parent.ChildNodes) { // attributes XmlElement charElement = (XmlElement)node; char name = charElement.Attributes["key"].Value[0]; string file = charElement.Attributes["image"].Value; // read from assembly Stream imgStream = getAssemblyStream(ass, file); Bitmap bitmap = Bitmap.FromStream(imgStream) as Bitmap; Image image = toImage(bitmap); imgStream.Close(); // new pattern CharPattern pattern = new CharPattern(name, image); patterns.Add(pattern); } return(patterns); }
private Candidate findSubCandidate(Image test, int xOffset) { double bestMSE = double.MaxValue; CharPattern bestCandidate = patterns[0]; foreach(CharPattern train in patterns) { if(train.width > 2 && train.height <= test.height && (train.width + xOffset) <= test.width) { int distance = test.height - train.height; for(int y = 0; y <= distance; y++) { int xStart = xOffset; int xEnd = xStart + train.width; int yStart = y; int yEnd = yStart + train.height; Image subTest = test.crop(xStart, xEnd, yStart, yEnd); double mse = calcMSE(train, subTest); if(mse < bestMSE) { bestMSE = mse; bestCandidate = train; } if(bestMSE == 0) { return new Candidate(bestMSE, bestCandidate); } } } } return new Candidate(bestMSE, bestCandidate); }
public Candidate(double mse, CharPattern pattern) { this.mse = mse; this.pattern = pattern; }