📄 lprprocess.cpp
字号:
}
pDestImg->Width = TempImg.Width;
pDestImg->Height = TempImg.Height;
pDestImg->pImg = TempImg.pImg;
return 1;
}
int RectifyLP(PGrayImg pDestImg, PGrayImg pSrcImg, PGrayImg pModelImg)
{
int i, j;
int res = 0;
TAngle Angle;
double RectifyAngle;
int iAngle;
TGrayImg TempImg1, TempImg2;
TGrayImg RowDiffImg;
res = Diff(&RowDiffImg, pModelImg, H_FILTER);
if (0 == res)
return 0;
res = HoughTrans(&Angle, &RowDiffImg, V_FILTER);
if (0 == res)
return 0;
res = GrayImg_Free(&RowDiffImg);
if (0 == res)
return 0;
res = Diff(&RowDiffImg, pModelImg, V_FILTER);
if (0 == res)
return 0;
res = HoughTrans(&Angle, &RowDiffImg, H_FILTER);
if (0 == res)
return 0;
res = GrayImg_Free(&RowDiffImg);
if (0 == res)
return 0;
res = GrayImg_Malloc(&TempImg1, pSrcImg->Width, pSrcImg->Height);
if (0 == res)
return 0;
for (i=0; i<pSrcImg->Height; i++)
for (j=0; j<pSrcImg->Width; j++)
*(TempImg1.pImg+i*TempImg1.Width+j) = 0;
if (fabs(Angle.HAngle+90) != 90.0)
RectifyAngle = -(Angle.HAngle + 90.0)/180.0*PI;
for (i=0; i<pSrcImg->Width; i++)
for (j=0; j<pSrcImg->Height; j++)
{
if (i*tan(RectifyAngle) < 0.0)
iAngle = (int)(i*tan(RectifyAngle)-0.5);
else
iAngle = (int)(i*tan(RectifyAngle)+0.5);
if ((j-iAngle >= 0) && (j-iAngle < pSrcImg->Height))
*(TempImg1.pImg+j*TempImg1.Width+i) = *(pSrcImg->pImg+(j-iAngle)*pSrcImg->Width+i);
}
res = GrayImg_Malloc(&TempImg2, pSrcImg->Width, pSrcImg->Height);
if (0 == res)
return 0;
for (i=0; i<pSrcImg->Height; i++)
for (j=0; j<pSrcImg->Width; j++)
*(TempImg2.pImg+i*TempImg2.Width+j) = 0;
if (fabs(Angle.VAngle) != 90.0)
RectifyAngle = Angle.VAngle/180.0*PI;
for (i=0; i<TempImg1.Height; i++)
for (j=0; j<TempImg1.Width; j++)
{
if (i*tan(RectifyAngle) < 0.0)
iAngle = (int)(i*tan(RectifyAngle)-0.5);
else
iAngle = (int)(i*tan(RectifyAngle)+0.5);
if ((j-iAngle >= 0) && (j-iAngle < TempImg1.Width))
*(TempImg2.pImg+i*TempImg2.Width+j) = *(TempImg1.pImg+i*TempImg1.Width+j-iAngle);
}
res = GrayImg_Free(&TempImg1);
if (0 == res)
return 0;
pDestImg->Width = TempImg2.Width;
pDestImg->Height = TempImg2.Height;
pDestImg->pImg = TempImg2.pImg;
return 1;
}
int Diff(PGrayImg pDestImg, PGrayImg pSrcImg, TPrjType PrjType)
{
int i, j;
int Temp;
int res = 0;
TGrayImg TempImg;
res = GrayImg_Malloc(&TempImg, pSrcImg->Width, pSrcImg->Height);
if (res == 0)
return 0;
switch(PrjType)
{
case H_FILTER:
for (i=0; i<pSrcImg->Height; i++)
*(TempImg.pImg+i*TempImg.Width+0) = 0;
for (i=0; i<pSrcImg->Height; i++)
for (j=1; j<pSrcImg->Width; j++)
{
Temp = abs(*(pSrcImg->pImg+i*pSrcImg->Width+j)-*(pSrcImg->pImg+i*pSrcImg->Width+j-1));
if (Temp > 80)
Temp = 255;
else
Temp = 0;
*(TempImg.pImg+i*TempImg.Width+j) = Temp;
}
break;
case V_FILTER:
for (j=0; j<pSrcImg->Width; j++)
*(TempImg.pImg+0+j) = 0;
for (j=0; j<pSrcImg->Width; j++)
for (i=1; i<pSrcImg->Height; i++)
{
Temp = abs(*(pSrcImg->pImg+i*pSrcImg->Width+j)-*(pSrcImg->pImg+(i-1)*pSrcImg->Width+j));
if (Temp > 80)
Temp = 255;
else
Temp = 0;
*(TempImg.pImg+i*TempImg.Width+j) = Temp;
}
break;
default:
return 0;
}
pDestImg->Width = TempImg.Width;
pDestImg->Height = TempImg.Height;
pDestImg->pImg = TempImg.pImg;
return 1;
}
int HoughTrans(PAngle pAngle, PGrayImg pSrcImg, TPrjType PrjType)
{
int i, j, p, o, Max, Tempo;
int *H;
int HWidth, HHeight;
double temp;
o = -90; p = 0;
HHeight = 4 * pSrcImg->Width + 1;
HWidth = 181;
H = (int*)malloc(HWidth*HHeight*sizeof(int));
if (NULL == H)
return 0;
for (i=-(HHeight/2); i<=(HHeight/2); i++)
for (j=-(HWidth/2); j<=(HWidth/2); j++)
*(H+(i+HHeight/2)*HWidth+j+HWidth/2) = 0;
switch (PrjType)
{
case H_FILTER:
for (i=0; i<pSrcImg->Height; i++)
for (j=0; j<pSrcImg->Width; j++)
{
if (*(pSrcImg->pImg+i*pSrcImg->Width+j) == 255)
{
for (o=-10; o<=10; o++)
{
if (o >= 0)
Tempo = o + 80;
else
Tempo = o - 80;
temp = j*cos(Tempo*PI/180.0)+i*sin(Tempo*PI/180.0);
if (temp < 0.0)
p = (int)(temp-0.5);
else
p = (int)(temp+0.5);
if ((p+HHeight/2 >= 0) && (p + HHeight/2 < HHeight))
(*(H+(p+HHeight/2)*HWidth+Tempo+HWidth/2))++;
}
}
}
Max = 0;
for (i=-HHeight/2; i<=HHeight/2; i++)
for (j=-10; j<=10; j++)
{
if (j >= 0)
Tempo = j + 80;
else
Tempo = j - 80;
if (Max<*(H+(i+HHeight/2)*HWidth+Tempo+HWidth/2))
{
Max = *(H+(i+HHeight/2)*HWidth+Tempo+HWidth/2);
o = Tempo;
p = i;
}
}
pAngle->HAngle = o;
break;
case V_FILTER:
for (i=0; i<pSrcImg->Height; i++)
for (j=0; j<pSrcImg->Width; j++)
{
if (*(pSrcImg->pImg+i*pSrcImg->Width+j) == 255)
{
for (o=-10; o<=10; o++)
{
temp = j*cos(o*PI/180.0)+i*sin(o*PI/180.0);
if (temp < 0.0)
p = (int)(temp-0.5);
else
p = (int)(temp+0.5);
if ((p+HHeight/2 >= 0) && (p+HHeight/2 < HHeight))
(*(H+(p+HHeight/2)*HWidth+o+HWidth/2))++;
}
}
}
Max = 0;
for (i=-HHeight/2; i<=HHeight/2; i++)
for (j=-10; j<=10; j++)
{
if (Max < *(H+(i+HHeight/2)*HWidth+j+HWidth/2))
{
Max = *(H+(i+HHeight/2)*HWidth+j+HWidth/2);
o = j;
p = i;
}
}
pAngle->VAngle = o;
break;
default:
if (H)
free(H);
return 0;
}
if (H)
{
free(H);
H = NULL;
}
return 1;
}
int GetLPAccuratePos(RECT *lpRect, PGrayImg pSrcImg)
{
int res = 0;
TGrayImg DiffImg;
TGrayImg LPImg;
int UpYPos, DownYPos, LeftXPos, RightXPos;
int *pHProject, *pVProject;
res = Diff(&DiffImg, pSrcImg, H_FILTER);
if (0 == res)
return 0;
pHProject = (int*)malloc(pSrcImg->Height*sizeof(int));
res = ProjectImg(pHProject, &DiffImg, H_FILTER);
if (0 == res)
return 0;
res = ProjectFilter(pHProject, pSrcImg->Height, IH_FILTER);
if (0 == res)
return 0;
res = GetLPAccurateYPos(pHProject, pSrcImg->Height, &UpYPos, &DownYPos);
if (0 == res)
return 0;
lpRect->left = 0;
lpRect->right = pSrcImg->Width - 1;
lpRect->top = UpYPos;
lpRect->bottom = DownYPos;
res = GetSubImg(&LPImg, pSrcImg, *lpRect);
if (0 == res)
return 0;
res = GrayImg_Free(&DiffImg);
if (0 == res)
return 0;
pVProject = (int*)malloc(LPImg.Width*sizeof(int));
res = ProjectImg(pVProject, &LPImg, V_FILTER);
if (0 == res)
return 0;
res = ProjectFilter(pVProject, LPImg.Width, IV_FILTER, DownYPos - UpYPos);
if (0 == res)
return 0;
res = GetLPAccurateXPos(pVProject, LPImg.Width, DownYPos - UpYPos, &LeftXPos, &RightXPos);
if (0 == res)
return 0;
if (pHProject != NULL)
{
free(pHProject);
pHProject = NULL;
}
if (pVProject != NULL)
{
free(pVProject);
pVProject = NULL;
}
res = GrayImg_Free(&LPImg);
if (0 == res)
return 0;
lpRect->top = UpYPos;
lpRect->bottom = DownYPos;
lpRect->left = LeftXPos;
lpRect->right = RightXPos;
return 1;
}
int GetLPAccurateXPos(int *pProject, int PrjLen, int LPHeight, int *pLeftXPos, int *pRightXPos)
{
int i, j, k;
int TempLeft, TempRight;
int TempPos[101], Code[101];
BOOL Flag1, Flag2;
*pLeftXPos = 0;
*pRightXPos = 0;
for (i=0; i<PrjLen; i++)
{
if (*(pProject+i) != 0)
{
*pLeftXPos = i;
break;
}
}
if ((int)(4.4*LPHeight+0.5)+(*pLeftXPos) > PrjLen-1)
*pRightXPos = PrjLen - 1;
else
*pRightXPos = (int)(4.4 * LPHeight + 0.5) + (*pLeftXPos);
if (*(pProject+(*pRightXPos)) != 0)
{
for (i=(*pRightXPos); i<PrjLen; i++)
{
if (*(pProject+i) == 0)
break;
}
*pRightXPos = i;
}
if ((*pRightXPos)-(*pLeftXPos) > (int)(4.0*LPHeight+0.5))
{
Flag2 = TRUE;
Flag1 = TRUE;
j = 0;
k = 0;
for (i=(*pLeftXPos); i<PrjLen; i++)
{
if ((*(pProject+i) != 0) && Flag1)
Flag1 = FALSE;
if ((*(pProject+i) == 0) && (!Flag1))
{
TempPos[j] = i;
j++;
if (j > 100)
{
return 0;
}
TempLeft = i;
Flag1 = TRUE;
Flag2 = FALSE;
}
if ((*(pProject+i) != 0) && (!Flag2))
{
TempRight = i;
if (TempRight-TempLeft < (int)(0.25*LPHeight+0.5))
Code[k] = 0;
else
Code[k] = 1;
k++;
if (k > 100)
return 0;
Flag2 = TRUE;
}
}
for (i=0; i<=j-2; i++)
{
if (Code[i] == 0)
{
if (Code[i+1] == 1)
{
if (TempPos[i]-(int)(0.45*LPHeight+0.5) > 0)
*pLeftXPos = TempPos[i]-(int)(0.45*LPHeight+0.5);
else
*pLeftXPos = 0;
break;
}
}
}
if ((int)(4.4*LPHeight+0.5)+(*pLeftXPos) < PrjLen-1)
*pRightXPos = (int)(4.4*LPHeight+0.5)+(*pLeftXPos);
else
*pRightXPos = PrjLen - 1;
if (*(pProject+(*pRightXPos)) != 0)
{
for (i=(*pRightXPos); i<PrjLen; i++)
{
if (*(pProject+i) == 0)
break;
}
*pRightXPos = i;
}
}
if ((*pRightXPos) <= (*pLeftXPos))
{
return 0;
}
return 1;
}
int GetLPAccurateYPos(int *pProject, int PrjLen, int *pUpYPos, int *pDownYPos)
{
int x;
int Temp, TempUp, TempDown;
BOOL Flag;
Flag = TRUE;
Temp = 0;
*pUpYPos = 0; *pDownYPos = 0;
*(pProject+PrjLen-1) = 0;
for (x=0; x<PrjLen; x++)
{
if (Flag && (*(pProject+x) != 0))
{
TempUp = x;
Flag = FALSE;
}
if ((!Flag) && (*(pProject+x) == 0))
{
TempDown = x;
Flag = TRUE;
if (Temp < TempDown- TempUp)
{
*pUpYPos = TempUp;
*pDownYPos = TempDown;
Temp = TempDown - TempUp;
}
}
}
if ((*pDownYPos) <= (*pUpYPos))
return 0;
return 1;
}
int GetLPCharPos(int *pCharCount, RECT LPCharRect[], PGrayImg pSrcImg)
{
int i, j, k, x, y;
BOOL Flag, Flag1;
int CharID;
int avg;
int TempLeft, TempRight;
int Code[101];
int *pOutLineProject;
int res = 0;
pOutLineProject = (int*)malloc(pSrcImg->Width*sizeof(int));
res = OutLineProject(pOutLineProject, pSrcImg);
if (0 == res)
return 0;
res = OutLineProjectFilter(pOutLineProject, pSrcImg->Width, pSrcImg->Height);
if (0 == res)
return 0;
Flag = TRUE;
CharID = 0;
*(pOutLineProject+pSrcImg->Width-1) = 0;
for (i=0; i<pSrcImg->Width; i++)
{
if ((*(pOutLineProject+i) != 0) && Flag)
{
LPCharRect[CharID].left = i;
LPCharRect[CharID].top = 0;
LPCharRect[CharID].bottom = pSrcImg->Height - 1;
Flag = FALSE;
avg = 0;
}
if ((*(pOutLineProject+i) == 0) && (!Flag))
{
LPCharRect[CharID].right = i;
for (j=LPCharRect[CharID].left; j<=LPCharRect[CharID].right; j++)
{
avg += *(pOutLineProject+j);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -