コード例 #1
0
        public void GetComponentFromImageNormalCase()
        {
            PCA p = new PCA();

            double[,] matrixArr = new double[10, 2]
            {
                { 1.507, 0.988 },
                { 2.107, -9.312 },
                { 1.407, 1.798 },
                { 1.397, 2.098 },
                { -9.563, 1.988 },
                { 0.797, 0.888 },
                { 2.607, 0.488 },
                { -0.493, 1.588 },
                { 0.627, -0.412 },
                { -0.393, -0.112 }
            };

            double[] expectation = new double[1] {
                0.083723606
            };

            double[,] img = new double[1, 2] {
                { 1, 1 }
            };

            p.Train(matrixArr);
            double[] res = p.GetComponentsFromImage(img, 1);
            Console.WriteLine(res.Length);
            CollectionAssert.AreEqual(expectation,
                                      res,
                                      new Comparer(floatingPointTolerance));
        }
コード例 #2
0
        public void GetComponentFromImageException()
        {
            PCA p = new PCA();

            double[,] image = new double[2, 2] {
                { 5, 10 }, { 10, 5 }
            };
            Assert.Throws <NullReferenceException>(() => p.GetComponentsFromImage(image, 1));
        }
コード例 #3
0
        public void GetComponentFromImageNormalCaseUShortArray()
        {
            PCA p = new PCA();

            byte[] matrixArr = new byte[16]
            {
                10, 20, 59, 223,
                24, 80, 99, 31,
                54, 113, 215, 70,
                167, 131, 200, 101
            };
            byte[] matrixArr2 = new byte[16]
            {
                10, 20, 59, 223,
                24, 80, 99, 31,
                54, 113, 215, 70,
                167, 131, 200, 101
            };

            double[] expectation = new double[1] {
                5130
            };

            byte[] imgArr = new byte[16] {
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
            };

            List <UShortArrayAsImage> img = new List <UShortArrayAsImage>();

            img.Add(new UShortArrayAsImage(matrixArr, 4, 4));
            img.Add(new UShortArrayAsImage(matrixArr2, 4, 4));



            p.Train(img);
            double[] res = p.GetComponentsFromImage(img[0], 1);
            Console.WriteLine(res.Length);
            CollectionAssert.AreEqual(expectation,
                                      res,
                                      new Comparer(floatingPointTolerance));
        }
コード例 #4
0
        private async Task PerformAnalysis(String path, Rectangle rectangle)
        {
            UShortArrayAsImage image = null;

            double[] pcaComponents = null;
            int      tasksComplete = 0;

            UpdateStatus(path, startingImageStatusStr);
            List <Task> tasks = new List <Task>()
            {
                new Task(() =>
                {
                    var file = db.FileStorage.FindById($"images/{path}");
                    var ms   = new MemoryStream();
                    file.CopyTo(ms);
                    ms.Seek(0, 0);
                    image = DicomFile.Open(ms).GetUshortImageInfo();

                    UpdateStatus(path, loadedImageStatusStr);
                }),
                new Task(() =>
                {
                    image = Normalization.GetNormalizedImage(image, rectangle,
                                                             int.Parse(Configuration.Get("sizeImageToAnalyze")));
                    db.FileStorage.Upload($"images/{path}-cropped", $"{path}-cropped",
                                          image.GetPngAsMemoryStream());

                    UpdateStatus(path, croppedImageStatusStr);
                }),
                new Task(() =>
                {
                    image = Contrast.ApplyHistogramEqualization(image);

                    db.FileStorage.Upload($"images/{path}-croppedContrast", $"{path}-croppedContrast",
                                          image.GetPngAsMemoryStream());

                    UpdateStatus(path, contrastImageStatusStr);
                }),
                new Task(() =>
                {
                    //PCA
                    PCA pca = PCA.LoadModelFromFile(Configuration.Get("PcaModelLocation"));

                    if (!int.TryParse(Configuration.Get("componentsToUse"), out int components))
                    {
                        components = pca.Eigenvalues.Length;
                    }
                    pcaComponents = pca.GetComponentsFromImage(image, components);
                    UpdateStatus(path, pcaImageStatusStr);
                }),
                new Task(() =>
                {
                    //SVM
                    SVMProblem svmProblem = new SVMProblem();

                    // add all the components to an SVMNode[]
                    SVMNode[] nodes = new SVMNode[pcaComponents.Length];
                    for (int i = 0; i < pcaComponents.Length; i++)
                    {
                        nodes[i] = new SVMNode(i + 1, pcaComponents[i]);
                    }

                    svmProblem.Add(nodes, 0);

                    svmProblem = svmProblem.Normalize(SVMNormType.L2);

                    SVMModel svmModel = SVM.LoadModel(Configuration.Get("ModelLocation"));

                    double[] results = svmProblem.PredictProbability(svmModel, out var probabilities);

                    var analysis              = db.GetCollection <Analysis>("analysis");
                    Analysis currentAnalysis  = analysis.FindOne(x => x.Id.ToString().Equals(path));
                    currentAnalysis.Certainty = results[0] == 0 ? probabilities[0][1] * 100 : probabilities[0][0] * 100;
                    currentAnalysis.Diagnosis = results[0] == 0
                        ? DdsmImage.Pathologies.Benign
                        : DdsmImage.Pathologies
                                                .Malignant;
                    analysis.Update(currentAnalysis);


                    UpdateStatus(path, svmImageStatusStr);
                })
            };

            foreach (Task task in tasks)
            {
                task.Start();
                await task;

                // lets set percentage done:
                var      analysis        = db.GetCollection <Analysis>("analysis");
                Analysis currentAnalysis = analysis.FindOne(x => x.Id.ToString().Equals(path));
                currentAnalysis.PercentageDone = (++tasksComplete * 100) / tasks.Count;
                analysis.Update(currentAnalysis);
            }

            UpdateStatus(path, doneStatusStr);
        }