private void TeachThread(object parameter)
        {
            TeachThreadParameter input = parameter as TeachThreadParameter;
            if (input == null)
            {
                throw new ArgumentNullException("Неправильный параметр потока.");
            }

            var delta = 1.0 / filesToTeach.Count;
            for (int i = input.From; (i < input.To) && (!isTeachingAborted); ++i)
            {
                try
                {
                    var image = new SimpleImage(imagesToTeach[i]);
                    var imageData = image.GetGrayData();
                    var lbp = new LBPCreator(imageData, true, true, false);
                    var glcm = new GLCMCreator(imageData, true, true, false);
                    AddFeatures(glcm.Feature, lbp.Feature, filesToTeach[i]);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    continue;
                }
                finally
                {
                    lock ((knownFiles as ICollection).SyncRoot)
                    {
                        teachProgress += delta;
                    }
                }
            }
            input.CommonBarrier.SignalAndWait();
        }
 public SplitImageThreadParameter(SimpleImage image, int from, int to, List<Rectangle> regions,
     List<TextureSample> samples, ManualResetEvent resetEvent)
 {
     Image = image;
     From = from;
     To = to;
     ResultRegions = regions;
     ResultSamples = samples;
     ResetEvent = resetEvent;
 }
 private void SplitImage(SimpleImage image, List<TextureSample> samples, List<Rectangle> regions)
 {
     int YCount = image.Height / RecognitionParameters.RecognitionFragmentSize;
     int threadCount = Math.Min(YCount, Environment.ProcessorCount * 2);
     int threadPart = YCount / threadCount;
     var resetEvents = new ManualResetEvent[threadCount + 1];
     var threadParameters = new SplitImageThreadParameter[threadCount + 1];
     for (int i = 0; i < threadCount; ++i)
     {
         resetEvents[i] = new ManualResetEvent(false);
         threadParameters[i] = new SplitImageThreadParameter(
             image, threadPart * i, ((i + 1) < threadCount) ? (threadPart * (i + 1)) : (YCount),
             regions, samples, resetEvents[i]);
         ThreadPool.QueueUserWorkItem(new WaitCallback(SplitImageThread), threadParameters[i]);
     }
     resetEvents[threadCount] = new ManualResetEvent(false);
     threadParameters[threadCount] = new SplitImageThreadParameter(
         image, 0, 0, regions, samples, resetEvents[threadCount]);
     ThreadPool.QueueUserWorkItem(new WaitCallback(SplitImageAdditionalThread), threadParameters[threadCount]);
     WaitHandle.WaitAll(resetEvents);
 }
        public List<RecognitionResult> RecognizeImages(List<Bitmap> images)
        {
            recognitionProgress = 0;
            isRecognizing = true;

            var answers = new List<RecognitionResult>();
            for (int i = 0; i < images.Count; ++i)
            {
                var imageToRecognize = new SimpleImage(images[i]);
                var imageData = imageToRecognize.GetGrayData(0, 0, imageToRecognize.Width, imageToRecognize.Height);
                var sample = new TextureSample(imageData, false);
                answers.Add(GetBestMatch(sample, new Rectangle(0, 0, imageData.Width, imageData.Height)));
                recognitionProgress += 1.0 * i / images.Count;
            }

            recognitionProgress = 1;
            isRecognizing = false;
            return answers;
        }
        public RecognitionResult RecognizeImage(Bitmap image, Rectangle region)
        {
            recognitionProgress = 0;
            isRecognizing = true;

            var imageToRecognize = new SimpleImage(image);
            recognitionProgress = 0.2;

            var imageData = imageToRecognize.GetGrayData(region.X, region.Y,
                region.X + region.Width, region.Y + region.Height);
            var sample = new TextureSample(imageData, false);
            recognitionProgress = 0.8;

            var answer = GetBestMatch(sample, region);

            recognitionProgress = 1;
            isRecognizing = false;
            return answer;
        }
        public List<RecognitionResult> RecognizeImage(Bitmap image)
        {
            recognitionProgress = 0;
            isRecognizing = true;

            var imageToRecognize = new SimpleImage(image);
            recognitionProgress = 0.05;

            var regions = new List<Rectangle>();
            var samples = new List<TextureSample>();
            SplitImage(imageToRecognize, samples, regions);

            var matches = new List<RecognitionResult>();
            for (int i = 0; i < samples.Count; ++i)
            {
                var answer = GetBestMatch(samples[i], regions[i]);
                matches.Add(answer);
                recognitionProgress = 0.95 + (0.05 * i) / samples.Count;
            }

            recognitionProgress = 1;
            isRecognizing = false;
            return matches;
        }