Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have a class named OCRChar
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.
 
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
 
foreach (OCRChar c )
            {
            Console.WriteLine(c.binVal);
            }
Posted 17-Nov-12 2:27am
bmw318mt1.2K
Edited 17-Nov-12 2:36am
v2
Comments
Toli Cuturicu at 17-Nov-12 20:34pm
   
Very poor syntax. This will never compile!

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 = new OCRChar(f,convertToBinary(img));
Therefore, it can't be used as an enumerable value in a foreach loop:
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:
 
private List<OCRChar> ocrFiles = new List<OCRChar>();
Now, when you create a new instance, add it to the list:
            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:
            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.
  Permalink  
Comments
bmw318mt at 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 at 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?
bmw318mt at 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 at 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?
bmw318mt at 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.
RaisKazi at 17-Nov-12 12:12pm
   
My 5!

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

  Print Answers RSS
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,680
2 CPallini 4,850
3 George Jonsson 3,454
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100