public Bitmap ToDilatation(Bitmap Im) { AForge.Imaging.Filters.Dilatation Img = new Dilatation(); Bitmap bmImage = AForge.Imaging.Image.Clone(new Bitmap(Im), PixelFormat.Format24bppRgb); return(Img.Apply(bmImage)); }
public static Bitmap ApplyFilters(Image img) { Bitmap imagem = new Bitmap(img); imagem = imagem.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); cor.Blue = new AForge.IntRange(200, 255); cor.Red = new AForge.IntRange(200, 255); cor.Green = new AForge.IntRange(200, 255); Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering(); Closing close = new Closing(); GaussianSharpen gs = new GaussianSharpen(); ContrastCorrection cc = new ContrastCorrection(); bc.MinHeight = 10; FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter); var filteredImage = seq.Apply(imagem); return(filteredImage); }
private void damarBulToolStripMenuItem_Click(object sender, EventArgs e) { ApplyFilter(new ExtractChannel(RGB.G)); if (orginalBitmap.PixelFormat != PixelFormat.Format24bppRgb) { MessageBox.Show("Contrast filter using HSL color space is available for color images only", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } ContrastCorrection filter = new ContrastCorrection(); filter.Factor = 15; ApplyFilter(filter); GaussianBlur filter2 = new GaussianBlur(); // GaussianSharpen filter = new GaussianSharpen(); filter2.Size = 5; filter2.Sigma = 0.6; ApplyFilter(filter2); resultBitmap = Filtreler.Canny(resultBitmap); maskBitmap = (Bitmap)imageCollection1Mask.Images[index];// combobox ta seçilmiş image in mask image ini getiriyor resultBitmap = Filtreler.Maskeleme(resultBitmap, maskBitmap); Dilatation filter3 = new Dilatation(); ApplyFilter(filter3); pictureBox1.Image = resultBitmap; }
private void reapplyToolStripMenuItem1_Click(object sender, EventArgs e) { Dilatation filter = new Dilatation(); // apply the filter pictureBox2.Image = filter.Apply((Bitmap)pictureBox2.Image); }
private string reconhecerCaptcha(Image img) { Bitmap imagem = new Bitmap(img); imagem = imagem.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); cor.Blue = new AForge.IntRange(200, 255); cor.Red = new AForge.IntRange(200, 255); cor.Green = new AForge.IntRange(200, 255); Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering(); Closing close = new Closing(); GaussianSharpen gs = new GaussianSharpen(); ContrastCorrection cc = new ContrastCorrection(); bc.MinHeight = 10; FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter); imagem = seq.Apply(imagem); //imagem.Save(Directory.GetCurrentDirectory() + "\\Captcha2.png", System.Drawing.Imaging.ImageFormat.Png); string reconhecido = OCR(imagem); //string reconhecido = ocr.Principal(Directory.GetCurrentDirectory() + "\\Captcha2.png"); return(reconhecido); }
private string reconhecerCaptcha(Image img) { Bitmap _bitmap = new Bitmap(img); _bitmap = _bitmap.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); cor.Blue = new AForge.IntRange(200, 255); cor.Red = new AForge.IntRange(200, 255); cor.Green = new AForge.IntRange(200, 255); Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering(); Closing close = new Closing(); GaussianSharpen gs = new GaussianSharpen(); ContrastCorrection cc = new ContrastCorrection(); bc.MinHeight = 10; FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter); pictureBox1.Image = seq.Apply(_bitmap); string recognedString = OCR((Bitmap)pictureBox1.Image); return(recognedString); }
public static Task <(Bitmap, string)> DeCaptchAsync(Image img) { return((Task <(Bitmap, string)>)Task.Run(() => { string value; Bitmap bmp = new Bitmap(img); bmp = bmp.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); //cor.Blue = new AForge.IntRange(200, 255); cor.Red = new AForge.IntRange(50, 255); //cor.Green = new AForge.IntRange(200, 255); Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering() { MinHeight = 10 }; Closing close = new Closing(); GaussianSharpen gs = new GaussianSharpen(); ContrastCorrection cc = new ContrastCorrection(); FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter, dilatation); Image image = seq.Apply(bmp); value = OCR((Bitmap)image); return ((Bitmap)image, value); })); }
private string reconhecerCaptcha(Image img) { Bitmap imagem = new Bitmap(img); imagem = imagem.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); cor.Blue = new AForge.IntRange(200, 255); cor.Red = new AForge.IntRange(200, 255); cor.Green = new AForge.IntRange(200, 255); Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering(); Closing close = new Closing(); GaussianSharpen gs = new GaussianSharpen(); ContrastCorrection cc = new ContrastCorrection(); bc.MinHeight = 10; GrayscaleRMY gray = new GrayscaleRMY(); Threshold thr = new Threshold(200); Difference diff = new Difference(dilatation.Apply(imagem)); FiltersSequence seq = new FiltersSequence(diff, inverter, erosion, gray, thr, cc); pictureBox.Image = seq.Apply(imagem); string reconhecido = OCR((Bitmap)pictureBox.Image); return(reconhecido); }
public static Bitmap thin(Bitmap source) // 선 가늘게 { ///////////// ini 객체 생성 시작 ///////////////////////////////////////////////////// //현재 프로그램이 실행되고 있는정보 가져오기: 디버깅 모드라면 bin/debug/프로그램명.exe FileInfo exefileinfo = new FileInfo(@"C:\Program Files\PLOCR\PLOCR.exe"); string pathini = exefileinfo.Directory.FullName.ToString(); //프로그램 실행되고 있는데 path 가져오기 string fileName = @"\PLOCRconfig.ini"; // 환경설정 파일명 string filePath = pathini + fileName; //ini 파일 경로 DocumentAnalysis.IniUtil ini = new DocumentAnalysis.IniUtil(filePath); // 만들어 놓았던 iniUtil 객체 생성(생성자 인자로 파일경로 정보 넘겨줌) //////////// ini 객체 생성 끝 ///////////////////////////////////////////////////////// int order = int.Parse(ini.GetIniValue("선굵기값", "가늘게횟수")); for (int i = 0; i < order; i++) { Bitmap tmp = (Bitmap)source; // 중요! 한번 이미지 처리가 끝난 비트맵 source 는 clone 함수로 보내기 전에 다시 한번 (Bitmap) 처리 해줘야함, 이유는 잘 모르겠음 // convert to 24 bits per pixel source = imageProcess.Clone(tmp, PixelFormat.Format24bppRgb); // delete old image tmp.Dispose(); Dilatation filter = new Dilatation(); filter.ApplyInPlace(source); } return(source); }
public Bitmap Dilatation(Bitmap bitmap) { var bmp = ReverseBitmapColors(bitmap); Dilatation filter = new Dilatation(); filter.ApplyInPlace(bmp); return(ReverseBitmapColors(bmp)); }
public static Bitmap Dilatation(Bitmap Imagem) { Dilatation filter = new Dilatation(); Imagem = Imagem.Clone(new Rectangle(0, 0, Imagem.Width, Imagem.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Imagem = filter.Apply(Imagem); return(Imagem); }
public static Bitmap DilatImage(Bitmap bit) { Bitmap bmp = bit.Clone(new Rectangle(0, 0, bit.Width, bit.Height), PixelFormat.Format24bppRgb); Dilatation dila = new Dilatation(); return(dila.Apply(bmp)); }
public static BitmapImage DilatationImage(this BitmapImage bitmapimage) { Bitmap bitmap = bitmapimage.BitmapImage2Bitmap(); Dilatation dilatation = new Dilatation(); dilatation.Apply(bitmap); return(bitmap.ToBitmapImage()); }
/// <summary> /// Creates a comic rendered copy of the input image. /// </summary> public override Bitmap Render(Bitmap sourceImage) { GrayscaleToRGB convertColor = new GrayscaleToRGB(); if (sourceImage.PixelFormat == PixelFormat.Format8bppIndexed) { sourceImage = convertColor.Apply(sourceImage); } BilateralBlur blur = new BilateralBlur(3, 0.1); Bitmap comic = blur.Apply(sourceImage); // Edges Bitmap grayscale = Grayscale.CommonAlgorithms.Y.Apply(comic); SobelEdgeDetector sobelEdge = new SobelEdgeDetector(); sobelEdge.ScaleIntensity = true; Bitmap edgeLayer = sobelEdge.Apply(grayscale); edgeLayer = convertColor.Apply(edgeLayer); Invert invertEdge = new Invert(); invertEdge.ApplyInPlace(edgeLayer); HSLLinear edgeLinear = new HSLLinear(); edgeLinear.InLuminance.Min = 0; edgeLinear.InLuminance.Max = 0.8; edgeLinear.ApplyInPlace(edgeLayer); // highlights Bitmap highlightLayer = invertEdge.Apply(edgeLayer); Dilatation highlightDilitation = new Dilatation(); highlightDilitation.ApplyInPlace(highlightLayer); BrightnessCorrection highlightBright = new BrightnessCorrection(-0.35); highlightBright.ApplyInPlace(highlightLayer); ColorDodge highlightBlend = new ColorDodge(highlightLayer); highlightBlend.ApplyInPlace(comic); // Merge edges with working layer Multiply multEdge = new Multiply(edgeLayer); multEdge.ApplyInPlace(comic); return(comic); }
void DilasiToolStripMenuItemClick(object sender, EventArgs e) { //jika gambar kosong/null maka akan mengembalikan nilai kosong/null if (gambar == null) { return; } //membuat filter dari inisiasi class Dilatation() pada objek dilatation Dilatation dilatation = new Dilatation( ); //clone variable gambar pada variable gambar2 gambar2 = (Bitmap)gambar.Clone(); //aplikasikan filter objek dilatation pada gambar2 dilatation.ApplyInPlace(gambar2); //tampilkan hasil gambar2 yang sudah diaplikasikan filter pada pictureBox2 pictureBox2.Image = gambar2; }
public static Bitmap Limpar(Image img) { Bitmap imagem = new Bitmap(img); Bitmap newBitmap = new Bitmap(imagem.Width, imagem.Height); Graphics g = Graphics.FromImage(newBitmap); g.FillRectangle(Brushes.White, 0, 0, imagem.Width, imagem.Height); ColorMatrix colorMatrix = new ColorMatrix( new float[][] { new float[] { .3f, .3f, .3f, 0, 0 }, new float[] { .59f, .59f, .59f, 0, 0 }, new float[] { .11f, .11f, .11f, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } }); ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(colorMatrix); g.DrawImage(imagem, new Rectangle(0, (int)(imagem.Height * 0.1), imagem.Width, (int)(imagem.Height * 0.8)), 0, imagem.Height * 0.1f, imagem.Width, imagem.Height * 0.8f, GraphicsUnit.Pixel, attributes); g.Dispose(); imagem = newBitmap; imagem = imagem.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); cor.Blue = new AForge.IntRange(200, 255); cor.Red = new AForge.IntRange(200, 255); cor.Green = new AForge.IntRange(200, 255); Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering(); GaussianSharpen gs = new GaussianSharpen(); ContrastCorrection cc = new ContrastCorrection(); bc.MinHeight = 3; FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter); return((Bitmap)seq.Apply(imagem)); }
/// <returns>Morphology opened image</returns> public static Bitmap MorphologyOpening(this Bitmap image, int kernel) { if (image.PixelFormat != PixelFormat.Format8bppIndexed) { throw new NotSupportedException("Operation can be applied to binary 8bpp images only");//we use morphology only for binarized images } Erosion morphologyErosion = new Erosion(); Bitmap temp = morphologyErosion.Apply(image); for (int i = 0; i < kernel - 1; i++) { temp = morphologyErosion.Apply(temp); } Dilatation morphologyDilatation = new Dilatation(); for (int i = 0; i < kernel; i++) { temp = morphologyDilatation.Apply(temp); } return(temp); }
private static string ResoudreItalien(Bitmap captcha) { Bitmap imagem = captcha; imagem = imagem.Clone(new Rectangle(0, 0, captcha.Width, captcha.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); cor.Blue = new AForge.IntRange(200, 255); cor.Red = new AForge.IntRange(200, 255); cor.Green = new AForge.IntRange(200, 255); Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering(); Closing close = new Closing(); GaussianSharpen gs = new GaussianSharpen(); ContrastCorrection cc = new ContrastCorrection(); bc.MinHeight = 10; FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter); string reconhecido = OCRItalien((Bitmap)seq.Apply(imagem)); return reconhecido; }
/// <summary> /// This method binarize dnd dilate image. /// </summary> /// <param name="image"></param> /// <returns></returns> private Bitmap BinarizeAndDilationWithMedian(Bitmap image) { int[,] kernel = { { -2, -1, 0 }, { -1, 1, 1 }, { 0, 1, 2 } }; // create filter Convolution Convolution = new Convolution(kernel); // apply the filter Convolution.ApplyInPlace(image); var bmp8bpp = Grayscale.CommonAlgorithms.BT709.Apply(image); Invert invert = new Invert(); invert.ApplyInPlace(bmp8bpp); Dilatation dilatation = new Dilatation(); dilatation.ApplyInPlace(bmp8bpp); invert.ApplyInPlace(bmp8bpp); Median median = new Median(); median.ApplyInPlace(bmp8bpp); Closing closing = new Closing(); closing.ApplyInPlace(bmp8bpp); OtsuThreshold OtsuThreshold = new OtsuThreshold(); OtsuThreshold.ApplyInPlace(bmp8bpp); //bmp8bpp.Save(@"C:\Users\Maor\Desktop\mode\BinarizeAndDilationWithMedian.jpeg"); return(bmp8bpp); }
public static Bitmap FormatImageRecaptcha(Bitmap imagem) { imagem = imagem.Clone(new Rectangle(0, 0, imagem.Width, imagem.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Closing close = new Closing(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); cor.Blue = new AForge.IntRange(200, 255); cor.Red = new AForge.IntRange(200, 255); cor.Green = new AForge.IntRange(200, 255); Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering(); bc.MinHeight = 30; // chỉ bắt các đối tượng có chiều cao >=30 GaussianSharpen gs = new GaussianSharpen(1, 2); // tăng độ đậm của chi tiết lên gấp đôi ContrastCorrection cc = new ContrastCorrection(); FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, open, cc, bc, cor); return(seq.Apply(imagem)); }
private string SolveCaptcha(Image img) { Bitmap imagem = new Bitmap(img); SetPixelColor(imagem, false); SetPixelColor(imagem, false); string filename = "test/" + Guid.NewGuid().ToString() + ".png"; SetPixelColor(imagem, false); imagem = ProcessImage(imagem); imagem.Save(filename); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(filename); var imageClone = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); cor.Red = new AForge.IntRange(150, 255); cor.Green = new AForge.IntRange(150, 255); cor.Blue = new AForge.IntRange(128, 255); Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering(); Closing close = new Closing(); GaussianSharpen gs = new GaussianSharpen(); ContrastCorrection cc = new ContrastCorrection(); FiltersSequence seq = new FiltersSequence(cc, gs, inverter, cor, bc, cor, cc, cor, bc); var image = seq.Apply(imageClone); var byteImg = ImageToByte(image); var result = OCR(ByteToImage(byteImg)); return(result); }
private void dİlateToolStripMenuItem_Click(object sender, EventArgs e) { Dilatation filter = new Dilatation(); ApplyFilter(filter); }
private void resultGestureToolStripMenuItem_Click(object sender, EventArgs e) { int dir; int no; List <string> filedir = new List <string>(Directory.GetDirectories(path)); for (dir = 0, no = 0; (dir < filedir.Count && no <= 26); dir++, no++) { string[] filePaths = Directory.GetFiles(filedir[dir].ToString()); List <Bitmap> y = new List <Bitmap>(); foreach (var iI in filePaths) { Bitmap Image = new Bitmap(iI); y.Add(Image); } foreach (Bitmap img in y) { pictureBox1.Image = img; srcImg = img; dstImg = img; Bitmap skin = new Bitmap(pictureBox1.Image); var rect = new Rectangle(0, 0, skin.Width, skin.Height); var data = skin.LockBits(rect, ImageLockMode.ReadWrite, skin.PixelFormat); var depth = Bitmap.GetPixelFormatSize(data.PixelFormat) / 8; //bytes per pixel var buffer = new byte[data.Width * data.Height * depth]; //copy pixels to buffer Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); System.Threading.Tasks.Parallel.Invoke( () => { //upper-left Process(buffer, 0, 0, data.Width / 2, data.Height / 2, data.Width, depth); }, () => { //upper-right Process(buffer, data.Width / 2, 0, data.Width, data.Height / 2, data.Width, depth); }, () => { //lower-left Process(buffer, 0, data.Height / 2, data.Width / 2, data.Height, data.Width, depth); }, () => { //lower-right Process(buffer, data.Width / 2, data.Height / 2, data.Width, data.Height, data.Width, depth); } ); //Copy the buffer back to image Marshal.Copy(buffer, 0, data.Scan0, buffer.Length); skin.UnlockBits(data); pictureBox2.Image = skin; Bitmap src = new Bitmap(pictureBox1.Image); Bitmap res = new Bitmap(pictureBox2.Image); src = resize(src, new Size(200, 200)); res = resize(res, new Size(200, 200)); pictureBox1.Image = src; pictureBox2.Image = res; GrayscaleBT709 grayoject = new GrayscaleBT709(); pictureBox2.Image = grayoject.Apply((Bitmap)pictureBox2.Image); Dilatation filter = new Dilatation(); // apply the filter pictureBox2.Image = filter.Apply((Bitmap)pictureBox2.Image); ExtractBiggestBlob filter1 = new ExtractBiggestBlob(); pictureBox2.Image = filter.Apply((Bitmap)pictureBox2.Image); blob = filter1.BlobPosition; Bitmap src1 = new Bitmap(pictureBox1.Image); Bitmap res1 = new Bitmap(pictureBox2.Image); Bitmap newBmp = new Bitmap(src1.Width, res1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //Threshold t = new Threshold(); //pictureBox2.Image = t.Apply((Bitmap)pictureBox2.Image); for (int i = 0; i < res1.Width; i++) { for (int j = 0; j < res1.Height; j++) { System.Drawing.Color srcColor = src1.GetPixel(i + blob.X, j + blob.Y); System.Drawing.Color dstColor = res1.GetPixel(i, j); if (!(dstColor.R >= 0 && dstColor.R <= 10 && dstColor.G >= 0 && dstColor.G <= 10 && dstColor.B >= 0 && dstColor.B <= 10)) { newBmp.SetPixel(i, j, srcColor); } else { newBmp.SetPixel(i, j, Color.Black); } } } res1 = newBmp; pictureBox2.Image = newBmp; List <double> edgeCount = new List <double>(); List <double> ratio = new List <double>(); int pixelCount = 0; Bitmap Destimg = new Bitmap(pictureBox2.Image); GrayscaleBT709 go = new GrayscaleBT709(); pictureBox2.Image = go.Apply((Bitmap)pictureBox2.Image); Destimg = go.Apply(Destimg); CannyEdgeDetector filter2 = new CannyEdgeDetector(0, 0, 1.4); pictureBox2.Image = filter2.Apply((Bitmap)pictureBox2.Image); Destimg = filter2.Apply(Destimg); var imgarray = new System.Drawing.Image[36]; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { pixelCount++; var index = i * 6 + j; imgarray[index] = new Bitmap(40, 40); var graphics = Graphics.FromImage(imgarray[index]); graphics.DrawImage(Destimg, new Rectangle(0, 0, 40, 40), new Rectangle(i * 40, j * 40, 40, 40), GraphicsUnit.Pixel); graphics.Dispose(); } } for (int n = 0; n < 36; n++) { int counter = 0; Bitmap bufferImage = new Bitmap(imgarray[n]); for (int i = 0; i < 40; i++) { for (int j = 0; j < 40; j++) { System.Drawing.Color hoefColor = bufferImage.GetPixel(i, j); //if(hoefColor.R<=255 && hoefColor.R>=230 && hoefColor.G <= 255 && hoefColor.G >= 230 && hoefColor.B <= 255 && hoefColor.B >= 230) if (!(hoefColor.R == 0 && hoefColor.G == 0 && hoefColor.B == 0)) { counter++; } } } edgeCount.Add(counter); } double total = edgeCount.Sum(); foreach (double x in edgeCount) { var a = (float)x / total; ratio.Add(a); } FileStream fs = new FileStream(@"D:\AI.txt", FileMode.Append, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.Write((no) + " "); for (int i = 0; i < ratio.Count; ++i) { sw.Write(i + ":" + ratio[i].ToString() + " "); } sw.WriteLine(); sw.Close(); fs.Close(); Problem train = Problem.Read(@"D:\AI.txt"); Problem test = Problem.Read(@"D:\test.txt"); Parameter parameters = new Parameter(); double C; double Gamma; parameters.C = 32; parameters.Gamma = 8; Model model = Training.Train(train, parameters); Prediction.Predict(test, @"D:\result.txt", model, false); } } }
private void Button_HSV_Filter_Click(object sender, EventArgs e) { Bitmap image = new Bitmap(pictureBox1.Image); HSLFiltering filterHsl = new HSLFiltering(); Mean filterMean = new Mean(); filterHsl.Luminance = new AForge.Range(0.1f, 1); filterHsl.UpdateHue = false; filterHsl.UpdateSaturation = false; filterHsl.UpdateLuminance = true; filterHsl.ApplyInPlace(image); filterMean.ApplyInPlace(image); SISThreshold filterThresold = new SISThreshold(); GrayscaleBT709 filterGray = new GrayscaleBT709(); image = filterGray.Apply(image); Bitmap clone = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format8bppIndexed); filterThresold.ApplyInPlace(clone); image = clone; Bitmap clone2normal = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format32bppRgb); image = clone2normal; BlobCounter bc = new BlobCounter(); bc.FilterBlobs = true; bc.MinWidth = 5; bc.MinHeight = 5; bc.ProcessImage(image); Blob[] blobs = bc.GetObjects(image, false); var rectanglesToClear = from blob in blobs select blob.Rectangle; using (var gfx = Graphics.FromImage(image)) { foreach (var rect in rectanglesToClear) { if (rect.Height < 20 && rect.Width < 20) { gfx.FillRectangle(Brushes.White, rect); } } gfx.Flush(); } Dilatation filterDilation = new Dilatation(); image = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format48bppRgb); filterDilation.ApplyInPlace(image); filterDilation.ApplyInPlace(image); Erosion filterErosion = new Erosion(); filterErosion.ApplyInPlace(image); pictureBox2.Image = image; }
private string reconhecerCaptcha(Image img) { Bitmap bmp = new Bitmap(pictureBox1.Image); int threshold_value = 220; //0-255 Image <Gray, Byte> img2 = new Image <Gray, byte>(bmp); pictureBox2.Image = img2.ToBitmap(); img2 = img2.ThresholdBinary(new Gray(threshold_value), new Gray(255)); pictureBox2.Image = img2.ToBitmap(); Bitmap imagem = new Bitmap(pictureBox2.Image); imagem = imagem.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Erosion erosion = new Erosion(); Dilatation dilatation = new Dilatation(); Invert inverter = new Invert(); ColorFiltering cor = new ColorFiltering(); cor.Blue = new AForge.IntRange(200, 255); cor.Red = new AForge.IntRange(200, 255); cor.Green = new AForge.IntRange(200, 255); // Opening open = new Opening(); BlobsFiltering bc = new BlobsFiltering(); Closing close = new Closing(); // GaussianSharpen gs = new GaussianSharpen(); ContrastCorrection cc = new ContrastCorrection(); bc.MinHeight = 10; FiltersSequence seq = new FiltersSequence(inverter, inverter, bc, inverter, cc, cor, bc, inverter); var result = ScaleByPercent(imagem, 200); pictureBox2.Image = seq.Apply(result); Bitmap bmp2 = new Bitmap(pictureBox2.Image); var palette = new Dictionary <Color, int>(); for (var x = 0; x < bmp2.Width; x++) { for (var y = 0; y < bmp2.Height; y++) { var clr = bmp2.GetPixel(x, y); if (!palette.ContainsKey(clr)) { palette.Add(clr, 1); } else { palette[clr] = palette[clr] + 1; } } } var i = 0; foreach (var c in palette) { if (c.Value > 30) { var temp = this.ClearBitmap(bmp2, c.Key); if (i == 0) { temp2 = new Bitmap(temp); } temp.Save(String.Format("D:\\mask-{0}.bmp", i)); i++; } } string reconhecido = OCR((Bitmap)pictureBox2.Image); return(reconhecido); }
public DilatationFilter() { dilatation = new Dilatation(); }
public Binarize(UISettings ui, FileData file) : base(ui, file) { try { Invert AFinvert = new Invert(); switch (ui.ThresholdIndex) // threshold method selection "Global mean" / "Local adaptive" { case 0: // Global if (ui.ThreshGlobalIsAbsolute) // use absolute { Threshold AFglobalbinary = new Threshold(ui.ThreshGlobalAbsolute); UnmanagedBlackWhite = AFglobalbinary.Apply(UnmanagedGray); } else // use relative { ImageStatistics stats = new ImageStatistics(UnmanagedGray, AFinvert.Apply(UnmanagedExclude)); Threshold AFglobalbinary = new Threshold(stats.Gray.Center2QuantileValue(1.0d * ui.ThreshGlobalRelative / 255.0d)); UnmanagedBlackWhite = AFglobalbinary.Apply(UnmanagedGray); } break; case 1: // Local BradleyLocalThresholdingX AFlocalbinary = new BradleyLocalThresholdingX() { PixelBrightnessDifferenceLimit = ui.ThreshLocalBrightnessDifference, WindowSize = ui.ThreshLocalWindowSize, UpperLimit = 250 }; UnmanagedBlackWhite = AFlocalbinary.Apply(UnmanagedGray); break; } if (ui.FillHoleAirspaceSwitch && ui.FillHoleAirspace != 0) // fill holes of airspaces { FillHoles AFfillinair = new FillHoles() { CoupledSizeFiltering = true, MaxHoleHeight = ui.FillHoleAirspace, MaxHoleWidth = ui.FillHoleAirspace }; //FillHolesArea AFfillinair=new FillHolesArea() { MaxHoleArea=ui.FillHoleAirspace }; AFfillinair.ApplyInPlace(UnmanagedBlackWhite); } UnmanagedBlackWhite = AFinvert.Apply(UnmanagedBlackWhite); if (ui.FillHoleTissueSwitch && ui.FillHoleTissue != 0) // fill holes of tissue { FillHoles AFfillintissue = new FillHoles() { CoupledSizeFiltering = true, MaxHoleHeight = ui.FillHoleTissue, MaxHoleWidth = ui.FillHoleTissue }; //FillHolesArea AFfillintissue=new FillHolesArea() { MaxHoleArea=ui.FillHoleTissue }; AFfillintissue.ApplyInPlace(UnmanagedBlackWhite); } if (ui.MorphoDilateSwitch && ui.MorphoDilate != 0) // Morphological Dilate { int n = (Math.Max(ui.MorphoDilate, 0) * 2 + 1); short[,] morphmatrix = new short[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { morphmatrix[i, j] = 1; } } Dilatation AFdilate = new Dilatation(morphmatrix); AFdilate.ApplyInPlace(UnmanagedBlackWhite); } if (ui.MorphoErodeSwitch && ui.MorphoErode != 0) // Morphological Erode { int n = (Math.Max(ui.MorphoErode, 0) * 2 + 1); short[,] morphmatrix = new short[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { morphmatrix[i, j] = 1; } } Erosion AFerode = new Erosion(morphmatrix); AFerode.ApplyInPlace(UnmanagedBlackWhite); } if (ui.ExcludeColorSwitch) { NonParen_SumArea = ui.um2px2 * UnmanagedExclude.NonBlackArea(); } if (ui.BlobMinSwitch) { Low_Threshold = Math.Pow(10.0d, ui.BlobMin) - 1; } else { Low_Threshold = 0.0d; } if (ui.BlobMaxSwitch) { High_Threshold = Math.Pow(10.0d, ui.BlobMax) - 1; } else { High_Threshold = int.MaxValue; } if (ui.BlobMinSwitch || ui.BlobMaxSwitch) { Merge AFmerge1 = new Merge(UnmanagedExclude); ExcludeSize AFexcsize = new ExcludeSize() { Low = (int)Math.Round(Low_Threshold / ui.um2px2), High = (int)Math.Round(Math.Min(int.MaxValue, High_Threshold / ui.um2px2)) }; Merge AFmerge2 = new Merge(AFexcsize.Apply(AFinvert.Apply(AFmerge1.Apply(UnmanagedBlackWhite)))); AFmerge2.ApplyInPlace(UnmanagedExclude); Low_SumArea = ui.um2px2 * AFexcsize.LowCount; High_SumArea = ui.um2px2 * AFexcsize.HighCount; } } catch { throw new Exception("Error Occured During Binarization"); } }
public override Bitmap ApplyFilter(List <Bitmap> bitmap) { Dilatation filter = new Dilatation(Matrix); return(filter.Apply(bitmap[0])); }
private void button2_Click(object sender, EventArgs e) { if (FinalFrame.IsRunning == true) { pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone(); } Bitmap InputImage = (Bitmap)pictureBox2.Image; Rectangle Tile = new Rectangle(0, 0, InputImage.Width, InputImage.Height); BitmapData bitmapdata = InputImage.LockBits(Tile, ImageLockMode.ReadWrite, InputImage.PixelFormat); int formatsize = Bitmap.GetPixelFormatSize(bitmapdata.PixelFormat) / 8; var tempreg = new byte[bitmapdata.Width * bitmapdata.Height * formatsize]; Marshal.Copy(bitmapdata.Scan0, tempreg, 0, tempreg.Length); System.Threading.Tasks.Parallel.Invoke( () => { multithread1(tempreg, 0, 0, bitmapdata.Width / 2, bitmapdata.Height / 2, bitmapdata.Width, formatsize); }, () => { multithread1(tempreg, 0, bitmapdata.Height / 2, bitmapdata.Width / 2, bitmapdata.Height, bitmapdata.Width, formatsize); }, () => { multithread1(tempreg, bitmapdata.Width / 2, 0, bitmapdata.Width, bitmapdata.Height / 2, bitmapdata.Width, formatsize); }, () => { multithread1(tempreg, bitmapdata.Width / 2, bitmapdata.Height / 2, bitmapdata.Width, bitmapdata.Height, bitmapdata.Width, formatsize); } ); Marshal.Copy(tempreg, 0, bitmapdata.Scan0, tempreg.Length); InputImage.UnlockBits(bitmapdata); Grayscale grayfilter = new Grayscale(0.2125, 0.7154, 0.0721);//GrayscaleBT709 grayfilter=new GrayscaleBT709(); Dilatation dilatefilter = new Dilatation(); Erosion erodefilter = new Erosion(); InputImage = grayfilter.Apply((Bitmap)InputImage); InputImage = dilatefilter.Apply((Bitmap)InputImage); InputImage = erodefilter.Apply((Bitmap)InputImage); //Opening openfilter = new Opening(); //InputImage=openfilter.Apply((Bitmap)InputImage); //Closing closefilter = new Closing(); //InputImage=closefilter.Apply((Bitmap)InputImage); ExtractBiggestBlob blob = new ExtractBiggestBlob(); InputImage = blob.Apply(InputImage); int cordx = blob.BlobPosition.X; int cordy = blob.BlobPosition.Y; Bitmap source = new Bitmap(pictureBox1.Image); Bitmap destination = new Bitmap(InputImage); var sourcerectangle = new Rectangle(0, 0, source.Width, source.Height); var destinationrectangle = new Rectangle(0, 0, destination.Width, destination.Height); var sourcedata = source.LockBits(sourcerectangle, ImageLockMode.ReadWrite, source.PixelFormat); var destinationdata = destination.LockBits(destinationrectangle, ImageLockMode.ReadWrite, destination.PixelFormat); var sourcedepth = Bitmap.GetPixelFormatSize(sourcedata.PixelFormat) / 8; var destinationdepth = Bitmap.GetPixelFormatSize(destinationdata.PixelFormat) / 8; var source1 = new byte[sourcedata.Width * sourcedata.Height * sourcedepth]; var destination1 = new byte[destinationdata.Width * destinationdata.Height * destinationdepth]; Marshal.Copy(sourcedata.Scan0, source1, 0, source1.Length); Marshal.Copy(destinationdata.Scan0, destination1, 0, destination1.Length); System.Threading.Tasks.Parallel.Invoke( () => { multithread2(source1, destination1, cordx, 0, cordy, 0, cordx + (destinationdata.Width / 2), destinationdata.Width / 2, cordy + (destinationdata.Height / 2), destinationdata.Height / 2, sourcedata.Width, destinationdata.Width, sourcedepth, destinationdepth); }, () => { multithread2(source1, destination1, cordx + (destinationdata.Width / 2), destinationdata.Width / 2, cordy, 0, cordx + (destinationdata.Width), destinationdata.Width, cordy + (destinationdata.Height / 2), destinationdata.Height / 2, sourcedata.Width, destinationdata.Width, sourcedepth, destinationdepth); }, () => { multithread2(source1, destination1, cordx, 0, cordy + (destinationdata.Height / 2), destinationdata.Height / 2, cordx + (destinationdata.Width / 2), destinationdata.Width / 2, cordy + (destinationdata.Height), destinationdata.Height, sourcedata.Width, destinationdata.Width, sourcedepth, destinationdepth); }, () => { multithread2(source1, destination1, cordx + (destinationdata.Width / 2), destinationdata.Width / 2, cordy + (destinationdata.Height / 2), destinationdata.Height / 2, cordx + (destinationdata.Width), destinationdata.Width, cordy + (destinationdata.Height), destinationdata.Height, sourcedata.Width, destinationdata.Width, sourcedepth, destinationdepth); } ); Marshal.Copy(source1, 0, sourcedata.Scan0, source1.Length); Marshal.Copy(destination1, 0, destinationdata.Scan0, destination1.Length); source.UnlockBits(sourcedata); destination.UnlockBits(destinationdata); InputImage = destination; InputImage = grayfilter.Apply((Bitmap)InputImage); CannyEdgeDetector edgesoutline = new CannyEdgeDetector(); InputImage = edgesoutline.Apply(InputImage); pictureBox2.Image = InputImage; Bitmap blocks = new Bitmap(InputImage); int[] numofedges = new int[100]; double[] normalized = new double[400]; String alphabet = null; int total = 0; int sq = 1; for (int p = 1; p <= 8; p++) { for (int q = 1; q <= 8; q++) { for (int x = (p - 1) * blocks.Width / 8; x < (p * blocks.Width / 8); x++) { for (int y = (q - 1) * blocks.Height / 8; y < (q * blocks.Height / 8); y++) { Color colorPixel = blocks.GetPixel(x, y); int r = colorPixel.R; int g = colorPixel.G; int b = colorPixel.B; if (r != 0 & g != 0 & b != 0) { numofedges[sq]++; } } } sq++; } } for (sq = 1; sq <= 64; sq++) { total = total + numofedges[sq]; } for (sq = 1; sq <= 64; sq++) { normalized[sq] = (double)numofedges[sq] / total; alphabet = alphabet + " " + sq.ToString() + ":" + normalized[sq].ToString(); } File.WriteAllText(@"datasets\testalpha.txt", label.ToString() + alphabet + Environment.NewLine); Problem train = Problem.Read(@"datasets\trainedset.txt"); Problem test = Problem.Read(@"datasets\testalpha.txt"); Parameter parameter = new Parameter(); parameter.C = 32; parameter.Gamma = 8; Model model = Training.Train(train, parameter); Prediction.Predict(test, @"datasets\result.txt", model, false); int value = Convert.ToInt32(File.ReadAllText(@"datasets\result.txt")); String res = null; res = res + (char)(value + 65); label1.Text = res; }
private void button2_Click(object sender, EventArgs e) { pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone(); Bitmap src = new Bitmap(pictureBox2.Image); Bitmap res = new Bitmap(pictureBox2.Image); SaveFileDialog saveDialog = new SaveFileDialog(); src = resize(src, new Size(200, 200)); res = resize(res, new Size(200, 200)); pictureBox2.Image = src; srcImg = src; pictureBox2.Image = res; Bitmap sampleImage = new Bitmap(pictureBox2.Image); var rect = new Rectangle(0, 0, sampleImage.Width, sampleImage.Height); var data = sampleImage.LockBits(rect, ImageLockMode.ReadWrite, sampleImage.PixelFormat); var depth = Bitmap.GetPixelFormatSize(data.PixelFormat) / 8; //bytes per pixel var buffer = new byte[data.Width * data.Height * depth]; //copy pixels to buffer Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); System.Threading.Tasks.Parallel.Invoke( () => { //upper-left Process(buffer, 0, 0, data.Width / 2, data.Height / 2, data.Width, depth); }, () => { //upper-right Process(buffer, data.Width / 2, 0, data.Width, data.Height / 2, data.Width, depth); }, () => { //lower-left Process(buffer, 0, data.Height / 2, data.Width / 2, data.Height, data.Width, depth); }, () => { //lower-right Process(buffer, data.Width / 2, data.Height / 2, data.Width, data.Height, data.Width, depth); } ); //Copy the buffer back to image Marshal.Copy(buffer, 0, data.Scan0, buffer.Length); sampleImage.UnlockBits(data); pictureBox2.Image = sampleImage; dstImg = sampleImage; void Process(byte[] buffer1, int x, int y, int endx, int endy, int width, int depth1) { for (int i = x; i < endx; i++) { for (int j = y; j < endy; j++) { var offset = ((j * width) + i) * depth; var B = buffer[offset + 0]; var G = buffer[offset + 1]; var R = buffer[offset + 2]; var a = Math.Max(R, Math.Max(B, G)); var b = Math.Min(R, Math.Min(B, G)); if (!(((R > 95) && (G > 40) && (B > 20) && ((a - b) > 15) && (Math.Abs(R - G) > 15) && (R > G) && (R > B)) || ((R > 220) && (G > 210) && (B > 170) && ((a - b) > 15) && (Math.Abs(R - G) > 15) && (R > G) && (G > B)))) { buffer[offset + 0] = buffer[offset + 1] = buffer[offset + 2] = 0; } else { buffer[offset + 0] = buffer[offset + 1] = buffer[offset + 2] = 255; } } } } //Graysacle GrayscaleBT709 filter = new GrayscaleBT709(); pictureBox2.Image = filter.Apply((Bitmap)pictureBox2.Image); dstImg = filter.Apply(dstImg); //Dilatation try { Dilatation filter1 = new Dilatation(); pictureBox2.Image = filter1.Apply((Bitmap)pictureBox2.Image); dstImg = filter1.Apply(dstImg); } catch (Exception) { System.Windows.Forms.MessageBox.Show("Apply Grayscale"); } //Biggest Blob Extraction ExtractBiggestBlob filter2 = new ExtractBiggestBlob(); pictureBox2.Image = filter2.Apply((Bitmap)pictureBox2.Image); dstImg = filter2.Apply(dstImg); blob = filter2.BlobPosition; Bitmap newBmp = new Bitmap(dstImg.Width, dstImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics gfx = Graphics.FromImage(newBmp)) { gfx.DrawImage(dstImg, 0, 0); } //newBmp = dstImg; for (int i = 0; i < dstImg.Width; i++) { for (int j = 0; j < dstImg.Height; j++) { System.Drawing.Color srcColor = srcImg.GetPixel(i + blob.X, j + blob.Y); System.Drawing.Color dstColor = dstImg.GetPixel(i, j); if (!(dstColor.R >= 0 && dstColor.R <= 10 && dstColor.G >= 0 && dstColor.G <= 10 && dstColor.B >= 0 && dstColor.B <= 10)) { newBmp.SetPixel(i, j, srcColor); } } } dstImg = newBmp; pictureBox2.Image = newBmp; List <double> edgeCount = new List <double>(); List <double> ratio = new List <double>(); int pixelCount = 0; Bitmap hoefImage = new Bitmap(pictureBox2.Image); GrayscaleBT709 grayFilter = new GrayscaleBT709(); hoefImage = grayFilter.Apply((Bitmap)pictureBox2.Image); CannyEdgeDetector cannyFilter = new CannyEdgeDetector(0, 0, 1.4); hoefImage = cannyFilter.Apply(hoefImage); pictureBox2.Image = hoefImage; var imgarray = new System.Drawing.Image[36]; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { pixelCount++; var index = i * 6 + j; imgarray[index] = new Bitmap(40, 40); var graphics = Graphics.FromImage(imgarray[index]); graphics.DrawImage(hoefImage, new Rectangle(0, 0, 40, 40), new Rectangle(i * 40, j * 40, 40, 40), GraphicsUnit.Pixel); graphics.Dispose(); } } for (int n = 0; n < 36; n++) { int counter = 0; Bitmap bufferImage = new Bitmap(imgarray[n]); for (int i = 0; i < 40; i++) { for (int j = 0; j < 40; j++) { System.Drawing.Color hoefColor = bufferImage.GetPixel(i, j); if (!(hoefColor.R == 0 && hoefColor.G == 0 && hoefColor.B == 0)) { counter++; } } } edgeCount.Add(counter); } double Total = edgeCount.Sum(); foreach (double x in edgeCount) { var a = x / Total; ratio.Add(a); } FileStream fs = new FileStream(@"E:\test.txt", FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); int no = 0; sw.Write((++no) + " "); for (int i = 0; i < ratio.Count; ++i) { sw.Write(i + ":" + ratio[i].ToString() + " "); } sw.WriteLine(); sw.Close(); fs.Close(); //Support Vector Machine Problem train = Problem.Read(@"E:\AI.txt"); Problem test = Problem.Read(@"E:\test.txt"); Parameter parameters = new Parameter(); double C; double Gamma; parameters.C = 32; parameters.Gamma = 8; Model model = Training.Train(train, parameters); Prediction.Predict(test, @"E:\result.txt", model, false); FileStream fs1 = new FileStream(@"E:\result.txt", FileMode.Open, FileAccess.Read); StreamReader sw1 = new StreamReader(fs1); string w = sw1.ReadLine(); if (w == "1") { MessageBox.Show("A"); } else if (w == "2") { MessageBox.Show("B"); } else if (w == "3") { MessageBox.Show("C"); } else if (w == "4") { MessageBox.Show("D"); } else if (w == "5") { MessageBox.Show("E"); } else if (w == "6") { MessageBox.Show("F"); } else if (w == "7") { MessageBox.Show("G"); } else if (w == "8") { MessageBox.Show("H"); } else if (w == "9") { MessageBox.Show("I"); } else if (w == "10") { MessageBox.Show("J"); } else if (w == "11") { MessageBox.Show("K"); } //else { MessageBox.Show("L"); } }