private void button2_Click(object sender, EventArgs e) { var watch = System.Diagnostics.Stopwatch.StartNew(); System.Drawing.Imaging.BitmapData objectsData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, b.PixelFormat); AForge.Imaging.UnmanagedImage grayImage = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(new AForge.Imaging.UnmanagedImage(objectsData)); AForge.Imaging.Filters.Threshold th = new AForge.Imaging.Filters.Threshold(128); th.ApplyInPlace(grayImage); // unlock image b.UnlockBits(objectsData); pictureBox1.Image = grayImage.ToManagedImage();; b = grayImage.ToManagedImage(); int count = 0; b2 = ResizeImage(b, new Size(size, size)); String s = ""; pictureBox2.Image = b2; for (int i = 0; i < Bsize; i++) { int k = 0; int t = 0; for (int j = 0; j < Bsize; j++) { if (b.GetPixel(i, j).R > 100 && b.GetPixel(i, j).G > 100 && b.GetPixel(i, j).B > 100) { s = s + 0; } else { s = s + 1; count++; k++; } if (b.GetPixel(j, i).R > 100 && b.GetPixel(j, i).G > 100 && b.GetPixel(j, i).B > 100) { } else { t++; } } richTextBox2.Text = richTextBox2.Text + k + ""; richTextBox1.Text = richTextBox1.Text + t + "\n"; h = h + "," + k; w = w + "," + t; s = s + "\n"; result = string.Join("", h); result = count + "\n" + h + "\n" + w; } label1.Text = "Total 1 is " + count; // System.IO.File.WriteAllText( "test.txt", s); }
public void ProcessImage(Bitmap input_image) { lock (balanceLock) { int side = Math.Min(input_image.Height, input_image.Width); Rectangle cropRect = new Rectangle(0, 0, side, side); // this is square that represents feed from camera g.DrawImage(input_image, new Rectangle(0, 0, input_image.Width, input_image.Height), cropRect, GraphicsUnit.Pixel); // place it on original bitmap // set new processed if (processed != null) { processed.Dispose(); } // Конвертируем изображение в градации серого processed = grayFilter.Apply(AForge.Imaging.UnmanagedImage.FromManagedImage(original)); // Пороговый фильтр применяем. Величина порога берётся из настроек, и меняется на форме threshldFilter.PixelBrightnessDifferenceLimit = ThresholdValue; threshldFilter.ApplyInPlace(processed); InvertFilter.ApplyInPlace(processed); Blober.ProcessImage(processed); AForge.Imaging.Blob[] blobs = Blober.GetObjectsInformation(); BlobCount = blobs.Length; if (blobs.Length > 0) { var BiggestBlob = blobs[0]; Recongnised = true; Blober.ExtractBlobsImage(processed, BiggestBlob, false); processed = BiggestBlob.Image; AForge.Point mc = BiggestBlob.CenterOfGravity; AForge.Point ic = new AForge.Point((float)BiggestBlob.Image.Width / 2, (float)BiggestBlob.Image.Height / 2); AngleRad = (ic.Y - mc.Y) / (ic.X - mc.X); Angle = (float)(Math.Atan(AngleRad) * 180 / Math.PI); } else { // TODO make arrengaments for No blobs case Recongnised = false; Angle = 0; AngleRad = -1; } if (number != null) { number.Dispose(); } number = processed.ToManagedImage(); } }
private void Go_Click(object sender, RoutedEventArgs e) { try { System.Windows.Media.Imaging.BitmapImage imagesource = imageBox.Source as System.Windows.Media.Imaging.BitmapImage; if (imageBox.Source == null) { System.Windows.MessageBox.Show("Add your image to the Image 1 box!"); return; } System.Drawing.Bitmap image = BmpImage2Bmp(imagesource); System.Windows.Media.Imaging.BitmapImage final = new System.Windows.Media.Imaging.BitmapImage(); if (!AForge.Imaging.Image.IsGrayscale(image)) { AForge.Imaging.Filters.ExtractChannel Grayer = new AForge.Imaging.Filters.ExtractChannel(0); image = Grayer.Apply(image); } if (Threshold.IsChecked == true) { imageBox.Source = null; AForge.Imaging.Filters.Threshold threshold = new AForge.Imaging.Filters.Threshold((int)slider.Value); threshold.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (GaussianFilter.IsChecked == true) { AForge.Imaging.Filters.GaussianBlur Gauss = new AForge.Imaging.Filters.GaussianBlur(); Gauss.Sigma = GaussSigma_Slide.Value; Gauss.Size = (int)GaussSize_Slide.Value; AForge.Imaging.UnmanagedImage unmanagedImage = AForge.Imaging.UnmanagedImage.FromManagedImage(image); AForge.Imaging.UnmanagedImage Dst = unmanagedImage.Clone(); Gauss.Apply(unmanagedImage, Dst); final = Bmp2BmpImage(Dst.ToManagedImage()); } else if (HiPass.IsChecked == true) { AForge.Imaging.Filters.Sharpen filter = new AForge.Imaging.Filters.Sharpen(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (Erode.IsChecked == true) { AForge.Imaging.Filters.Erosion filter = new AForge.Imaging.Filters.Erosion(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (Invert.IsChecked == true) { AForge.Imaging.Filters.Invert filter = new AForge.Imaging.Filters.Invert(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (EdgeDetector.IsChecked == true) { AForge.Imaging.Filters.CannyEdgeDetector filter = new AForge.Imaging.Filters.CannyEdgeDetector(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (Median.IsChecked == true) { AForge.Imaging.Filters.Median filter = new AForge.Imaging.Filters.Median(); filter.Size = (int)GaussSize_Slide.Value; filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (More.IsChecked == true) { if (Dilate.IsSelected) { AForge.Imaging.Filters.Dilatation filter = new AForge.Imaging.Filters.Dilatation(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } } imageBox.Source = final; TransformImage = image; boxWidth = imageBox.RenderSize.Width; boxHeight = imageBox.RenderSize.Height; } catch (Exception exc) { System.Windows.MessageBox.Show(exc.ToString()); } }
private void button1_Click(object sender, EventArgs e) { richTextBox1.Text = ""; System.Drawing.Imaging.BitmapData objectsData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, b.PixelFormat); AForge.Imaging.UnmanagedImage grayImage = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(new AForge.Imaging.UnmanagedImage(objectsData)); AForge.Imaging.Filters.Threshold th = new AForge.Imaging.Filters.Threshold(128); th.ApplyInPlace(grayImage); // unlock image b.UnlockBits(objectsData); pictureBox2.Image = grayImage.ToManagedImage();; Pen pen = new Pen(Color.Red, 2); Bitmap img = grayImage.ToManagedImage(); Graphics g = Graphics.FromImage(b); string text = "0"; int z = 0; { for (int j = 2; j < img.Height; j++) { int pixel = img.GetPixel(1, j).R; if (pixel == 255) { z = j; break; } // g.DrawString(j.ToString(), new System.Drawing.Font("Tahoma", 12, FontStyle.Bold), Brushes.Red, i, j); // g.DrawLine(pen, i, j, i+1, j+1 ); } // if (z!=0) // break; // text = text + "\n"; } g.DrawRectangle(pen, 0, 0, z, z); bitsize = z; int l = bitsize % 3; int lastblack = 0; int lastwhite = 0; for (int i = bitsize + bitsize / 3; i < img.Width;) { int ptb = 0; int ptw = 0; int bitsizecount = 0; for (int j = bitsize; j < img.Height; j++) { bitsizecount++; g.DrawLine(pen, i, i, i + 1, j); int pixel = img.GetPixel(i, j).R; if (pixel == 255) { ptb++; ptw = 0; } if (pixel == 0) { ptb = 0; ptw++; } if (bitsize == bitsizecount) { if (ptb >= ptw) { horijontal = horijontal + 0; } else { horijontal = horijontal + 1; } bitsizecount = 0; ptb = 0; ptw = 0; } } if (i % 3 == 0) { i = i + bitsize + 1; } else if (i % 5 == 0) { i = i + bitsize + 1; } else { i = i + bitsize + bitsize % 3; } } for (int j = bitsize + bitsize / 3; j < img.Height;) { int ptb = 0; int ptw = 0; int bitsizecount = 0; for (int i = bitsize; i < img.Height; i++) { bitsizecount++; g.DrawLine(pen, i, j, i, j + 1); int pixel = img.GetPixel(i, j).R; if (pixel == 255) { ptb++; ptw = 0; } if (pixel == 0) { ptb = 0; ptw++; } if (bitsize == bitsizecount) { if (ptb > ptw) { vertical = vertical + 0; } else { vertical = vertical + 1; } bitsizecount = 0; ptb = 0; ptw = 0; } } // j = j + bitsize+2 ; if (j % 3 == 0) { j = j + bitsize + 1; } else if (j % 5 == 0) { j = j + bitsize + 1; } else { j = j + bitsize + bitsize % 3; } } pictureBox2.Image = b; int la = horijontal.LastIndexOf('1'); horijontal = horijontal.Substring(0, la); int lb = vertical.LastIndexOf('1'); vertical = vertical.Substring(0, lb); String s1 = horijontal + "" + vertical; String s2 = vertical + "" + horijontal; int f = 8 - s2.Length % 8; for (int k = 0; k < f; k++) { s2 = s2 + "0"; s1 = s1 + "0"; } richTextBox1.Text = "Result Binarycode: " + vertical + horijontal; label1.Text = "Result TEXT: " + BinaryToString(vertical + horijontal);// +" - " + BinaryToString(horijontal); label2.Text = "Bitsize: " + bitsize + " Datalength " + s2.Length; }
/// <summary> /// Get spectrogram image. /// </summary> /// <param name="audioData">Audio data.</param> /// <param name="height">Spectrogram height.</param> /// <param name="width">Spectrogram width.</param> /// <returns>Spectrogram image.</returns> public static Bitmap GetImage(double[,] audioData, int height, int width) { var managedImage = new Bitmap(width, height, PixelFormat.Format24bppRgb); AForge.Imaging.UnmanagedImage image = AForge.Imaging.UnmanagedImage.FromManagedImage(managedImage); int pixelSize = Image.GetPixelFormatSize(image.PixelFormat) / 8; // image dimension int imageWidth = image.Width; int imageHeight = image.Height; int stride = image.Stride; const int StartX = 0; int stopX = imageWidth - 1; // spectrogram is drawn from the bottom const int StartY = 0; int stopY = imageHeight - 1; // min, max, range double min; double max; audioData.MinMax(out min, out max); double range = max - min; int offset = stride - ((stopX - StartX + 1) * pixelSize); int heightOffset = imageHeight; unsafe { // do the job byte *ptr = (byte *)image.ImageData.ToPointer() + (StartY * stride) + (StartX * pixelSize); // height for (int y = StartY; y <= stopY; y++) { // width for (int x = StartX; x <= stopX; x++, ptr += pixelSize) { // required to render spectrogram correct way up int spectrogramY = heightOffset - 1; // NormaliseMatrixValues and bound the value - use min bound, max and 255 image intensity range // this is the amplitude double value = (audioData[x, spectrogramY] - min) / range; double colour = 255.0 - Math.Floor(255.0 * value); colour = Math.Min(colour, 255); colour = Math.Max(colour, 0); byte paintColour = Convert.ToByte(colour); // set colour ptr[AForge.Imaging.RGB.R] = paintColour; ptr[AForge.Imaging.RGB.G] = paintColour; ptr[AForge.Imaging.RGB.B] = paintColour; } ptr += offset; heightOffset--; } } return(image.ToManagedImage()); }