Click here to Skip to main content
15,901,853 members
Home / Discussions / C#
   

C#

 
AnswerRe: How can I dynamically name and instantiate List<string> lists? Pin
OriginalGriff30-Jan-15 5:36
mveOriginalGriff30-Jan-15 5:36 
GeneralRe: How can I dynamically name and instantiate List<string> lists? Pin
turbosupramk32-Feb-15 3:51
turbosupramk32-Feb-15 3:51 
GeneralRe: How can I dynamically name and instantiate List<string> lists? Pin
OriginalGriff2-Feb-15 5:07
mveOriginalGriff2-Feb-15 5:07 
QuestionImprove OCR program Pin
rosy8430-Jan-15 4:55
rosy8430-Jan-15 4:55 
AnswerRe: Improve OCR program Pin
Eddy Vluggen30-Jan-15 5:16
professionalEddy Vluggen30-Jan-15 5:16 
AnswerRe: Improve OCR program Pin
rosy8430-Jan-15 5:36
rosy8430-Jan-15 5:36 
GeneralRe: Improve OCR program Pin
Eddy Vluggen30-Jan-15 7:19
professionalEddy Vluggen30-Jan-15 7:19 
GeneralRe: Improve OCR program Pin
rosy8430-Jan-15 12:26
rosy8430-Jan-15 12:26 
Thank Eddy Vluggen
firstly i have inventory ticket (all inventory tickets are same format), I also calculated Vertical Histogram and Horizoltal Histogram to detect rows and columns.
But sometime row is 20 sometime is 21 and sometime got other values. (images already deskew before process)
Could everyone help me fix it?
image link here[^]

My code in here
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;

namespace InventoryOCR
{
    struct Column
    {
        public int first;
        public int last;
    }
    struct Row
    {
        public int first;
        public int last;
        public List<Column> columns;
    }

    class AuditInvoice
    {
        public ProgressBar progressBar;
        public int[] horizontalHisto;
        public int[] verticalHisto;
        private int width, height;        
        private Bitmap inputImage;
        public List<Row> rows = new List<Row>();
        private int top, left, bottom, right;
        public int removerows = 0 ;// 

        public void LoadImage(Bitmap bmp)
        {
            if (bmp != null)
            {
               // NeuralLib.IPHelper.Preprocessing(ref bmp, true, true);
                inputImage = bmp;                
                //System.IntPtr Scan0 
                width = inputImage.Width;
                height = inputImage.Height;
                horizontalHisto = new int[height];
                verticalHisto = new int[width];
                BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                int stride01 = bmData.Stride;
                System.IntPtr Scan0 = bmData.Scan0;
                toBlackWhite(Scan0, stride01, width, height,180);
                bmp.UnlockBits(bmData);
            }
        }

        int Max(int[] histo, int length)
        {
            int max = 0;
            for (int i = 0; i < length; i++)
            {
                if (max < histo[i])
                    max = histo[i];
            }
            return max;
        }

