/* 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); } }
public Image Bernsen(Image sourceImage, int windowSize, int eps) { int matrixSize = sourceImage.Height * sourceImage.Width; Image im = sourceImage; Bitmap myBitmap = new Bitmap(sourceImage.Width, sourceImage.Height); try { if (windowSize % 2 != 0) { //odczyt obrazu using (Bitmap bmp = new Bitmap(sourceImage)) { Color originalColor; Color framePixelColor; GrayPixel[] _pixels = new GrayPixel[matrixSize]; GrayPixel _framePixelTemp = new GrayPixel(); GrayPixel[] _framePixels = new GrayPixel[(windowSize - 1) * (windowSize - 1)]; int j = 0; int counter = 0; int distance = Convert.ToInt32((windowSize - 1) * 0.5); int _maxTempValue = 0; int _minTempValue = 0; for (int i = 0; i < matrixSize; i++) { _pixels[i] = new GrayPixel(); } for (int i = 0; i < (windowSize - 1) * (windowSize - 1); i++) { _framePixels[i] = new GrayPixel(); } #region for (int x = 0; x < sourceImage.Width; x++) { for (int y = 0; y < sourceImage.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)); _pixels[j].R = originalColor.R; _pixels[j].G = originalColor.G; _pixels[j].B = originalColor.B; _pixels[j].x = x; _pixels[j].y = y; //tworzymy liste o wielkosci okna naokolo pixela if (x >= distance && x < sourceImage.Width - distance) { if (y >= distance && y < sourceImage.Height - distance) { ; //_framePixels.Clear(); counter = 0; //i,k - obramowanie okna po ktorym sie poruszam naokolo danego pixela //pobieram pixele naokolo pixela i licze prog za pomoca ich wartosci for (int i = x - distance; i < x + distance; i++) { for (int k = y - distance; k < y + distance; k++) { ; //pobieram wartosci dla kazdego pixela z okna i odrazu licze prog lokalny dla obecnego pixela(centralnego z okna) framePixelColor = bmp.GetPixel(i, k); _framePixels[counter].value = (int)((framePixelColor.R * .3) + (framePixelColor.G * .59) + (framePixelColor.B * .11)); _framePixels[counter].x = i; _framePixels[counter].y = k; //lista pixeli w oknie - zle dodaje do listy? WTF - do poprawy //_framePixels.Add(_framePixelTemp); counter++; } } //obliczenia na kazdym pixelu w liscie w danej chwili by obliczyc prog dla pixela na pozycji globalnej x,y _maxTempValue = _framePixels.Max(max => max.value); //max // min _minTempValue = _framePixels.Min(min => min.value); // oblicz prog dla danego globalnego pixela if (_maxTempValue - _minTempValue <= eps) { _pixels[j].threshold = 121;//Convert.ToInt32((_maxTempValue + _minTempValue) * 0.5); } else { _pixels[j].threshold = Convert.ToInt32((_maxTempValue + _minTempValue) * 0.5); } } } else { //tu trafiaja przypadki, kiedy pixel jest za blisko brzegu obrazka - nie ruszac pixeli i wziac je z oryginalnego obrazka } j++; } } #endregion j = 0; for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++) { for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++) { if (Xcount >= windowSize && Ycount >= windowSize && Xcount <= myBitmap.Width - windowSize && Ycount <= myBitmap.Height - windowSize) { if (_pixels[j].value > _pixels[j].threshold) { myBitmap.SetPixel(Xcount, Ycount, Color.White); } else if (_pixels[j].value <= _pixels[j].threshold) { myBitmap.SetPixel(Xcount, Ycount, Color.Black); } } else { myBitmap.SetPixel(Xcount, Ycount, Color.FromArgb(_pixels[j].R, _pixels[j].G, _pixels[j].B)); } j++; } } im = myBitmap; } } else { return(im); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return(im); }