【发布时间】:2018-06-30 16:35:05
【问题描述】:
我正在尝试缩放二维二进制数组(我从位图图像中提取)。我正在使用这种缩放来使用特征提取进行单词识别。我已经尝试使用此代码来缩放大小为 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
【问题讨论】:
-
所以你需要做一些调试。
-
@MBo 我试过了,但找不到错误。
标签: image algorithm c++11 image-processing bitmap