?? segmentationbymodel.cpp
字號:
// SegmentationbyModel.cpp: implementation of the CSegmentationbyModel class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SegmentationbyModel.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define ROUND(X) (((X)-int(X))>0.5?int(X)+1:int(X))
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSegmentationbyModel::CSegmentationbyModel()
{
}
CSegmentationbyModel::~CSegmentationbyModel()
{
}
CSegmentationbyModel::Segment(CList<CImage,CImage &> &ImgOutList,CImage &ImgVLP,CRect rcVLP, CImage &ImgOrg,BOOL &m_bBlack,BOOL IsEnter)
{
// CImage ImgVLP;
// ImgOrg.CutFromImg(ImgVLP,rcVLP);
#define HEAD_AND_TAIL 10
//白底黑字化
CImage ImgTemp;
CImage ImgOut=ImgVLP;
double dA[2];
double dB[2];
int nLR[2];
if(IsEnter == FALSE)
{
CImage imTwoOrg;
CImage imTwoQuZao;
ImgVLP.imGraytoTwo(imTwoOrg,m_bBlack);
CList<CRect,CRect> ListRect;
imTwoQuZao = imTwoOrg;
imTwoQuZao.imLTquzao(ListRect);
double dA[2];
double dB[2];
//先通過連通字符求得傾斜角
//要是連同不行就用跳變
if (imTwoQuZao.GetABByLianTong(dA,dB))
{
}
else
{
ImgVLP.GetABByTioaBian(dA,dB);
}
// ImgVLP.GetABByTioaBian(dA,dB);
int nLongType = 0;//等于0為正常;等于1為太長;等于-1為太短;
CImage imRctTwoOrg;
CImage imRctTwoQuZao;
CImage imRctVLP;
ImgVLP.RectifyAB(imRctVLP,dA,dB,m_bBlack?255:0,rcVLP.TopLeft(),&ImgOrg);
imTwoOrg.RectifyAB(imRctTwoOrg,dA,dB,m_bBlack?255:0,rcVLP.TopLeft(),NULL,FALSE);
imTwoQuZao.RectifyAB(imRctTwoQuZao,dA,dB,m_bBlack?255:0,rcVLP.TopLeft(),NULL,FALSE);
if (imRctVLP.GetWidth()>imRctVLP.GetHeight()*6)
{
nLongType = 1;
// AfxMessageBox("太長");
}
if (imRctVLP.GetWidth()<imRctVLP.GetHeight()*4)
{
nLongType = -1;
// AfxMessageBox("太短");
}
//去掉左右邊界
CImage imCutTwoOrg;
CImage imCutTwoQuZao = imRctVLP;
CImage imCutVLP;
int nLR[2];
ImgVLP.CutLRByTiaoBian(nLR,m_bBlack,imRctVLP.GetHeight());
imRctVLP.CutFromImg(ImgVLP,CRect(nLR[0],0,nLR[1],imRctVLP.GetHeight()));
imRctTwoQuZao.CutFromImg(ImgOut,CRect(nLR[0],0,nLR[1],imRctTwoQuZao.GetHeight()));
}
if(IsEnter == TRUE)
{
int m_nWidth = ImgVLP.GetWidth();
int m_nHeight = ImgVLP.GetHeight();
ImgVLP.imGraytoTwo(ImgOut,m_bBlack);
int nW = ImgOut.GetWidth();
int nH = ImgOut.GetHeight();
double K = 5.3;
if (nH<=11)
{
K = 6.1;
}
int x1 =0 ;
for(int i=0;i<nW/2;i++)
{
if((nW-i)/(1.0*nH)<K)
{
x1 = i;
break;
}
}
CImage ImgOut1 = ImgOut;
ImgOut1.CutFromImg(ImgOut,CRect(x1,0,nW,nH));
CImage ImgTemp1 = ImgVLP;
ImgTemp1.CutFromImg(ImgVLP,CRect(x1,0,nW,nH));
}
CImage ImgOut1;
ImgOut1 = ImgOut;
CList<CRect,CRect> ListRect;
ImgOut1.imLTquzao(ListRect);
//判斷太長
/*
*/
ImgOutList.RemoveAll();
int nLCut[10];
if(!GetCCutLine(ImgOut,nLCut,IsEnter,m_bBlack))
{
return FALSE;
}
/*
if(IsEnter == TRUE)
{
int nLCut1[2];
GetRCutLine1(ImgOut,nLCut1);
ImgOut.CutFromImg(ImgOut1,CRect(0,nLCut1[0],ImgOut.GetWidth(),nLCut1[1]));
ImgOut = ImgOut1;
GetRCutLine(ImgOut,nLCut1);
ImgOut.CutFromImg(ImgOut1,CRect(0,nLCut1[0],ImgOut.GetWidth(),nLCut1[1]));
}*/
CImage ImgOut2;
ImgOut1 = ImgOut;
for(int i=0;i<8;i++)
{
if (i==2) {
continue;
}
if(i==0)
{
ImgVLP.CutFromImg(ImgOut,CRect(nLCut[i],0,nLCut[i+1],ImgOut1.GetHeight()));
ImgOut2 = ImgOut;
ImgOut2.imGraytoTwo(ImgOut,m_bBlack);
int Top=0;
int Down = ImgOut.GetHeight();
ImgOut2 = ImgOut;
ImgOut2.hanziCutUpAndDown(Top,Down);
ImgOut2.CutFromImg(ImgOut,CRect(0,Top,ImgOut2.GetWidth(),Down));
ImgOut2 = ImgOut;
ImgOut2.DanziLeftAndRight(ImgOut);
ImgOutList.AddTail(ImgOut);
}
else
{
ImgOut1.CutFromImg(ImgOut,CRect(nLCut[i],0,nLCut[i+1],ImgOut1.GetHeight()));
int Top=0;
int Down = ImgOut.GetHeight();
ImgOut2 = ImgOut;
ImgOut2.CutUpAndDown(Top,Down);
ImgOut2.CutFromImg(ImgOut,CRect(0,Top,ImgOut2.GetWidth(),Down));
ImgOut2 = ImgOut;
ImgOut2.DanziLeftAndRight(ImgOut);
// ImgOut.DenoiseMax();
ImgOutList.AddTail(ImgOut);
}
}
return TRUE;
}
BOOL CSegmentationbyModel::GetCCutLine(CImage &imTwo, int nLCut[],BOOL IsEnter,BOOL &m_bBlack)
{
double n=1,m=0.5;
int x1=0,x2=0;
double h1,h2,h3;
int nW = imTwo.GetWidth();
int *pBPoint = new int[nW];
memset(pBPoint,0,sizeof(int)*nW);
for(int i=0;i<nW;i++)
{
for(int j=0;j<imTwo.GetHeight();j++)
{
if (imTwo.m_pR[j*nW+i]>128)
{
pBPoint[i]++;
}
}
}
int nMax=-100000000;
int nX1=0,nX2=0;
int Sum;
for(x1=0;x1<HEAD_AND_TAIL;x1++)
{
for(x2=0;x2<HEAD_AND_TAIL;x2++)
{
h2 = (nW-x1-x2)/(6+n+m);
h1=n*h2;
h3=m*h2;
Sum=0.0;
int LL=min(int(x1+h1+6*h2+h3)+1,imTwo.GetWidth());
int KK = max(int(x1-1),0);
//考慮情況x1==0和x2==0的時候
if (x1==0)
{
Sum = imTwo.GetHeight()+Max(pBPoint[int(x1+h1)-1],pBPoint[int(x1+h1)],pBPoint[int(x1+h1)+1])+Max(pBPoint[int(x1+h1+h2)-1],pBPoint[int(x1+h1+h2)],pBPoint[int(x1+h1+h2)+1])+
Max(pBPoint[int(x1+h1+h2+h3)-1],pBPoint[int(x1+h1+h2+h3)],pBPoint[int(x1+h1+h2+h3)]+1)+Max(pBPoint[int(x1+h1+2*h2+h3)-1],pBPoint[int(x1+h1+2*h2+h3)],pBPoint[int(x1+h1+2*h2+h3)+1])+
Max(pBPoint[int(x1+h1+3*h2+h3)-1],pBPoint[int(x1+h1+3*h2+h3)],pBPoint[int(x1+h1+3*h2+h3)+1])+Max(pBPoint[int(x1+h1+4*h2+h3)-1],pBPoint[int(x1+h1+4*h2+h3)],pBPoint[int(x1+h1+4*h2+h3)+1])+
Max(pBPoint[int(x1+h1+5*h2+h3)-1],pBPoint[int(x1+h1+5*h2+h3)],pBPoint[int(x1+h1+5*h2+h3)+1])+Max(pBPoint[int(x1+h1+6*h2+h3)-1],pBPoint[int(x1+h1+6*h2+h3)],pBPoint[LL]);
Sum = imTwo.GetHeight()+pBPoint[int(x1+h1)]+pBPoint[int(x1+h1+h2)]+pBPoint[int(x1+h1+h2+h3)]+pBPoint[int(x1+h1+2*h2+h3)]+pBPoint[int(x1+h1+3*h2+h3)]+pBPoint[int(x1+h1+4*h2+h3)]+pBPoint[int(x1+h1+5*h2+h3)]+pBPoint[int(x1+h1+6*h2+h3)];
// Sum1 = imTwo.GetHeight()+pBPoint[int(x1+h1)+1]+pBPoint[int(x1+h1+h2)]+pBPoint[int(x1+h1+h2+h3)]+pBPoint[int(x1+h1+2*h2+h3)]+pBPoint[int(x1+h1+3*h2+h3)]+pBPoint[int(x1+h1+4*h2+h3)]+pBPoint[int(x1+h1+5*h2+h3)]+pBPoint[int(x1+h1+6*h2+h3)];
}
else if (x2==0)
{
Sum = imTwo.GetHeight()+Max(pBPoint[KK],pBPoint[x1],pBPoint[x1+1])+Max(pBPoint[int(x1+h1)-1],pBPoint[int(x1+h1)],pBPoint[int(x1+h1)+1])+Max(pBPoint[int(x1+h1+h2)-1],pBPoint[int(x1+h1+h2)],pBPoint[int(x1+h1+h2)+1])+
Max(pBPoint[int(x1+h1+h2+h3)-1],pBPoint[int(x1+h1+h2+h3)],pBPoint[int(x1+h1+h2+h3)]+1)+Max(pBPoint[int(x1+h1+2*h2+h3)-1],pBPoint[int(x1+h1+2*h2+h3)],pBPoint[int(x1+h1+2*h2+h3)+1])+
Max(pBPoint[int(x1+h1+3*h2+h3)-1],pBPoint[int(x1+h1+3*h2+h3)],pBPoint[int(x1+h1+3*h2+h3)+1])+Max(pBPoint[int(x1+h1+4*h2+h3)-1],pBPoint[int(x1+h1+4*h2+h3)],pBPoint[int(x1+h1+4*h2+h3)+1])+
Max(pBPoint[int(x1+h1+5*h2+h3)-1],pBPoint[int(x1+h1+5*h2+h3)],pBPoint[int(x1+h1+5*h2+h3)+1]);
Sum = imTwo.GetHeight() +pBPoint[x1]+pBPoint[int(x1+h1)]+pBPoint[int(x1+h1+h2)]+pBPoint[int(x1+h1+h2+h3)]+pBPoint[int(x1+h1+2*h2+h3)]+pBPoint[int(x1+h1+3*h2+h3)]+pBPoint[int(x1+h1+4*h2+h3)]+pBPoint[int(x1+h1+5*h2+h3)];
}
else
{
Sum = Max(pBPoint[KK],pBPoint[x1],pBPoint[x1+1])+Max(pBPoint[int(x1+h1)-1],pBPoint[int(x1+h1)],pBPoint[int(x1+h1)+1])+Max(pBPoint[int(x1+h1+h2)-1],pBPoint[int(x1+h1+h2)],pBPoint[int(x1+h1+h2)+1])+
Max(pBPoint[int(x1+h1+h2+h3)-1],pBPoint[int(x1+h1+h2+h3)],pBPoint[int(x1+h1+h2+h3)]+1)+Max(pBPoint[int(x1+h1+2*h2+h3)-1],pBPoint[int(x1+h1+2*h2+h3)],pBPoint[int(x1+h1+2*h2+h3)+1])+
Max(pBPoint[int(x1+h1+3*h2+h3)-1],pBPoint[int(x1+h1+3*h2+h3)],pBPoint[int(x1+h1+3*h2+h3)+1])+Max(pBPoint[int(x1+h1+4*h2+h3)-1],pBPoint[int(x1+h1+4*h2+h3)],pBPoint[int(x1+h1+4*h2+h3)+1])+
Max(pBPoint[int(x1+h1+5*h2+h3)-1],pBPoint[int(x1+h1+5*h2+h3)],pBPoint[int(x1+h1+5*h2+h3)+1])+Max(pBPoint[int(x1+h1+6*h2+h3)-1],pBPoint[int(x1+h1+6*h2+h3)],pBPoint[LL]);
Sum = pBPoint[x1]+pBPoint[int(x1+h1)]+pBPoint[int(x1+h1+h2)]+pBPoint[int(x1+h1+h2+h3)]+pBPoint[int(x1+h1+2*h2+h3)]+pBPoint[int(x1+h1+3*h2+h3)]+pBPoint[int(x1+h1+4*h2+h3)]+pBPoint[int(x1+h1+5*h2+h3)]+pBPoint[int(x1+h1+6*h2+h3)];
}
if (Sum>nMax)
{
nMax = Sum;
nX1 = x1;
nX2 = x2;
}
}
}
if (nX1>8)
{
Sum = 0;
int *sum2 = new int[nX1];
memset(sum2,0,sizeof(int)*nX1);
for (int i=0;i<imTwo.GetHeight();i++)
{
for (int j=0;j<nX1;j++)
{
if (imTwo.m_pR[i*nW+j] <128)
{
Sum++;
sum2[j]++;
}
}
}
int Y;
Y = Sum/nX1;
int kk = nX1;
if((imTwo.GetHeight()-1>Y) &&(Y>imTwo.GetHeight()/3))
{
kk = 0;
}
for (i=0;i<nW;i++)
{
for (int j=0;j<nX1;j++)
{
if (sum2[j]>=imTwo.GetHeight()-1)
{
kk = j;
}
else if(sum2[j]<2)
{
kk = j;
}
else if(sum2[j]<imTwo.GetHeight()-1 && sum2[j]>2)
{
break;
}
}
}
nX1 = kk;
delete[]sum2;
}
h2 = (nW-nX1-nX2)/(6+n+m);
h1=n*h2;
h3=m*h2;
nLCut[0] = Max(pBPoint,nX1,imTwo.GetWidth());//int(nX1);
nLCut[1] = Max(pBPoint,nX1+h1,imTwo.GetWidth());//int(nX1+h1);
nLCut[2] = Max(pBPoint,nX1+h1+h2,imTwo.GetWidth());//int(nX1+h1+h2);
nLCut[3] = Max(pBPoint,nX1+h1+h2+h3,imTwo.GetWidth());//int(nX1+h1+h2+h3);
nLCut[4] = Max(pBPoint,nX1+h1+2*h2+h3,imTwo.GetWidth());//int(nX1+h1+2*h2+h3);
nLCut[5] = Max(pBPoint,nX1+h1+3*h2+h3,imTwo.GetWidth());//int(nX1+h1+3*h2+h3);
nLCut[6] = Max(pBPoint,nX1+h1+4*h2+h3,imTwo.GetWidth());//int(nX1+h1+4*h2+h3);
nLCut[7] = Max(pBPoint,nX1+h1+5*h2+h3,imTwo.GetWidth());//int(nX1+h1+5*h2+h3);
nLCut[8] = Max(pBPoint,nX1+h1+6*h2+h3,imTwo.GetWidth());//int(nX1+h1+6*h2+h3);
int Sum5 = 0;
/*
if(nLCut[3]-nLCut[2]<2 && IsEnter == FALSE)
{
delete []pBPoint;
return FALSE;
}*/
for(i=nLCut[2];i<nLCut[3];i++)
{
Sum5 += imTwo.GetHeight()-pBPoint[i];
}
double dRate = Sum5/(imTwo.GetHeight()*(nLCut[3]-nLCut[2])*1.0);
/*
if(dRate>0.75 && IsEnter == FALSE)
{
if(m_bBlack==TRUE)
{
m_bBlack = FALSE;
}
else
{
m_bBlack = TRUE;
}
delete []pBPoint;
return FALSE;
}
*/
if(dRate>0.25 && IsEnter == FALSE)
{
delete []pBPoint;
return FALSE;
}
delete []pBPoint;
return TRUE;
}
BOOL CSegmentationbyModel::GetRCutLine(CImage &imTwo, int nLCut[])
{
int KK = 2;
int LL = 1;
int nW = imTwo.GetWidth();
int nH = imTwo.GetHeight();
int Sum1=0, Sum2=0, Sum3=0;
int Y=-100;
int x1 = 0, x2 = 0;
int rh = 0;
int *pBPoint = new int[nH];
memset(pBPoint,0,sizeof(int)*nH);
for(int i=0;i<nH;i++)
{
for(int j=0;j<nW;j++)
{
if (imTwo.m_pR[i*nW+j]>128)
{
pBPoint[i]++;
}
}
}
if(i==0)
{
pBPoint[i]=nW-pBPoint[i];
}
/*
else
{
pBPoint[i]=abs(pBPoint[i]-pBPoint[i-1]);//應該可以不用
}*/
for(x1=0;x1<KK;x1++)
{
for(x2=0;x2<LL;x2++)
{
rh = nH-x2;
if(x2==0)
{
Sum1 = pBPoint[x1]+pBPoint[rh];
Sum2 = pBPoint[x1+1]+pBPoint[rh];
Sum3 = pBPoint[x1]+pBPoint[rh-1];
}
else
{
Sum1 = pBPoint[x1]+pBPoint[rh];
Sum2 = pBPoint[x1+1]+pBPoint[rh];
Sum3 = pBPoint[x1]+pBPoint[rh+1];//可考慮為rh-1;
}
//簡化下面操作
if(Sum1>Y)
{
Y=Sum1;
nLCut[0] = x1;
nLCut[1] = rh;
}
if(Sum2>Y)
{
Y=Sum2;
nLCut[0] = x1+1;
nLCut[1] = rh;
}
if(Sum3>Y)
{
Y=Sum3;
nLCut[0] = x1;
nLCut[1] = rh;
}
}
}
delete []pBPoint;
return TRUE;
}
int CSegmentationbyModel::Max(int a, int b, int c)
{
a = max(a,b);
a = max(a,c);
return a;
}
int CSegmentationbyModel::Max(int * pBPoint,int nPos,int nNum)
{
int data[3] = {max(nPos-1,0),nPos,min(nPos+1,nNum-1)};
int a,b,c;
a = pBPoint[data[0]];
b = pBPoint[data[1]];
c = pBPoint[data[2]];
int nMaxNo = 0;
nMaxNo = a>b?0:1;
int nMax = max(a,b);
nMaxNo = nMax>c?nMaxNo:2;
nMax = max(nMax,c);
return data[nMaxNo];
}
BOOL CSegmentationbyModel::GetRCutLine1(CImage &imTwo, int nLCut[])
{
int KK = 4;
int LL = 3;
int nW = imTwo.GetWidth();
int nH = imTwo.GetHeight();
int Sum1=0, Sum2=0, Sum3=0;
int Y=-100;
int x1 = 0, x2 = 0;
int rh = 0;
int *pBPoint = new int[nH];
memset(pBPoint,0,sizeof(int)*nH);
for(int i=0;i<nH;i++)
{
for(int j=0;j<nW;j++)
{
if (imTwo.m_pR[i*nW+j]>128)
{
pBPoint[i]++;
}
}
}
for(x1=0;x1<KK;x1++)
{
for(x2=0;x2<LL;x2++)
{
rh = nH-x2;
if(x2==0)
{
Sum1 = pBPoint[x1]+pBPoint[rh];
Sum2 = pBPoint[x1+1]+pBPoint[rh];
Sum3 = pBPoint[x1]+pBPoint[rh-1];
}
else
{
Sum1 = pBPoint[x1]+pBPoint[rh];
Sum2 = pBPoint[x1+1]+pBPoint[rh];
Sum3 = pBPoint[x1]+pBPoint[rh+1];//可考慮為rh-1;
}
//簡化下面操作
if(Sum1>Y)
{
Y=Sum1;
nLCut[0] = x1;
nLCut[1] = rh;
}
if(Sum2>Y)
{
Y=Sum2;
nLCut[0] = x1+1;
nLCut[1] = rh;
}
if(Sum3>Y)
{
Y=Sum3;
nLCut[0] = x1;
nLCut[1] = rh;
}
}
}
delete []pBPoint;
return TRUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -