dimanche 21 janvier 2018

Upscale a 2 Dimensional binary Array of a bitmap image

I am trying to scale a 2 Dimensional binary array(which I extracted from a bitmap image). I am using this scaling for word recognition using feature extraction. I have tried this code for scaling a 2D array of size 27 X 40 to scale it to 32 X 32 and it works fine for further downscaling but stops and throws exception for upscaling for sizes like 56 x 56 or 63 x 63. I think there might a problem of using of out of bound memory but am unable to find it out. Any help ?

Here the variables used are :

        @param top : It hold the row number from where the character starts.
        @param bottom : It holds the row number where the character ends.
        @param left : It holds the column number from where the character starts.
        @param right : It holds the column number where the character ends.
        @param destrow : It represents the new height of the character.
        @param destcol : It represents the new width of the character.
        FinalImageBuffer    : input image 2D array
        scaled : output scaled 2D array
//call made via:  scale(0,objBMP.intheight,0,objBMP.intWidth,63,63) where objBMP is an object of BMP file.


#define FRACTION(x) ((x)-(floor(x)))
#define INT(x) ((int)floor(x))
#define THRESHOLD 0.2
void scale(int top, int bottom, int left, int right, int destrow, int destcol)
{
    int l=0,m=0;                    
    int ssize=0;
    int srcrow =  bottom - top+1;
    int srccol =  right - left+1;
    float i,j;    
    float pval1,pval2,pval;
    float xint,yint;
    int newht=0,newwd=0;
    int ii,jj, i1, j1;
    newht=0;
    newwd=0;
    ssize = destrow;

    for(ii=0;ii<1000;ii++)
    for(jj=0;jj<1000;jj++)
        scaled[ii][jj]=0;



    if(srcrow > srccol)
                {
                    destcol = (int)floor((ssize/(double)srcrow*srccol) + 0.5);
                    //newwd = destcol;
                }
                else
                {
                    destrow = (int)floor((ssize/(double)srccol*srcrow) + 0.5);
                    //newht = destrow;
                }
    newht = destrow;
    newwd = destcol;

    xint = (float)(srcrow)/(destrow);           //xinterval
    yint = (float)(srccol)/(destcol);           //yinterval


    i = top;
    for (l = 0; l < destrow; l++)
    {       
        j = left;
        for ( m = 0; m < destcol ; m++ )
        {
            if ( INT(j) >= right-1)
                pval1 = FinalImageBuffer[INT(i)][INT(j)];
            else
                pval1 = (1 - FRACTION(j))*FinalImageBuffer[INT(i)][INT(j)] +
                    FRACTION(j)*FinalImageBuffer[INT(i)][INT(j)+1];
            if ( INT(i) >= bottom-1 || INT (j) >= right-1 )
                pval2 = FinalImageBuffer[INT(i)][INT(j)];
            else
                pval2 = (1 - FRACTION(j))*FinalImageBuffer[INT(i)+1][INT(j)] +
                    FRACTION(j)*FinalImageBuffer[INT(i)+1][INT(j)+1];

            pval = (1 - FRACTION(i))*pval1 + FRACTION(i)*pval2;
            if ( pval > THRESHOLD)
                scaled[l][m] = 1;
            else
                scaled[l][m] = 0;
            j += yint;

        }
        i += xint;
    }

        if(srcrow > srccol)
            {    
                for(i1=0;i1<ssize;i1++)
                for(j1=newwd;j1<ssize;j1++)
                    scaled[i1][j1]=0;
            }
            else
            {   
                for(i1=newht;i1<ssize;i1++)
                for(j1=0;j1<ssize;j1++)
                    scaled[i1][j1]=0;
            }
}

My input array of size 27 X 40 looks like:

1111000000000000000000000000000000000000
1111110000000000000000001111000011111000
1111111000000000000001111111100111111111
1111111100000000000001111111101111111111
1111111100000111000001111111101111111111
1111111110001111100001111111110111111110
0001111111111111100000111111110001111110
0000111111111111110000111110000001111100
0000011111111111110000111110000001111100
0000011111111111110000111110000001111100
0000011111110011110011111110000001111100
0000111111100011110011111110000001111100
0001111111000011111111111110000001111100
0001111110000011111111111110000001111100
0011111110000011111111111110000001111100
0011111110000011111100111110000001111100
0011111100000111111000111110000001111100
0011111100001111110000111110000001111100
0011111000011111110000111110000000000000
0000000000011111110000111110000000000000
0000000000011111110000111110000000000000
0000000000011111100000111110000000000000
0000000000011111000000111111000000000000
0000000000011111000000111111000000000000
0000000000011110000001111111000000000000
0000000000000000000001111111000000000000

Aucun commentaire:

Enregistrer un commentaire