Exemple #1
0
        public static GrayPixelHistogram[] getAllPossibleConfigurationsGrayPixels()
        {
            GrayPixelHistogram[] hst = new GrayPixelHistogram[_maxPixelValue];
            for (int i = 0; i < _maxPixelValue; i++)
            {
                hst[i] = new GrayPixelHistogram();
            }

            for (int i = 0; i < _maxPixelValue; i++)
            {
                hst[i].Value = i;
                hst[i].Count = 0;
            }

            return(hst);
        }
Exemple #2
0
        /* private void Otsu_Click(object sender, EventArgs e)
         * {
         *   var histogram = getGrayHistogramFromImage(_sampleImage);
         *
         * }*/

        public static GrayPixelHistogram[] getGrayHistogramFromImage(Image image)
        {
            int matrixSize = image.Height * image.Width;
            int j          = 0;

            using (Bitmap bmp = new Bitmap(image))
            {
                Color originalColor;
                GrayPixelHistogram[] pixels       = new GrayPixelHistogram[matrixSize];
                GrayPixelHistogram[] allHstValues = new GrayPixelHistogram[_maxPixelValue];
                GrayPixelHistogram[] sortedPixels = new GrayPixelHistogram[_maxPixelValue];

                for (int i = 0; i < matrixSize; i++)
                {
                    pixels[i]          = new GrayPixelHistogram();
                    _originalPixels[i] = new GrayPixel();
                }

                for (int i = 0; i < _maxPixelValue; i++)
                {
                    allHstValues[i] = new GrayPixelHistogram();
                    sortedPixels[i] = new GrayPixelHistogram();
                }
                for (int x = 0; x < image.Width; x++)
                {
                    for (int y = 0; y < image.Height; y++)
                    {
                        originalColor = bmp.GetPixel(x, y);
                        //create the grayscale version of the pixel
                        pixels[j].Value          = (int)((originalColor.R * .3) + (originalColor.G * .59) + (originalColor.B * .11));
                        _originalPixels[j].value = pixels[j].Value;
                        _originalPixels[j].R     = originalColor.R;
                        _originalPixels[j].G     = originalColor.G;
                        _originalPixels[j].B     = originalColor.B;
                        _originalPixels[j].x     = x;
                        _originalPixels[j].y     = y;

                        j++;
                    }
                }

                var listOfUniqueVectors = pixels.GroupBy(l => l.Value).Select(g => new
                {
                    Value = g.Key,
                    Count = g.Select(l => l.Value).Count()
                });

                var sortedList = listOfUniqueVectors.OrderBy(r => r.Value);
                allHstValues = getAllPossibleConfigurationsGrayPixels();

                //int _counter = 0;
                //foreach (var item in sortedList)
                //{
                //    sortedPixels[_counter].Value = item.Value;
                //    sortedPixels[_counter].Count = item.Count;
                //    _counter++;
                //}

                //rewrite vectors into empty matrix
                for (int i = 0; i < _maxPixelValue; i++)
                {
                    foreach (var item in sortedList)
                    {
                        if (item.Value == allHstValues[i].Value)
                        {
                            allHstValues[i].Value = Convert.ToInt32(item.Value);
                            allHstValues[i].Count = item.Count;
                        }
                    }
                }
                return(allHstValues);
            }
        }