Пример #1
0
        private TextureClass GetBestMatchForFeature(TextureSample sample, TextureFeatures feature)
        {
            var distancesList = GetDistancesListForFeature(sample, feature);
            var indexesList   = new int[TextureClassCount];
            var classesList   = new List <int>();
            var classesFlags  = new List <int>();

            while (true)
            {
                int bestClass = GetBestDistance(distancesList, indexesList, classesList, classesFlags);
                indexesList[bestClass] += 1;

                if (indexesList[bestClass] == knownClasses[bestClass].KnownSamplesNumber(feature))
                {
                    classesFlags.Add(bestClass);
                    if (classesFlags.Count == knownClasses.Count)
                    {
                        return(knownClasses[classesFlags[0]]);
                    }
                }
                if (indexesList[bestClass] == RecognitionParameters.NeededNeighborsNumber)
                {
                    return(knownClasses[bestClass]);
                }
            }
        }
Пример #2
0
        private RecognitionResult GetBestMatch(TextureSample sample, Rectangle region)
        {
            var answer = new RecognitionResult(region, sample);

            answer.SetAnswer(TextureFeatures.GLCM, GetBestMatchForFeature(sample, TextureFeatures.GLCM));
            answer.SetAnswer(TextureFeatures.LBP, GetBestMatchForFeature(sample, TextureFeatures.LBP));
            return(answer);
        }
Пример #3
0
        private List <double>[] GetDistancesListForFeature(TextureSample sample, TextureFeatures feature)
        {
            var distancesList = new List <double> [TextureClassCount];

            for (int i = 0; i < TextureClassCount; ++i)
            {
                distancesList[i] = knownClasses[i].GetSortedDistances(sample, feature);
            }
            return(distancesList);
        }
Пример #4
0
        internal List <double> GetSortedDistances(TextureSample sample, TextureFeatures feature)
        {
            var list  = new List <double>();
            var count = KnownSamplesNumber(feature);

            for (int i = 0; i < count; ++i)
            {
                list.Add(GetDistance(sample, i, feature));
            }
            list.Sort();
            return(list);
        }
Пример #5
0
        private void SplitImageThread(object parameter)
        {
            SplitImageThreadParameter input = parameter as SplitImageThreadParameter;

            if (input == null)
            {
                throw new ArgumentException("Неправильный формат потока");
            }

            int fragmentSize = RecognitionParameters.RecognitionFragmentSize;
            var image        = input.Image;
            var regions      = input.ResultRegions;
            var samples      = input.ResultSamples;
            int XCount       = image.Width / fragmentSize;
            int YCount       = image.Height / fragmentSize;

            for (int y = input.From; (y < input.To) && (!isRecognizingAborted); ++y)
            {
                for (int x = 0; (x < XCount) && (!isRecognizingAborted); ++x)
                {
                    int           fromX  = x * fragmentSize;
                    int           fromY  = y * fragmentSize;
                    int           toX    = fromX + fragmentSize;
                    int           toY    = fromY + fragmentSize;
                    TextureSample sample = null;
                    try
                    {
                        var imageData = image.GetGrayData(fromX, fromY, toX, toY);
                        sample = new TextureSample(imageData, false);
                    }
                    catch (Exception ex)
                    {
                        if (MessageBox.Show("Возникла ошибка. Продолжать?\n" + ex.Message, "Внимание", MessageBoxButtons.YesNo) == DialogResult.No)
                        {
                            isRecognizingAborted = true;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    lock ((regions as ICollection).SyncRoot)
                    {
                        regions.Add(new Rectangle(fromX, fromY, fragmentSize, fragmentSize));
                        samples.Add(sample);
                        recognitionProgress += 0.9 / (XCount * YCount);
                    }
                }
            }

            input.ResetEvent.Set();
        }
Пример #6
0
        private double GetDistance(TextureSample sample, int featureIndex, TextureFeatures feature)
        {
            switch (feature)
            {
            case TextureFeatures.GLCM:
                return(sample.GLCM.GetDistance(glcmFeatures[featureIndex]));

            case TextureFeatures.LBP:
                return(sample.LBP.GetDistance(lbpFeatures[featureIndex]));

            default:
                return(0);
            }
        }
Пример #7
0
        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);
        }
Пример #8
0
        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);
        }
Пример #9
0
        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;
        }
