Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I have a class named OCRChar
C#
class OCRChar
    {
        public string fileName;
        public string binVal;       

        public OCRChar(string iFileName, string iBinVal)
        {
            this.fileName = iFileName;
            this.binVal = iBinVal;          
        }
    }


My Main Program consists of a button that loads some .png files, converts them to binary and I need to store them in memory so that later I can train a neural network with the binary values.

C#
private void btnLoadTrainingData_Click(object sender, EventArgs e)
        {
            string[] filenames = Directory.GetFiles(@"C:\temp\test", "*.png", SearchOption.AllDirectories);

            foreach (string f in filenames)
            {
                Console.WriteLine(getFirstChar(Path.GetFileName(f)));
                img = (Bitmap)Bitmap.FromFile(f);                
                c = new OCRChar(f,convertToBinary(img));               
            }
            //img.Dispose();
        }


How can I list all the items I have stored? maybe using a foreach loop? this is not working

C#
foreach (OCRChar c )
            {
            Console.WriteLine(c.binVal);
            }
Posted
Updated 17-Nov-12 2:36am
v2
Comments
Toli Cuturicu 17-Nov-12 20:34pm    
Very poor syntax. This will never compile!

1 solution

I think you need to go back a step, and review what a list and so forth is.

"c" is not a list. I can tell this, because you are assigning a single instance of an object to it:
C#
c = new OCRChar(f,convertToBinary(img));

Therefore, it can't be used as an enumerable value in a foreach loop:
C#
foreach (OCRChar c )
            {
            Console.WriteLine(c.binVal);
            }
(Ignoring that the syntax for foreach doesn't look like that).

Step back, and look at what you are trying to do.
Add an emumerable object to your main form, so hold all the OCRChar instances - I would suggest that a List of OCRChar objects would be a good choice:

C#
private List<OCRChar> ocrFiles = new List<OCRChar>();

Now, when you create a new instance, add it to the list:
C#
foreach (string f in filenames)
{
    Console.WriteLine(getFirstChar(Path.GetFileName(f)));
    img = (Bitmap)Bitmap.FromFile(f);
    ocrFiles.Add(new OCRChar(f,convertToBinary(img)));
}

Then you can iterate through them later:
C#
foreach (OCRChar c in ocrFiles)
{
    Console.WriteLine(c.binVal);
}


At the moment, it looks like you are trying to code by guessing and hoping it works - that isn't a viable strategy! I strongly recommend that you go back a stage or two and look at the basics - get a good solid grasp of them first before you go much further. You are venturing into harder and harder territory and I can't see that you are going to do more than confuse and frustrate yourself unless you understand the simple stuff a lot better than you appear to now.
 
Share this answer
 
Comments
datt265 17-Nov-12 9:14am    
Yes you are true I am trying to guess code and hope it works. Actually I am a bit confused of what is the best and easiest way to get this code ready. Actually I still do not know if this is the right way to do it or there was another simple solution.
OriginalGriff 17-Nov-12 9:35am    
Without knowing what you are trying to achieve, I can't tell! :laugh:
I can tell what the code does - mostly - but I have no idea why.
Do you have a book, or course to follow?
datt265 17-Nov-12 10:07am    
I am trying to build an OCR using back propagation neural networks.

my task is to load a number of .png images that I can train my neural network with, and then with other images I can be able to read the character.
OriginalGriff 17-Nov-12 10:34am    
So...why are you converting them to a string of '0' or '1'? An array of bytes, pixels or bools would be more useful, wouldn't it? And probably a two dimensional array would be more use than a "flat" string?
datt265 17-Nov-12 23:47pm    
So do you suggest removing the class OCRChar + the list and implementing a multi dim array is better? My binary number consists of 300 1's & 0's as my image is a 30x40 = 1200, I have downsized it to 15x20 = 300.

Together with this I have to add the character and the desired output.

Example:

lets say a particular 'A' binary equivalent is 0000011100.......0000111 and I have to fix a desired output. Since I have 10 numeric digits and 26 alphabet letters in total 36 characters I will require an output of 6 bits. That is why I have assigned a binary output to each character so each and every 'A' has 000000 and 'B' has 000001 and so on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900