Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

public void nonret(SqlCommand cmd) // creating class for non-return operations

from the database//


{
cmd.Connection = con; // creating connection//
cmd.CommandType = CommandType.StoredProcedure; //invoking stored
procedure//
con.Open(); // opening connection//
cmd.ExecuteNonQuery(); // executing non-return query//
con.Close(); //closing connection//
}
The above code shows how the data base is connected. Also code for the non-return
function like ‘insert’ is shown above. In the same way return functions, functions calling the
maximum value etc can be described.

4.1.1 Code for image size and format specification

Before applying the preprocessing operations a piece of code is written to enable image
resize, so that irrespective of the format and size of the scanned input image, conversion can be
performed. For this to be possible the code is as follows.

public Bitmap Save(Bitmap image, int maxWidth, int maxHeight, int quality)
{
int originalWidth = image.Width;
int originalHeight = image.Height;
// To preserve the aspect ratio//
float ratioX = (float)maxWidth / (float)originalWidth;
float ratioY = (float)maxHeight / (float)originalHeight;
float ratio = Math.Min(ratioX, ratioY);
// New width and height based on aspect ratio//
int newWidth = (int)(originalWidth * ratio);
int newHeight = (int)(originalHeight * ratio);
// Convert other formats to RGB//
Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.
Format24bppRgb ); }

17
4.1.2 Code for Thresholding

The scanned image is the input and at first the input image is preprocessed. That is the
operations such as removal of noise, thresholding and thinning has to be performed on the input
image. The thresholding operation is performed in order to separate the character from
background, i.e. foreground and background separation is achieved on calling the thresholding
function.

bitmaps bs= new bitmaps();

pictureBox1.Image = bs.threshold(bmp, 100);

The function threshold() is defined in the class bimaps. So an object bs of the class
bitmaps is created and the function is called using the object created. The bmp contains the input
image and 100 is the threshold value set by the manually. The function definition defined in the
bitmaps class is as follows.

for (int i = 0; i < width; i++)


{
for (int j = 0; j < height; j++)
{
Color c = b.GetPixel(i, j);
if (c.R< th && c.G<th && c.B<th)
{
b.SetPixel(i, j, Color.Black);
}
else
{
b.SetPixel(i, j, Color.White);
}
The height and width is that of the input image. Using the GetPixel() method the pixel
value at each position is retrieved and stored in a color variable c. The RGB component values
are retrieved with the c.R, c.G and c.B expressions. These values are compared with the
threshold value. If the the value is less than the threshold value then the pixel value at the

18
corresponding location is set to Black color else to White color. Thus the background and
foreground separation is achieved.

4.1.3 Code for Thinning operation

The thinning operation is the next process to be applied on the scanned input image. This
is applied in order to bring all the characters to a single pixel width. For this operation to perform
erosion is applied. The function call is as:

pictureBox1.Image = bs.eros((Bitmap)pictureBox1.Image);
The eros() function is defined in the bitmaps class. Thus using its object bs eros()
function is called and the scanned image in the picture box is passed as the parameter. The
function definition is as follows.
public Bitmap eros(Bitmap bmp)
{
Erosion s = new Erosion();
Bitmap f = s.Apply(bmp);
return f;

}
The Erosion is a built in class defined in the namespace ‘using system. Aforge. Imaging.
Filters’. The eros() function takes the scanned image as the input parameter. An object is created
for the Erosion class named s. Using the apply function the erosion is applied on to the image
and the image thus is returned.

4.1.4 Code for Feature Extraction

The second stage is the Feature extraction where the characters in the image are detected.
After the preprocessing stage the filtered image is produced. Now the feature extraction is
applied on to the preprocessed image. The code for the following process is as below.

string d = " ";


Ocr.Gen oc = new Ocr.Gen();
oc.Process_img(textBox1.Text);

19
string str = "";
List<string> c = oc.getstr();
for (int i = 0; i < c.Count; i++)
{
d = c[i];

if (d != "null")
{
string[] f = d.Split(',');

ch.pcor = Convert.ToInt32(f[0]);
ch.pend = Convert.ToInt32(f[1]);
ch.pbif = Convert.ToInt32(f[2]);
dt = p.values(ch);

if (dt.Rows.Count > 0)
{
str += dt.Rows[0][0].ToString();
}
else
{
str += " ";
}
}
else
{
str += "";
}
richTextBox1.Text = str;
}

20

You might also like