Exemple #1
0
        private void applyButton_Click(object sender, EventArgs e)
        {
            if (InputImage == null)
            {
                return;                                                     // Get out if no input image
            }
            if (OutputImage != null)
            {
                OutputImage.Dispose();                                                  // Reset output image
            }
            OutputImage    = new Bitmap(InputImage.Size.Width, InputImage.Size.Height); // Create new output image
            Color[,] Image = new Color[InputImage.Size.Width, InputImage.Size.Height];  // Create array to speed-up operations (Bitmap functions are very slow)

            // Setup progress bar
            progressBar.Visible = true;
            progressBar.Minimum = 1;
            progressBar.Maximum = InputImage.Size.Width * InputImage.Size.Height * 1150;
            progressBar.Value   = 1;
            progressBar.Step    = 1;

            // Copy input Bitmap to array
            for (int x = 0; x < InputImage.Size.Width; x++)
            {
                for (int y = 0; y < InputImage.Size.Height; y++)
                {
                    Image[x, y] = InputImage.GetPixel(x, y);                // Set pixel color in array at (x,y)
                }
            }

            //==========================================================================================
            // TODO: include here your own code
            int[,] imageValues = FilterWhite(Image, 35);

            imageValues = Opening(imageValues, 1);
            imageValues = Closing(imageValues, 4);


            int[,] white = imageValues;

            imageValues = ObjectDistance(imageValues);
            imageValues = Invert(imageValues);

            int[,] watershedLines = WaterShed(imageValues);
            imageValues           = watershedLines;

            imageValues = SubtractImage(white, watershedLines);

            Tuple <int[, ], Dictionary <int, int> > objectInfo = FindObjectsWithArea(imageValues);

            int[,] distanceToBackgroundImage = ObjectDistance(objectInfo.Item1);
            int[,] perimeters = CalculatePerimeters(distanceToBackgroundImage, objectInfo.Item1);
            Dictionary <int, List <Tuple <int, int> > > perimetersPerObject = GetPerimetersPerObject(perimeters);

            Dictionary <int, ImageObject> objects = new Dictionary <int, ImageObject>();

            foreach (int objectID in objectInfo.Item2.Keys)
            {
                objects[objectID] = new ImageObject(objectInfo.Item2[objectID], perimetersPerObject[objectID], objectID);
            }

            List <int> drawables = new List <int>();

            foreach (ImageObject obj in objects.Values)
            {
                if (obj.Variance < 1.5 && obj.Area > 1000)
                {
                    drawables.Add(obj.ID);
                }

                /*if(obj.Variance > 0 && obj.Area < 5000 && obj.Area > 1000)
                 * {
                 *  drawables.Add(obj.ID);
                 * }*/
            }
            imageValues = FilterObjects(drawables, objectInfo.Item1);
            imageValues = Threshold(imageValues, 1);
            //imageValues = perimeters;
            //imageValues = FindObjects(imageValues);*/


            //==========================================================================================

            // Copy array to output Bitmap
            for (int x = 0; x < InputImage.Size.Width; x++)
            {
                for (int y = 0; y < InputImage.Size.Height; y++)
                {
                    int grayValue = imageValues[x, y];
                    OutputImage.SetPixel(x, y, Color.FromArgb(grayValue, grayValue, grayValue));               // Set the pixel color at coordinate (x,y)
                }
            }

            pictureBox2.Image   = (Image)OutputImage;                       // Display output image
            progressBar.Visible = false;                                    // Hide progress bar
        }
Exemple #2
0
        private void applyButton_Click(object sender, EventArgs e)
        {
            if (InputImage == null) return;                                 // Get out if no input image
            if (OutputImage != null) OutputImage.Dispose();                 // Reset output image
            OutputImage = new Bitmap(InputImage.Size.Width, InputImage.Size.Height); // Create new output image
            Color[,] Image = new Color[InputImage.Size.Width, InputImage.Size.Height]; // Create array to speed-up operations (Bitmap functions are very slow)

            // Setup progress bar
            progressBar.Visible = true;
            progressBar.Minimum = 1;
            progressBar.Maximum = InputImage.Size.Width * InputImage.Size.Height * 1150;
            progressBar.Value = 1;
            progressBar.Step = 1;

            // Copy input Bitmap to array            
            for (int x = 0; x < InputImage.Size.Width; x++)
            {
                for (int y = 0; y < InputImage.Size.Height; y++)
                {
                    Image[x, y] = InputImage.GetPixel(x, y);                // Set pixel color in array at (x,y)
                }
            }

            //==========================================================================================
            // TODO: include here your own code
            int[,] imageValues = FilterWhite(Image, 35);
            
            imageValues = Opening(imageValues, 1);
            imageValues = Closing(imageValues, 4);

            
            int[,] white = imageValues;
            
            imageValues = ObjectDistance(imageValues);
            imageValues = Invert(imageValues);
            
            int[,]watershedLines = WaterShed(imageValues);
            imageValues = watershedLines;
            
            imageValues = SubtractImage(white, watershedLines);
            
            Tuple<int[,],Dictionary<int,int>> objectInfo = FindObjectsWithArea(imageValues);
            int[,] distanceToBackgroundImage = ObjectDistance(objectInfo.Item1);
            int[,] perimeters = CalculatePerimeters(distanceToBackgroundImage,objectInfo.Item1);            
            Dictionary<int, List<Tuple<int, int>>> perimetersPerObject = GetPerimetersPerObject(perimeters);

            Dictionary<int, ImageObject> objects = new Dictionary<int, ImageObject>();

            foreach(int objectID in objectInfo.Item2.Keys)
            {
                objects[objectID] = new ImageObject(objectInfo.Item2[objectID],perimetersPerObject[objectID],objectID);
            }

            List<int> drawables = new List<int>();

            foreach(ImageObject obj in objects.Values)
            {
                
                if(obj.Variance < 1.5 && obj.Area > 1000)
                {
                    drawables.Add(obj.ID);
                }

                /*if(obj.Variance > 0 && obj.Area < 5000 && obj.Area > 1000)
                {
                    drawables.Add(obj.ID);
                }*/
            }
            imageValues = FilterObjects(drawables, objectInfo.Item1);
            imageValues = Threshold(imageValues, 1);
            //imageValues = perimeters;
            //imageValues = FindObjects(imageValues);*/
            

            //==========================================================================================

            // Copy array to output Bitmap
            for (int x = 0; x < InputImage.Size.Width; x++)
            {
                for (int y = 0; y < InputImage.Size.Height; y++)
                {
                    int grayValue = imageValues[x, y];
                    OutputImage.SetPixel(x, y, Color.FromArgb(grayValue, grayValue, grayValue));               // Set the pixel color at coordinate (x,y)
                }
            }

            pictureBox2.Image = (Image)OutputImage;                         // Display output image
            progressBar.Visible = false;                                    // Hide progress bar
        }