public async Task <IActionResult> UploadFile(List <IFormFile> files)
        {
            if (files.Count == 0)
            {
                return(RedirectToAction("Index", "Home", new { error = true }));
            }
            var formFile = files.First();

            if (formFile.Length <= 0 || !formFile.FileName.EndsWith(".dcm") ||
                !IsDcomFileValid(formFile.OpenReadStream()))
            {
                return(RedirectToAction("Index", "Home", new { error = true }));
            }

            // use a unique id as filename/id
            Guid id;
            var  analysis          = db.GetCollection <Analysis>("analysis");
            UShortArrayAsImage img = DicomFile.Open(formFile.OpenReadStream()).GetUshortImageInfo();

            Analysis toInsert = new Analysis();

            analysis.Insert(toInsert);
            db.FileStorage.Upload($"images/{toInsert.Id.ToString()}", toInsert.Id.ToString(),
                                  formFile.OpenReadStream());
            id = toInsert.Id;


            return(RedirectToAction("SelectRegion", "Analyze", new { FileName = id }));
        }
Exemplo n.º 2
0
        public void UShortPixelArrayGetSetTest()
        {
            ushort[,] pixelArray = new ushort[, ] {
                { 2, 50 }, { 0, 10 }
            };
            UShortArrayAsImage image = new UShortArrayAsImage(pixelArray);

            image.PixelArray = pixelArray;
            CollectionAssert.AreEqual(pixelArray, image.PixelArray);
        }
        public void ApplyHistogramEqualizationTest()
        {
            var pixelArrayTestValuesResult = new ushort[, ] {
                { 0, 21845 }, { 43690, 65535 }
            };
            var image = new UShortArrayAsImage(new ushort[, ] {
                { 0, 20001 }, { 40001, 65535 }
            });

            image = Contrast.ApplyHistogramEqualization(image);

            CollectionAssert.AreEqual(pixelArrayTestValuesResult, image.PixelArray);
        }
        public void ApplyHistogramEqualizationTestBlackImage()
        {
            var pixelArrayTestValuesResult = new ushort[, ] {
                { 0, 0 }, { 0, 0 }
            };
            var image = new UShortArrayAsImage(new ushort[, ] {
                { 0, 0 }, { 0, 0 }
            });

            image = Contrast.ApplyHistogramEqualization(image);

            CollectionAssert.AreEqual(pixelArrayTestValuesResult, image.PixelArray);
        }
Exemplo n.º 5
0
        public void GetNormalizedImageTest2()
        {
            ushort[,] testValue = new ushort[, ] {
                { 1, 1, 1, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0 }, { 0, 0, 1, 0, 0, 1 }, { 0, 0, 1, 0, 0, 1 }, { 0, 0, 1, 0, 0, 1 }, { 0, 0, 1, 1, 1, 0 }
            };
            UShortArrayAsImage testImage = new UShortArrayAsImage(testValue);

            Rectangle testRect        = new Rectangle(0, 0, 4, 2);
            var       normalizedImage = GetNormalizedImage(testImage, testRect, 4);

            Rectangle squareRect   = new Rectangle(0, testRect.Y - (testRect.Width - testRect.Height) / 2, 4, 4);
            var       croppedImage = Crop(squareRect, testImage);

            Assert.AreEqual(croppedImage.PixelArray, normalizedImage.PixelArray);
        }
Exemplo n.º 6
0
        public void ResizeImageUpTest()
        {
            // Expected Value
            ushort[,] realValue = new ushort[, ] {
                { 1, 1, 1, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0 }, { 0, 0, 1, 0, 0, 1 }, { 0, 0, 1, 0, 0, 1 }, { 0, 0, 1, 0, 0, 1 }, { 0, 0, 1, 1, 1, 0 }
            };

            //Test image to pass to method
            ushort[,] testValue = new ushort[, ] {
                { 1, 1, 0, 0 }, { 0, 1, 0, 1 }, { 0, 1, 0, 1 }, { 0, 1, 1, 0 }
            };
            UShortArrayAsImage testImage = new UShortArrayAsImage(testValue);

            Assert.AreEqual(realValue, ResizeImage(testImage, 6).PixelArray);
        }
Exemplo n.º 7
0
        public void CropStartOutOfBoundsCaseTest()
        {
            Rectangle testRect = new Rectangle(-1, -1, 2, 2);

            ushort[,] testValue = new ushort[, ] {
                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 1, 1 }
            };
            ushort[,] realCrop = new ushort[, ] {
                { 0, 0 }, { 0, 1 }
            };

            UShortArrayAsImage image    = new UShortArrayAsImage(testValue);
            UShortArrayAsImage testCrop = Crop(testRect, image);

            Assert.AreEqual(realCrop, testCrop.PixelArray);
        }
Exemplo n.º 8
0
        public void ResizeImageDownTest()
        {
            //Expected value
            ushort[,] realValue = new ushort[, ] {
                { 1, 0 }, { 0, 0 }
            };

            //Test image to pass to method
            ushort[,] testValue = new ushort[, ] {
                { 1, 1, 0, 0 }, { 0, 1, 0, 1 }, { 0, 1, 0, 1 }, { 0, 1, 1, 0 }
            };
            UShortArrayAsImage testImage = new UShortArrayAsImage(testValue);

            //Method result
            var resizeImageDown = ResizeImage(testImage, 2);

            Assert.AreEqual(realValue, resizeImageDown.PixelArray);
        }
Exemplo n.º 9
0
 ///<summary>
 ///Projects a image onto a eigenspace that is made then the method Train is run
 ///</summary>
 ///<param name="image">a UShortArrayAsImage to perform projectionn with</param>
 ///<param name="numberOfComponents">Number of eigenvectors to project image onto</param>
 public double[] GetComponentsFromImage(UShortArrayAsImage image, int numberOfComponents)
 {
     double[,] tmpImage = new double[image.Width, image.Height];
     Array.Copy(image.PixelArray, tmpImage, image.PixelArray.Length);
     return(GetComponentsFromImage(tmpImage, numberOfComponents));
 }
        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);
        }