internal static double Evaluate(List<WordsearchImage> wordsearchImages, Classifier classifier) { DefaultLog.Info("Evaluating Wordsearch Image Rotation Correction . . ."); int numCorrect = 0; int numTotal = 0; //Test the algorithm on each Wordsearch Image foreach(WordsearchImage wordsearchImage in wordsearchImages) { //Register an interest in the Bitmap of the wordsearch Image wordsearchImage.RegisterInterestInBitmap(); //Test the system on each posisble rotation of the wordsearch image int[] angles = new int[] { 0, 90, 180, 270 }; Parallel.ForEach(angles, angle => { WordsearchRotation rotation = new WordsearchRotation(wordsearchImage.Bitmap.DeepCopy(), (int)wordsearchImage.Rows, (int)wordsearchImage.Cols); rotation.Rotate(angle); //Rotate the image for the wordsearch back to the correct orientation so that we know the correct answer WordsearchRotation correctRotation = rotation.DeepCopy(); correctRotation.Rotate((360 - angle) % 360); //Angles must be EXACTLY the same as the ones used in the correction in order to yield the same result (i.e. 0, 90, 180, 270) WordsearchRotation proposedRotation = WordsearchRotationCorrection.CorrectOrientation(rotation, classifier); //Keep track of the number correct & total number if (proposedRotation.Bitmap.DataEquals(correctRotation.Bitmap)) { numCorrect++; } numTotal++; //Clean up rotation.Bitmap.Dispose(); correctRotation.Bitmap.Dispose(); proposedRotation.Bitmap.Dispose(); }); //Clean up wordsearchImage.DeregisterInterestInBitmap(); } DefaultLog.Info("Returned {0}/{1} Wordsearch Rotations Correctly", numCorrect, numTotal); DefaultLog.Info("Wordsearch Image Rotation Evaluation Completed"); return (double)numCorrect / numTotal; }
public static WordsearchRotation CorrectOrientation(WordsearchRotation wordsearchRotation, Classifier classifier) //Classifier MUST be probablistic { //Rotate the Bitmap through the 4 possible rotations WordsearchRotation[] rotations = new WordsearchRotation[4]; for(int i = 0; i < rotations.Length; i++) { int angle = i * 90; rotations[i] = wordsearchRotation.DeepCopy(); rotations[i].Rotate(angle); } //Calculate how likely wach rotation is to be correct double bestScore = double.MinValue; int bestIdx = -1; for(int i = 0; i < rotations.Length; i++) { double score = scoreBitmap(rotations[i], classifier); if(score > bestScore) { bestScore = score; bestIdx = i; } } //Dispose of the Wordsearch Rotations that weren't the best for(int i = 0; i < rotations.Length; i++) { if(i != bestIdx) { rotations[i].Bitmap.Dispose(); } } return rotations[bestIdx]; }