Professional Documents
Culture Documents
Draft Report-17-20
Draft Report-17-20
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.
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.
18
corresponding location is set to Black color else to White color. Thus the background and
foreground separation is achieved.
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.
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.
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