       public void CalcVerticalHisto(int y1, int y2) 
        {
            BitmapData bmData = inputImage.LockBits(new Rectangle(0, 0, width, height),
               ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
            int stride = bmData.Stride;
            unsafe
            {
                byte* scan0 = (byte*)bmData.Scan0.ToPointer();
                for (int x = 0; x < width; x++)
                {
                    verticalHisto[x] = 0;                 
                    //for (int y = y1; y <= y2; y++)
                    for (int y = y1; y <= y2; y++)// 
                    {
                        if (scan0[y * stride + x] == 0 || scan0[(y +1)* stride + x] == 0 || scan0[(y+2) * stride + x] == 0 || scan0[(y+3) * stride + x]==0)
                            verticalHisto[x]++;
                    }
                }
            }
            inputImage.UnlockBits(bmData);
        }

       public void CalcVerticalHisto01(int y1, int y2)
       {
           BitmapData bmData = inputImage.LockBits(new Rectangle(0, 0, width, height),
              ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
           int stride = bmData.Stride;
           unsafe
           {
               byte* scan0 = (byte*)bmData.Scan0.ToPointer();
               for (int x = 0; x < width; x++)
               {
                   verticalHisto[x] = 0;                 //
                   //for (int y = y1; y <= y2; y++)
                   for (int y = y1; y <= y2; y++)// 
                   {
                       if (scan0[y * stride + x] == 0 )
                           verticalHisto[x]++;
                   }
               }
           }
           inputImage.UnlockBits(bmData);
       }
        public void CalcHorizontalHisto() // calculate horizontalHisto
        {
            BitmapData bmData = inputImage.LockBits(new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);//.Format8bppIndexed);
            int stride = bmData.Stride;
            System.IntPtr p = bmData.Scan0;
            unsafe
            {
                byte* scan0 = (byte*)p;//.ToPointer();
                for (int y = 0; y < height; y++)
                {
                    horizontalHisto[y] = 0;
                    //for (int x = 0; x < width; x++)
                    for (int x = 0; x < stride; x+=3)
                    {
                        //if (scan0[y * stride + x] == 0)
                        if (scan0[y*stride+x]==0&&scan0[y*stride+x+1]==0&&scan0[y*stride+x+2]==0)
                        {
                            horizontalHisto[y]=horizontalHisto[y]+1 ;
                        }
                            //horizontalHisto[y]++;
                    }
                }
            }
            progressBar.PerformStep();
            inputImage.UnlockBits(bmData);
        }

        void CalcRows()
        {
            Row row = new Row();
            float maxPixel = Max(horizontalHisto, height);
            bool inRow = false;
            int y = 0;
            
            //while (!inRow)
            //{
            //    if (horizontalHisto[y] > 0)
            //    {
            //        row.first = y - 1;
            //        inRow = true;
            //    }
            //    else
            //        y++;
            //}
            //while (inRow)
            //{
            //    if (horizontalHisto[y] == 0)
            //    {
            //        row.last = y;
            //        inRow = false;
            //    }
            //    else
            //        y++;
            //}
            ////---------------------------------------------------------------------------------------
            //row.columns = CalcNoAreaCols(row.first, row.last); // 
            //rows.Add(row);
            ////int threshold = (int)(maxPixel - maxPixel / 2);
            int threshold = (int)(maxPixel / 2);
            inRow = false;

            int i = 1; int j;
            //while (++y < height - 1)
            //{
            //    //if (!inRow && horizontalHisto[y] > threshold && horizontalHisto[y + 1] <= threshold)
            //    if (!inRow && horizontalHisto[y] > (maxPixel - threshold) && horizontalHisto[y + 1] < (maxPixel - threshold))
            //    {
            //        inRow = true;
            //        row.first = ++y;
            //        continue;
            //    }
            //    if (inRow && horizontalHisto[y] < (maxPixel - threshold) && horizontalHisto[y + 1] > (maxPixel - threshold))
            //    {
            //        inRow = false;
            //        row.last = y;
            //        while (horizontalHisto[++y] > threshold) ;
            //        row.columns = CalcColumns(row.first, row.last, i++);
            //        rows.Add(row);
            //    }
            //}

            for (j = 0; j < height - 1; j++ )
            {
                //if (!inRow && horizontalHisto[y] > threshold && horizontalHisto[y + 1] <= threshold)
                if (!inRow && horizontalHisto[j] > (maxPixel - threshold) && horizontalHisto[j + 1] < (maxPixel - threshold))
                {
                    inRow = true;
                    row.first = j;
                    continue;
                }
                if (inRow && horizontalHisto[j] < (maxPixel - threshold) && horizontalHisto[j + 1] > (maxPixel - threshold))
                {
                    inRow = false;
                    row.last = j+2;
                    j--;
                    while (horizontalHisto[++y] > threshold) ;
                    row.columns = CalcColumns(row.first, row.last, i++);
                    rows.Add(row);
                }
            }
            

            progressBar.PerformStep();
        }

        void RemoveVoidColumns()
        {
            int numberOfRows = 20, qty = 6, qtyCols = 36;

            int i, j, k;
            if (rows.Count > numberOfRows)
            {
                while (rows.Count != numberOfRows)
                    rows.RemoveAt(rows.Count - 1);
            }
            for (i = qty; i < numberOfRows; i++)
            {
                if (rows[i].columns.Count <= qtyCols) // old if (rows[i].columns.Count != qtyCols) 
                {  // 
                    rows.RemoveAt(i--);
                    numberOfRows--;
                    continue;
                }
                j = 36; //j=9;
                rows[i].columns.RemoveAt(j--); // 
                //rows[i].columns.RemoveAt(j--);
                //rows[i].columns.RemoveAt(--j);
            }
            int dem=0; 
            for (i = qty; i < rows.Count; i++)
            {
                CalcVerticalHisto(rows[i].first + 4, rows[i].last - 4); // 
                for (j = 0; j < rows[i].columns.Count; j++) // 
                {
                    k = rows[i].columns[j].first + 1;
                    while (k < rows[i].columns[j].last)
                        if (verticalHisto[k++] > 0)       //
                            break;
                    if (k == rows[i].columns[j].last)
                        dem++;
                }
                if (dem >=25) // if cells is empty
                {
                    rows.RemoveAt(i--); // Remove rows
                    removerows++;
                }
                dem = 0;
                //if (rows[i].columns.Count <5)   // 
                  //  rows.RemoveAt(i--);
            }
            progressBar.PerformStep();
        }

        List<Column> CalcColumns(int r1, int r2, int index) // calculate histogram to find columns
        {
            CalcVerticalHisto(r1, r2);
            int maxPixel = Max(verticalHisto, width);
            int threshold = maxPixel - 5;
            List<Column> columns = new List<Column>();
            Column column = new Column();
            bool inCol = false;
            for (int x = 1; x < width - 1; x++)
            {
                if (!inCol && verticalHisto[x] > threshold
                    && verticalHisto[x + 1] <= threshold)
                {
                    column.first = ++x;
                    inCol = true;
                }
                if (inCol && verticalHisto[x] <= threshold
                    && verticalHisto[x + 1] > threshold)
                {
                    switch (index)
                    {
                        case 1:
                        case 2:
                            column.last = x;
                            while (verticalHisto[++x] > threshold) ;
                            break;
                        default:
                            column.last = x;
                            break;
                    }
                    inCol = false;
                    columns.Add(column);
                }
            }
            return columns;
        }

        List<Column> CalcNoAreaCols(int r1, int r2)
        {
            CalcVerticalHisto(r1, r2);// calculate header histogram
            bool inCol = false;
            List<Column> columns = new List<Column>();
            Column column = new Column();
            int halfWidth = width / 2;
            for (int i = width - 1; i > halfWidth; i--)
            {
                if (!inCol && verticalHisto[i] == 0 && verticalHisto[i - 1] > 0)
                {
                    column.last = i;
                    inCol = true;
                }
                if (inCol && verticalHisto[i] > 0 && verticalHisto[i - 1] == 0)
                {
                    inCol = false;
                    column.first = i - 1;
                    columns.Add(column);
                }
            }
            columns.Reverse();
            return columns;
        }
        
    }
}




thank you.
Rosy

AnswerRe: Improve OCR program Pin
rosy847-Feb-15 22:38
rosy847-Feb-15 22:38 
QuestionHow to share a DLL between two applications? Pin
JBHowl30-Jan-15 4:54
JBHowl30-Jan-15 4:54 
QuestionRe: How to share a DLL between two applications? Pin
Eddy Vluggen30-Jan-15 5:13
professionalEddy Vluggen30-Jan-15 5:13 
AnswerRe: How to share a DLL between two applications? Pin
JBHowl30-Jan-15 5:15
JBHowl30-Jan-15 5:15 
GeneralRe: How to share a DLL between two applications? Pin
Eddy Vluggen30-Jan-15 5:38
professionalEddy Vluggen30-Jan-15 5:38 
AnswerRe: How to share a DLL between two applications? Pin
manchanx30-Jan-15 5:13
professionalmanchanx30-Jan-15 5:13 
GeneralRe: How to share a DLL between two applications? Pin
JBHowl30-Jan-15 5:15
JBHowl30-Jan-15 5:15 
GeneralRe: How to share a DLL between two applications? Pin
manchanx30-Jan-15 5:44
professionalmanchanx30-Jan-15 5:44 
AnswerRe: How to share a DLL between two applications? Pin
Richard Andrew x6430-Jan-15 5:24
professionalRichard Andrew x6430-Jan-15 5:24 
GeneralRe: How to share a DLL between two applications? Pin
JBHowl30-Jan-15 5:31
JBHowl30-Jan-15 5:31 
GeneralRe: How to share a DLL between two applications? Pin
Richard Andrew x6430-Jan-15 5:46
professionalRichard Andrew x6430-Jan-15 5:46 
AnswerRe: How to share a DLL between two applications? Pin
Gerry Schmitz30-Jan-15 10:32
mveGerry Schmitz30-Jan-15 10:32 
GeneralRe: How to share a DLL between two applications? Pin
jschell30-Jan-15 11:13
jschell30-Jan-15 11:13 
Questionsql to C#.net databinding Pin
User 1138061130-Jan-15 3:52
User 1138061130-Jan-15 3:52 
AnswerRe: sql to C#.net databinding Pin
OriginalGriff30-Jan-15 4:00
mveOriginalGriff30-Jan-15 4:00 
GeneralRe: sql to C#.net databinding Pin
User 1138061130-Jan-15 4:13
User 1138061130-Jan-15 4:13 
AnswerRe: sql to C#.net databinding Pin
Simon_Whale30-Jan-15 4:13
Simon_Whale30-Jan-15 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.