高档位图图像的二维二进制数组

时间:2018-01-22 04:57:52

标签: image algorithm c++11 image-processing bitmap

我正在尝试缩放2维二进制数组(我从位图图像中提取)。我正在使用这种缩放来使用特征提取进行单词识别。我已经尝试过这个代码来缩放尺寸为27 X 40的2D数组以将其缩放到32 X 32并且它可以进一步缩小尺寸,但是对于像56 x 56或63 x 63这样的尺寸进行升级而停止和抛出异常。我认为那里可能是使用出界内存但无法找到它的问题。有什么帮助吗?

这里使用的变量是:

        @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;
            }
}

我的输入数组大小为27 X 40,如下所示:

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

1 个答案:

答案 0 :(得分:0)

我提出了一种更好的缩放图像方法,算法如下:

void gradient_scale(int srcrow,int srccol)
{
    int i1,j1,k,ii,jj;
    int n = srccol;
    int m = srcrow;
    cout<<srcrow<<" "<<srccol<<endl;
    int x,y;
    //Here I want to scale an input image into 63X63 image
    double v1 = ((double)63/(double)m) ;
    double v2 = ((double)63/(double)n);
    //cout<<v1<<"  "<<v2<<endl;
    for(ii=0;ii<1000;ii++)
    for(jj=0;jj<1000;jj++)
        directional_scaled[ii][jj]=0;

    for(n=0;n<63;n++)
    {
      for(m=0;m<63;m++)
      {
        x = (int)((double)m/v1)+1 ;
        y = (int)((double)n/v2) +1;
        //cout<<x<<" "<<y<<endl;
        if(x>=srcrow||y>=srccol)
        {
          break;
        }
        gradient_scaled[m][n] = FinalImageBuffer[x][y];
        }
    }
   for(i1=0;i1<63;i1++)
   {
       for(j1=0;j1<63;j1++)
       {
            cout<<gradient_scaled[i1][j1];
       }
       cout<<endl;
   }

}