private static String OcrImage(Tesseract ocr, Mat image, OCRMode mode, Mat imageColor) { Bgr drawCharColor = new Bgr(Color.Red); if (image.NumberOfChannels == 1) { CvInvoke.CvtColor(image, imageColor, ColorConversion.Gray2Bgr); } else { image.CopyTo(imageColor); } if (mode == OCRMode.FullPage) { ocr.SetImage(imageColor); if (ocr.Recognize() != 0) { throw new Exception("Failed to recognizer image"); } Tesseract.Character[] characters = ocr.GetCharacters(); if (characters.Length == 0) { Mat imgGrey = new Mat(); CvInvoke.CvtColor(image, imgGrey, ColorConversion.Bgr2Gray); Mat imgThresholded = new Mat(); CvInvoke.Threshold(imgGrey, imgThresholded, 65, 255, ThresholdType.Binary); ocr.SetImage(imgThresholded); characters = ocr.GetCharacters(); imageColor = imgThresholded; if (characters.Length == 0) { CvInvoke.Threshold(image, imgThresholded, 190, 255, ThresholdType.Binary); ocr.SetImage(imgThresholded); characters = ocr.GetCharacters(); imageColor = imgThresholded; } } foreach (Tesseract.Character c in characters) { CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar); } return(ocr.GetUTF8Text()); } else { bool checkInvert = true; Rectangle[] regions; using ( ERFilterNM1 er1 = new ERFilterNM1("trained_classifierNM1.xml", 8, 0.00025f, 0.13f, 0.4f, true, 0.1f)) using (ERFilterNM2 er2 = new ERFilterNM2("trained_classifierNM2.xml", 0.3f)) { int channelCount = image.NumberOfChannels; UMat[] channels = new UMat[checkInvert ? channelCount * 2 : channelCount]; for (int i = 0; i < channelCount; i++) { UMat c = new UMat(); CvInvoke.ExtractChannel(image, c, i); channels[i] = c; } if (checkInvert) { for (int i = 0; i < channelCount; i++) { UMat c = new UMat(); CvInvoke.BitwiseNot(channels[i], c); channels[i + channelCount] = c; } } VectorOfERStat[] regionVecs = new VectorOfERStat[channels.Length]; for (int i = 0; i < regionVecs.Length; i++) { regionVecs[i] = new VectorOfERStat(); } try { for (int i = 0; i < channels.Length; i++) { er1.Run(channels[i], regionVecs[i]); er2.Run(channels[i], regionVecs[i]); } using (VectorOfUMat vm = new VectorOfUMat(channels)) { regions = ERFilter.ERGrouping(image, vm, regionVecs, ERFilter.GroupingMethod.OrientationHoriz, "trained_classifier_erGrouping.xml", 0.5f); } } finally { foreach (UMat tmp in channels) { if (tmp != null) { tmp.Dispose(); } } foreach (VectorOfERStat tmp in regionVecs) { if (tmp != null) { tmp.Dispose(); } } } Rectangle imageRegion = new Rectangle(Point.Empty, imageColor.Size); for (int i = 0; i < regions.Length; i++) { Rectangle r = ScaleRectangle(regions[i], 1.1); r.Intersect(imageRegion); regions[i] = r; } } List <Tesseract.Character> allChars = new List <Tesseract.Character>(); String allText = String.Empty; foreach (Rectangle rect in regions) { using (Mat region = new Mat(image, rect)) { ocr.SetImage(region); if (ocr.Recognize() != 0) { throw new Exception("Failed to recognize image"); } Tesseract.Character[] characters = ocr.GetCharacters(); //convert the coordinates from the local region to global for (int i = 0; i < characters.Length; i++) { Rectangle charRegion = characters[i].Region; charRegion.Offset(rect.Location); characters[i].Region = charRegion; } allChars.AddRange(characters); allText += ocr.GetUTF8Text() + Environment.NewLine; } } Bgr drawRegionColor = new Bgr(Color.Red); foreach (Rectangle rect in regions) { CvInvoke.Rectangle(imageColor, rect, drawRegionColor.MCvScalar); } foreach (Tesseract.Character c in allChars) { CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar); } return(allText); } }
public void TestERFilter() { CvInvoke.SanityCheck(); bool checkInvert = true; using (Image<Bgr, Byte> image = EmguAssert.LoadImage<Bgr, Byte>("scenetext01.jpg")) using (ERFilterNM1 er1 = new ERFilterNM1(EmguAssert.GetFile("trained_classifierNM1.xml"), 8, 0.00025f, 0.13f, 0.4f, true, 0.1f)) using (ERFilterNM2 er2 = new ERFilterNM2(EmguAssert.GetFile("trained_classifierNM2.xml"), 0.3f)) { //using (Image<Gray, Byte> mask = new Image<Gray,byte>(image.Size.Width + 2, image.Size.Height + 2)) int channelCount = image.NumberOfChannels; UMat[] channels = new UMat[checkInvert ? channelCount * 2 : channelCount]; for (int i = 0; i < channelCount; i++) { UMat c = new UMat(); CvInvoke.ExtractChannel(image.Mat, c, i); channels[i] = c; } if (checkInvert) { for (int i = 0; i < channelCount; i++) { UMat c = new UMat(); CvInvoke.BitwiseNot(channels[i], c); channels[i + channelCount] = c; } } VectorOfERStat[] regionVecs = new VectorOfERStat[channels.Length]; for (int i = 0; i < regionVecs.Length; i++) regionVecs[i] = new VectorOfERStat(); /* for (int i = 0; i < channels.Length; i++) { Emgu.CV.UI.ImageViewer.Show(channels[i]); }*/ try { for (int i = 0; i < channels.Length; i++) { er1.Run(channels[i], regionVecs[i]); er2.Run(channels[i], regionVecs[i]); } using (VectorOfUMat vm = new VectorOfUMat(channels)) { Rectangle[] regions = ERFilter.ERGrouping(image, vm, regionVecs, ERFilter.GroupingMethod.OrientationHoriz, EmguAssert.GetFile("trained_classifier_erGrouping.xml"), 0.5f); foreach (Rectangle rect in regions) image.Draw(rect, new Bgr(0, 0, 255), 2); } } finally { foreach (UMat tmp in channels) if (tmp != null) tmp.Dispose(); foreach (VectorOfERStat tmp in regionVecs) if (tmp != null) tmp.Dispose(); } //Emgu.CV.UI.ImageViewer.Show(image); } }
private void loadImageButton_Click(object sender, EventArgs e) { if (openImageFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { fileNameTextBox.Text = openImageFileDialog.FileName; imageBox1.Image = null; ocrTextBox.Text = String.Empty; hocrTextBox.Text = String.Empty; Bgr drawCharColor = new Bgr(Color.Blue); try { Mat image = new Mat(openImageFileDialog.FileName, ImreadModes.AnyColor); Mat imageColor = new Mat(); if (image.NumberOfChannels == 1) CvInvoke.CvtColor(image, imageColor, ColorConversion.Gray2Bgr); else image.CopyTo(imageColor); if (Mode == OCRMode.FullPage) { _ocr.Recognize(image); Tesseract.Character[] characters = _ocr.GetCharacters(); if (characters.Length == 0) { Mat imgGrey = new Mat(); CvInvoke.CvtColor(image, imgGrey, ColorConversion.Bgr2Gray); Mat imgThresholded = new Mat(); CvInvoke.Threshold(imgGrey, imgThresholded,65, 255, ThresholdType.Binary); _ocr.Recognize(imgThresholded); characters = _ocr.GetCharacters(); imageColor = imgThresholded; if (characters.Length == 0) { CvInvoke.Threshold(image, imgThresholded, 190, 255, ThresholdType.Binary); _ocr.Recognize(imgThresholded); characters = _ocr.GetCharacters(); imageColor = imgThresholded; } } foreach (Tesseract.Character c in characters) { CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar); } imageBox1.Image = imageColor; String text = _ocr.GetText(); ocrTextBox.Text = text; String hocrText = _ocr.GetHOCRText(); hocrTextBox.Text = hocrText; } else { bool checkInvert = true; Rectangle[] regions; using (ERFilterNM1 er1 = new ERFilterNM1("trained_classifierNM1.xml", 8, 0.00025f, 0.13f, 0.4f, true, 0.1f)) using (ERFilterNM2 er2 = new ERFilterNM2("trained_classifierNM2.xml", 0.3f)) { int channelCount = image.NumberOfChannels; UMat[] channels = new UMat[checkInvert ? channelCount * 2 : channelCount]; for (int i = 0; i < channelCount; i++) { UMat c = new UMat(); CvInvoke.ExtractChannel(image, c, i); channels[i] = c; } if (checkInvert) { for (int i = 0; i < channelCount; i++) { UMat c = new UMat(); CvInvoke.BitwiseNot(channels[i], c); channels[i + channelCount] = c; } } VectorOfERStat[] regionVecs = new VectorOfERStat[channels.Length]; for (int i = 0; i < regionVecs.Length; i++) regionVecs[i] = new VectorOfERStat(); try { for (int i = 0; i < channels.Length; i++) { er1.Run(channels[i], regionVecs[i]); er2.Run(channels[i], regionVecs[i]); } using (VectorOfUMat vm = new VectorOfUMat(channels)) { regions = ERFilter.ERGrouping(image, vm, regionVecs, ERFilter.GroupingMethod.OrientationHoriz, "trained_classifier_erGrouping.xml", 0.5f); } } finally { foreach (UMat tmp in channels) if (tmp != null) tmp.Dispose(); foreach (VectorOfERStat tmp in regionVecs) if (tmp != null) tmp.Dispose(); } Rectangle imageRegion = new Rectangle(Point.Empty, imageColor.Size); for (int i = 0; i < regions.Length; i++) { Rectangle r = ScaleRectangle( regions[i], 1.1); r.Intersect(imageRegion); regions[i] = r; } } List<Tesseract.Character> allChars = new List<Tesseract.Character>(); String allText = String.Empty; foreach (Rectangle rect in regions) { using (Mat region = new Mat(image, rect)) { _ocr.Recognize(region); Tesseract.Character[] characters = _ocr.GetCharacters(); //convert the coordinates from the local region to global for (int i = 0; i < characters.Length; i++) { Rectangle charRegion = characters[i].Region; charRegion.Offset(rect.Location); characters[i].Region = charRegion; } allChars.AddRange(characters); allText += _ocr.GetText() + Environment.NewLine; } } Bgr drawRegionColor = new Bgr(Color.Red); foreach (Rectangle rect in regions) { CvInvoke.Rectangle(imageColor, rect, drawRegionColor.MCvScalar); } foreach (Tesseract.Character c in allChars) { CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar); } imageBox1.Image = imageColor; ocrTextBox.Text = allText; } } catch (Exception exception) { MessageBox.Show(exception.Message); } } }
private void loadImageButton_Click(object sender, EventArgs e) { if (openImageFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { fileNameTextBox.Text = openImageFileDialog.FileName; imageBox1.Image = null; ocrTextBox.Text = String.Empty; hocrTextBox.Text = String.Empty; Bgr drawCharColor = new Bgr(Color.Blue); try { Mat image = new Mat(openImageFileDialog.FileName, ImreadModes.AnyColor); Mat imageColor = new Mat(); if (image.NumberOfChannels == 1) { CvInvoke.CvtColor(image, imageColor, ColorConversion.Gray2Bgr); } else { image.CopyTo(imageColor); } if (Mode == OCRMode.FullPage) { _ocr.Recognize(image); Tesseract.Character[] characters = _ocr.GetCharacters(); if (characters.Length == 0) { Mat imgGrey = new Mat(); CvInvoke.CvtColor(image, imgGrey, ColorConversion.Bgr2Gray); Mat imgThresholded = new Mat(); CvInvoke.Threshold(imgGrey, imgThresholded, 65, 255, ThresholdType.Binary); _ocr.Recognize(imgThresholded); characters = _ocr.GetCharacters(); imageColor = imgThresholded; if (characters.Length == 0) { CvInvoke.Threshold(image, imgThresholded, 190, 255, ThresholdType.Binary); _ocr.Recognize(imgThresholded); characters = _ocr.GetCharacters(); imageColor = imgThresholded; } } foreach (Tesseract.Character c in characters) { CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar); } imageBox1.Image = imageColor; String text = _ocr.GetText(); ocrTextBox.Text = text; String hocrText = _ocr.GetHOCRText(); hocrTextBox.Text = hocrText; } else { bool checkInvert = true; Rectangle[] regions; using (ERFilterNM1 er1 = new ERFilterNM1("trained_classifierNM1.xml", 8, 0.00025f, 0.13f, 0.4f, true, 0.1f)) using (ERFilterNM2 er2 = new ERFilterNM2("trained_classifierNM2.xml", 0.3f)) { int channelCount = image.NumberOfChannels; UMat[] channels = new UMat[checkInvert ? channelCount * 2 : channelCount]; for (int i = 0; i < channelCount; i++) { UMat c = new UMat(); CvInvoke.ExtractChannel(image, c, i); channels[i] = c; } if (checkInvert) { for (int i = 0; i < channelCount; i++) { UMat c = new UMat(); CvInvoke.BitwiseNot(channels[i], c); channels[i + channelCount] = c; } } VectorOfERStat[] regionVecs = new VectorOfERStat[channels.Length]; for (int i = 0; i < regionVecs.Length; i++) { regionVecs[i] = new VectorOfERStat(); } try { for (int i = 0; i < channels.Length; i++) { er1.Run(channels[i], regionVecs[i]); er2.Run(channels[i], regionVecs[i]); } using (VectorOfUMat vm = new VectorOfUMat(channels)) { regions = ERFilter.ERGrouping(image, vm, regionVecs, ERFilter.GroupingMethod.OrientationHoriz, "trained_classifier_erGrouping.xml", 0.5f); } } finally { foreach (UMat tmp in channels) { if (tmp != null) { tmp.Dispose(); } } foreach (VectorOfERStat tmp in regionVecs) { if (tmp != null) { tmp.Dispose(); } } } Rectangle imageRegion = new Rectangle(Point.Empty, imageColor.Size); for (int i = 0; i < regions.Length; i++) { Rectangle r = ScaleRectangle(regions[i], 1.1); r.Intersect(imageRegion); regions[i] = r; } } List <Tesseract.Character> allChars = new List <Tesseract.Character>(); String allText = String.Empty; foreach (Rectangle rect in regions) { using (Mat region = new Mat(image, rect)) { _ocr.Recognize(region); Tesseract.Character[] characters = _ocr.GetCharacters(); //convert the coordinates from the local region to global for (int i = 0; i < characters.Length; i++) { Rectangle charRegion = characters[i].Region; charRegion.Offset(rect.Location); characters[i].Region = charRegion; } allChars.AddRange(characters); allText += _ocr.GetText() + Environment.NewLine; } } Bgr drawRegionColor = new Bgr(Color.Red); foreach (Rectangle rect in regions) { CvInvoke.Rectangle(imageColor, rect, drawRegionColor.MCvScalar); } foreach (Tesseract.Character c in allChars) { CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar); } imageBox1.Image = imageColor; ocrTextBox.Text = allText; } } catch (Exception exception) { MessageBox.Show(exception.Message); } } }
public string Recognize(Mat image) { Rectangle[] regions; Bgr drawCharColor = new Bgr(Color.Red); using (var er1 = new ERFilterNM1("Assets\\trained_classifierNM1.xml", 8, 0.00025f, 0.13f, 0.4f, true, 0.1f)) { using (var er2 = new ERFilterNM2("Assets\\trained_classifierNM2.xml", 0.3f)) { var channelCount = image.NumberOfChannels; var channels = new UMat[channelCount * 2]; for (int i = 0; i < channelCount; i++) { var c = new UMat(); CvInvoke.ExtractChannel(image, c, i); channels[i] = c; } for (int i = 0; i < channelCount; i++) { var c = new UMat(); CvInvoke.BitwiseNot(channels[i], c); channels[i + channelCount] = c; } var regionVecs = new VectorOfERStat[channels.Length]; for (int i = 0; i < regionVecs.Length; i++) { regionVecs[i] = new VectorOfERStat(); } try { for (int i = 0; i < channels.Length; i++) { er1.Run(channels[i], regionVecs[i]); er2.Run(channels[i], regionVecs[i]); } using (var vm = new VectorOfUMat(channels)) { regions = ERFilter.ERGrouping(image, vm, regionVecs, ERFilter.GroupingMethod.OrientationHoriz, "Assets\\trained_classifier_erGrouping.xml", 0.5f); } } finally { foreach (UMat tmp in channels) { if (tmp != null) { tmp.Dispose(); } } foreach (VectorOfERStat tmp in regionVecs) { if (tmp != null) { tmp.Dispose(); } } } Rectangle imageRegion = new Rectangle(Point.Empty, image.Size); for (int i = 0; i < regions.Length; i++) { Rectangle r = ScaleRectangle(regions[i], 1.1); r.Intersect(imageRegion); regions[i] = r; } } var allChars = new List <Tesseract.Character>(); String allText = String.Empty; foreach (Rectangle rect in regions) { using (Mat region = new Mat(image, rect)) { _ocr.SetImage(region); if (_ocr.Recognize() != 0) { return(null); } //var characters = _ocr.GetCharacters(); ////convert the coordinates from the local region to global //for (int i = 0; i < characters.Length; i++) { // Rectangle charRegion = characters[i].Region; // charRegion.Offset(rect.Location); // characters[i].Region = charRegion; //} //allChars.AddRange(characters); allText += _ocr.GetUTF8Text() + "|"; } } //Bgr drawRegionColor = new Bgr(Color.Red); //foreach (Rectangle rect in regions) { // CvInvoke.Rectangle(image, rect, drawRegionColor.MCvScalar); //} //foreach (Tesseract.Character c in allChars) { // CvInvoke.Rectangle(image, c.Region, drawCharColor.MCvScalar); //} return(allText); } }