/// <summary> /// Method for creating a nice red highlighting rectangle above the cell under cursor /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { try { Point cellCoords = BitmapCoordsToCellCoords(PicBoxCoordsToBitmapCoords(e.Location)); if (cellCoords == m_lastCellCoords) { return; } var cellRectangle = OcrReader.GetCellCoordinates(m_hLines, m_vLines, cellCoords.X, cellCoords.Y).ToRectangle(); if (!m_lastCellRectangle.IsEmpty) { var b = pictureBox1.Image as Bitmap; for (int y = m_lastCellRectangle.Top; y < m_lastCellRectangle.Bottom; y++) { for (int x = m_lastCellRectangle.Left; x < m_lastCellRectangle.Right; x++) { Debug.Assert(b != null, "b != null"); b.SetPixel(x, y, m_picture.GetPixel(x, y)); } } } m_lastCellRectangle = cellRectangle; m_lastCellCoords = cellCoords; Graphics g = Graphics.FromImage(pictureBox1.Image); g.FillRectangle(new SolidBrush(Color.FromArgb(50, 255, 0, 0)), cellRectangle); pictureBox1.Refresh(); } catch {} }
private void butBrowse_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog { CheckFileExists = true, Multiselect = false, InitialDirectory = Environment.CurrentDirectory, Filter = "Image files (*.png, *.jpg, *.bmp, *.jpeg)|*.png;*.jpg;*.bmp;*.jpeg" }; if (ofd.ShowDialog() == DialogResult.OK) { try { progressBar1.Show(); pictBox.Image = Image.FromFile(ofd.FileName); pictBox.Show(); Application.DoEvents(); int[,] table = OcrReader.ParsePicture(ofd.FileName, pictBox); InitDataTable(dataTable1); dataTable1.Show(); IntArrayToDataTable(table, dataTable1); progressBar1.Hide(); lblCheckOCR.Show(); } catch (FileNotFoundException) { progressBar1.Hide(); dataTable1.Hide(); pictBox.Hide(); MessageBox.Show( "The file with neural network does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch { progressBar1.Hide(); dataTable1.Hide(); pictBox.Hide(); String msg = "An error while parsing image occured. \nPlease make sure that the image is correct. \nCropping image might also help."; if (Properties.Settings.Default.Method == 1 && Properties.Settings.Default.NeuralNetPath != "Resources") { msg = "An error while parsing image occured. \nPlease make sure that the image and the neural network file is correct. \nCropping image might also help."; } MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Returns a list of filled cells. /// Lists through every cell and if at least 1% is non-white, the cell is considered to be non-empty. /// </summary> /// <returns></returns> private List <MyRectangle> FindFilledCells() { Color penColor = m_picture.GetPixel(0, 0); // Get color of pen using which the numbers are written List <MyRectangle> result = new List <MyRectangle>(); for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { Rectangle cell = OcrReader.GetCellCoordinates(m_hLines, m_vLines, x, y).ToRectangle(); int numOfPixelsFilled = 0; for (int i = cell.Left; i < cell.Right; i++) { for (int j = cell.Top; j < cell.Bottom; j++) { if (m_picture.GetPixel(i, j).Equals(penColor)) { numOfPixelsFilled++; } } } // If at least 1% is filled if (numOfPixelsFilled > NUM_OF_PERCENT_FILLED * (cell.Width * cell.Height)) { // Test of correctness /*Graphics g = Graphics.FromImage(pictureBox1.Image); * g.DrawRectangle(new Pen(Color.Red, 3), cell); * pictureBox1.Refresh(); * Application.DoEvents();*/ result.Add(new MyRectangle(cell, x, y)); } } } //pictureBox1.Refresh(); return(result); }