Пример #10
0
 private RecognitionResult GetBestMatch(TextureSample sample, Rectangle region)
 {
     var answer = new RecognitionResult(region, sample);
     answer.SetAnswer(TextureFeatures.GLCM, GetBestMatchForFeature(sample, TextureFeatures.GLCM));
     answer.SetAnswer(TextureFeatures.LBP, GetBestMatchForFeature(sample, TextureFeatures.LBP));
     return answer;
 }
 public RecognitionResult(Rectangle region, TextureSample sample)
 {
     this.region = region;
     this.sample = sample;
     answers     = new Dictionary <TextureFeatures, TextureClass>();
 }
 public RecognitionResult()
 {
     region  = new Rectangle(0, 0, 0, 0);
     sample  = null;
     answers = new Dictionary <TextureFeatures, TextureClass>();
 }
Пример #13
0
 public RecognitionResult(Rectangle region, TextureSample sample)
 {
     this.region = region;
     this.sample = sample;
     answers = new Dictionary<TextureFeatures, TextureClass>();
 }
Пример #14
0
 public RecognitionResult()
 {
     region = new Rectangle(0, 0, 0, 0);
     sample = null;
     answers = new Dictionary<TextureFeatures, TextureClass>();
 }
Пример #15
0
        private TextureClass GetBestMatchForFeature(TextureSample sample, TextureFeatures feature)
        {
            var distancesList = GetDistancesListForFeature(sample, feature);
            var indexesList = new int[TextureClassCount];
            var classesList = new List<int>();
            var classesFlags = new List<int>();

            while (true)
            {
                int bestClass = GetBestDistance(distancesList, indexesList, classesList, classesFlags);
                indexesList[bestClass] += 1;

                if (indexesList[bestClass] == knownClasses[bestClass].KnownSamplesNumber(feature))
                {
                    classesFlags.Add(bestClass);
                    if (classesFlags.Count == knownClasses.Count)
                    {
                        return knownClasses[classesFlags[0]];
                    }
                }
                if (indexesList[bestClass] == RecognitionParameters.NeededNeighborsNumber)
                {
                    return knownClasses[bestClass];
                }
            }
        }
Пример #16
0
 private List<double>[] GetDistancesListForFeature(TextureSample sample, TextureFeatures feature)
 {
     var distancesList = new List<double>[TextureClassCount];
     for (int i = 0; i < TextureClassCount; ++i)
     {
         distancesList[i] = knownClasses[i].GetSortedDistances(sample, feature);
     }
     return distancesList;
 }
Пример #17
0
        private void SplitImageThread(object parameter)
        {
            SplitImageThreadParameter input = parameter as SplitImageThreadParameter;
            if (input == null)
            {
                throw new ArgumentException("Неправильный формат потока");
            }

            int fragmentSize = RecognitionParameters.RecognitionFragmentSize;
            var image = input.Image;
            var regions = input.ResultRegions;
            var samples = input.ResultSamples;
            int XCount = image.Width / fragmentSize;
            int YCount = image.Height / fragmentSize;

            for (int y = input.From; (y < input.To) && (!isRecognizingAborted); ++y)
            {
                for (int x = 0; (x < XCount) && (!isRecognizingAborted); ++x)
                {
                    int fromX = x * fragmentSize;
                    int fromY = y * fragmentSize;
                    int toX = fromX + fragmentSize;
                    int toY = fromY + fragmentSize;
                    TextureSample sample = null;
                    try
                    {
                        var imageData = image.GetGrayData(fromX, fromY, toX, toY);
                        sample = new TextureSample(imageData, false);
                    }
                    catch (Exception ex)
                    {
                        if (MessageBox.Show("Возникла ошибка. Продолжать?\n" + ex.Message, "Внимание", MessageBoxButtons.YesNo) == DialogResult.No)
                        {
                            isRecognizingAborted = true;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    lock ((regions as ICollection).SyncRoot)
                    {
                        regions.Add(new Rectangle(fromX, fromY, fragmentSize, fragmentSize));
                        samples.Add(sample);
                        recognitionProgress += 0.9 / (XCount * YCount);
                    }
                }
            }

            input.ResetEvent.Set();
        }
Пример #18
0
        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;
        }
Пример #19
0
 private double GetDistance(TextureSample sample, int featureIndex, TextureFeatures feature)
 {
     switch (feature)
     {
         case TextureFeatures.GLCM:
             return sample.GLCM.GetDistance(glcmFeatures[featureIndex]);
         case TextureFeatures.LBP:
             return sample.LBP.GetDistance(lbpFeatures[featureIndex]);
         default:
             return 0;
     }
 }
Пример #20
0
 internal List<double> GetSortedDistances(TextureSample sample, TextureFeatures feature)
 {
     var list = new List<double>();
     var count = KnownSamplesNumber(feature);
     for (int i = 0; i < count; ++i)
     {
         list.Add(GetDistance(sample, i, feature));
     }
     list.Sort();
     return list;
 }