www.pudn.com > dip.rar > DigitClass.cpp
// DigitClass.cpp: implementation of the CDigitClass class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DigitClass.h"
#include "Thinner.h"
//#include "dib.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDigitClass::CDigitClass()
{
}
CDigitClass::~CDigitClass()
{
}
/*void CDigitClass::CopyArToBitmap(void)
{
LONG x, y;
digitWidth = digitdib.GetWidth();
digitHeight = digitdib.GetHeight();
for(x=0; x= thre)
digitarray[x][y]=(BYTE)1;
else
digitarray[x][y]=(BYTE)0;
}
}
}
void CDigitClass::BinaryDigit()
{
int x,y;
int k=0;
long double total=0;
float aver=0;
for(x=0;x=judge)
{
digitarray[x][y]=1;
}
else
{
digitarray[x][y]=0;
}
}
}
}
void CDigitClass::ThinDigit_1()
{
LONG x,y,k;
k=0;
// digitWidth = digitdib.GetWidth();
// digitHeight = digitdib.GetHeight();
for(x=0; x= 0) && (j0 < lWidth) && (i0 >= 0) && (i0 < lHeight))
{
// 复制象素
digitarray[j][i] = temp_img[lHeight * j0 + i0];
}
else
{
// 对于源图中没有的象素,直接赋值为255
//digitarray[j][i] = 255;
digitarray[j][i] = 0;
}*/
}
}
delete [] temp_img;
}
void CDigitClass::FixSize1()
{
if(digitWidth==20&&digitHeight==36)
return;
// 源图像的宽度和高度
LONG lWidth=digitWidth;
LONG lHeight=digitHeight;
BYTE *temp_img=new BYTE[lWidth*lHeight];
// 循环变量
LONG i;
LONG j;
//memcpy(temp_img,digitarray,lWidth*lHeight);
for(i = 0; i < digitHeight; i++)
{
for(j = 0; j < digitWidth; j++)
{
temp_img[j*digitHeight+i]=digitarray[j][i];
}
}
// 缩放后图像的宽度和高度
LONG lNewWidth;
LONG lNewHeight;
float XZRatio=(float) 20/digitWidth;
float YZRatio=(float) 36/digitHeight;
// 象素在源坐标
LONG i0;
LONG j0;
// 计算缩放后的图像实际宽度
// 此处直接加0.5是由于强制类型转换时不四舍五入,而是直接截去小数部分
lNewWidth = (LONG) (lWidth * XZRatio + 0.5);
// 计算缩放后的图像高度
lNewHeight = (LONG) (lHeight * YZRatio + 0.5);
for(i = 0; i < lNewHeight; i++)
{
for(j = 0; j < lNewWidth; j++)
{
i0 = (LONG) (i / YZRatio + 0.5);
j0 = (LONG) (j / XZRatio + 0.5);
// 判断是否在源图范围内
if( (j0 >= 0) && (j0 < lWidth) && (i0 >= 0) && (i0 0?1:0;
}
}
delete [] temp;
}
BYTE CDigitClass::Interpolation(BYTE *image, LONG lWidth, LONG lHeight, FLOAT x, FLOAT y)
{
// 四个最临近象素的坐标(i1, j1), (i2, j1), (i1, j2), (i2, j2)
LONG i1, i2;
LONG j1, j2;
// 四个最临近象素值
BYTE f1, f2, f3, f4;
// 二个插值中间值
BYTE f12, f34;
// 定义一个值,当象素坐标相差小于改值时认为坐标相同
FLOAT EXP;
// 赋值
EXP = (FLOAT) 0.0001;
// 计算四个最临近象素的坐标
i1 = (LONG) x;
i2 = i1 + 1;
j1 = (LONG) y;
j2 = j1 + 1;
// 根据不同情况分别处理
if( (x < 0) || (x > lWidth - 1) || (y < 0) || (y > lHeight - 1))
{
// 要计算的点不在源图范围内,直接返回255。
return 255;
}
else
{
if (fabs(x - lWidth + 1) <= EXP)
{
// 要计算的点在图像右边缘上
if (fabs(y - lHeight + 1) <= EXP)
{
// 要计算的点正好是图像最右下角那一个象素,直接返回该点象素值
f1 = (BYTE) image[lWidth * j1 + i1];
return f1;
}
else
{
// 在图像右边缘上且不是最后一点,直接一次插值即可
f1 = (BYTE)image[lWidth * j1 + i1];
f3 = (BYTE)image[lWidth * j1 + i2];
// 返回插值结果
return ((BYTE) (f1 + (y -j1) * (f3 - f1)));
}
}
else if (fabs(y - lHeight + 1) <= EXP)
{
// 要计算的点在图像下边缘上且不是最后一点,直接一次插值即可
f1 = (BYTE)image[lWidth * j1 + i1];
f2 = (BYTE)image[lWidth * j2 + i1];
// 返回插值结果
return ((BYTE) (f1 + (x -i1) * (f2 - f1)));
}
else
{
// 计算四个最临近象素值
f1 = (BYTE) image[lWidth * j1 + i1];
f2 = (BYTE) image[lWidth * j2 + i1];
f3 = (BYTE) image[lWidth * j1 + i2];
f4 = (BYTE) image[lWidth * j2 + i2];
// 插值1
f12 = (BYTE) (f1 + (x - i1) * (f2 - f1));
// 插值2
f34 = (BYTE) (f3 + (x - i1) * (f4 - f3));
// 插值3
return ((BYTE) (f12 + (y -j1) * (f34 - f12)));
}
}
}