ImageObject.cpp
资源名称:LPR.rar [点击查看]
上传用户:jjiangjuan
上传日期:2013-03-27
资源大小:59k
文件大小:24k
源码类别:
波变换
开发平台:
Visual C++
- // ImageObject.cpp : implementation file
- //
- #include "stdafx.h"
- #include "LPR.h"
- #include "ImageObject.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CImageObject
- BYTE BMPRec[COL*ROW*3];
- BYTE GraftBuf[COL][ROW];
- int FINISHED[COL][ROW],Closed[COL][ROW],VetorRec[COL][ROW];
- CImageObject::CImageObject()
- {
- lpBuf=NULL;
- LPLocated = false;
- StrongStrech = false;
- }
- CImageObject::~CImageObject()
- {
- }
- BEGIN_MESSAGE_MAP(CImageObject, CStatic)
- //{{AFX_MSG_MAP(CImageObject)
- ON_WM_PAINT()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CImageObject message handlers
- void CImageObject::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // TODO: Add your message handler code here
- // Do not call CStatic::OnPaint() for painting messages
- HDC hDC;
- hDC = dc.GetSafeHdc();
- if(lpBuf!=NULL&&LPLocated!=true)
- {
- StretchDIBits(hDC,
- 0,
- 0,
- bitmapinfoheader.biWidth,
- bitmapinfoheader.biHeight,
- 0,
- 0,
- bitmapinfoheader.biWidth,
- bitmapinfoheader.biHeight,
- lpBuf,
- pbi,
- DIB_RGB_COLORS,
- SRCCOPY);
- }
- else
- if(LPLocated==true)
- {
- StretchDIBits(hDC,
- 0,
- 0,
- bitmapinfoheader.biWidth,
- bitmapinfoheader.biHeight,
- 0,
- 0,
- bitmapinfoheader.biWidth,
- bitmapinfoheader.biHeight,
- BMPRec,
- pbi,
- DIB_RGB_COLORS,
- SRCCOPY);
- }
- }
- void CImageObject::LoadBmp()
- {
- LPLocated=false;
- CString strPath;
- CFile file;
- CFileDialog *openFile = new CFileDialog(TRUE,"bmp","*.bmp",NULL,"Bitmap Files(*.bmp)",NULL);
- if(openFile->DoModal() == IDOK)
- {
- strPath = openFile->GetPathName();
- if(file.Open(strPath,CFile::modeRead|CFile::shareDenyNone,NULL)==0)
- {
- AfxMessageBox("无法开文件!",MB_OK,0);
- return;
- }
- file.Read(&bitmapfileheader,sizeof(bitmapfileheader));
- file.Read(&bitmapinfoheader,sizeof(bitmapinfoheader));
- numQuad=0;
- if(bitmapinfoheader.biBitCount<24)
- {
- numQuad=1<<bitmapinfoheader.biBitCount;
- }
- pbi=(BITMAPINFO*) HeapAlloc(GetProcessHeap(),0,sizeof(BITMAPINFOHEADER)+numQuad*sizeof(RGBQUAD));
- memcpy(pbi,&bitmapinfoheader,sizeof(bitmapinfoheader));
- quad=(RGBQUAD*)((BYTE*)pbi+sizeof(BITMAPINFOHEADER));
- if(numQuad!=0)
- {
- file.Read(quad,sizeof(RGBQUAD)*numQuad);
- }
- bitmapinfoheader.biSizeImage=bitmapfileheader.bfSize-bitmapfileheader.bfOffBits;
- lpBuf=(BYTE*)HeapAlloc(GetProcessHeap(),0,bitmapinfoheader.biSizeImage);
- file.Read(lpBuf,bitmapinfoheader.biSizeImage);
- file.Close();
- }
- Invalidate (FALSE);
- }
- void CImageObject::gray(BYTE lpData[COL][ROW])
- {
- BYTE GrayLevel;
- int i,j,k;
- int w = bitmapinfoheader.biWidth;
- int h = bitmapinfoheader.biHeight;
- for(i = 0; i < h ;i++)
- {
- for(j = 0; j < w;j++)
- {
- k = i*w+j;
- GrayLevel = BYTE(0.299*lpBuf[k*3] + 0.587*lpBuf[k*3+1] + 0.114 * lpBuf[k*3+2]);
- BMPRec[k*3]=lpBuf[k*3];
- BMPRec[k*3+1]=lpBuf[k*3+1];
- BMPRec[k*3+2]=lpBuf[k*3+2];
- lpBuf[k*3] = GrayLevel;
- lpBuf[k*3+1] = GrayLevel;
- lpBuf[k*3+2] = GrayLevel;
- lpData[i][j]=GrayLevel;
- GraftBuf[i][j] = GrayLevel;
- }
- }
- Invalidate(FALSE);
- }
- void CImageObject::InteEqualize(BYTE lpData[COL][ROW],int lCount[])
- {
- int lTemp, i, j;
- // 灰度映射表
- BYTE bMap[256];
- for(i = 0; i < 256; i++)
- {
- bMap[i] = 0;
- }
- // 计算灰度映射表
- for (i = 0; i < 256; i++)
- {
- // 初始为0
- lTemp = 0;
- for (j = 0; j <= i ; j++)
- {
- lTemp += lCount[j];
- }
- // 计算对应的新灰度值
- bMap[i] = (BYTE) ((lTemp * 254+i) / COL / ROW);
- }
- int w = bitmapinfoheader.biWidth;
- int h = bitmapinfoheader.biHeight;
- for(i = 0; i < h; i++)
- {
- for(j = 0; j < w; j++)
- {
- GraftBuf[i][j] = bMap[GraftBuf[i][j]];
- lpData[i][j] = GraftBuf[i][j];
- }
- }
- this->Graft2BmpData();
- Invalidate(FALSE);
- }
- void CImageObject::GetEdge()
- {
- int i,j;
- BYTE TempBuf[COL][ROW];
- for(j=1;j<ROW;j++)//changed
- {
- for(i=0;i<COL;i++)//changed
- {
- TempBuf[i][j-1]=abs(GraftBuf[i][j]-GraftBuf[i][j-1]);//)<100)?2*abs(GraftBuf[i][j]-GraftBuf[i][j-1]):abs(GraftBuf[i][j]-GraftBuf[i][j-1]);
- if(TempBuf[i][j-1]<=20)
- TempBuf[i][j-1]=0;
- if(j==ROW-1)
- TempBuf[i][j]=0;
- }
- }
- for(i=1;i<COL;i++)
- for(j=0;j<ROW;j++)
- {
- if(abs(GraftBuf[i][j]-GraftBuf[i-1][j])>=TempBuf[i-1][j])
- TempBuf[i-1][j]=abs(GraftBuf[i][j]-GraftBuf[i-1][j]);//(abs(GraftBuf[i][j]-GraftBuf[i-1][j])<100)?2*abs(GraftBuf[i][j]-GraftBuf[i-1][j]):abs(GraftBuf[i][j]-GraftBuf[i-1][j]);//+TempBuf[i-1][j]+50;
- if(i==COL-1)
- TempBuf[i][j]=0;
- GraftBuf[i-1][j]=TempBuf[i-1][j];
- }
- for(i=0;i<COL;i++)
- for(j=0;j<ROW;j++)
- Closed[i][j]=NOTSEARCH;
- this->Graft2BmpData();
- }
- void CImageObject::Graft2BmpData()
- {
- int i,j,k;
- int h = bitmapinfoheader.biHeight;
- int w = bitmapinfoheader.biWidth;
- for(i=0;i< h;i++)
- {
- for(j=0;j< w;j++)
- {
- k = i*w+j;
- lpBuf[k*3]=GraftBuf[i][j];
- lpBuf[k*3+1]=GraftBuf[i][j];
- lpBuf[k*3+2]=GraftBuf[i][j];
- }
- }
- }
- void CImageObject::DelNoise()
- {
- int i,j;
- int sum=0,num1=0;
- EdgeTest();
- for(j=0;j<ROW;j++)
- {
- for(i=0;i<COL;i++)
- {
- if(FINISHED[i][j]!=SEARCHED)
- {
- sum=0;
- FilterDirectionPix(Closed[i][j],i,j,sum);
- }
- }
- }
- for(j=0;j<ROW;j++)
- for(i=0;i<COL;i++)
- FINISHED[i][j]=NOTSEARCH;
- for(i=0;i<COL;i++)
- {
- for(j=0;j<ROW;j++)
- {
- if((Closed[i][j]==9||Closed[i][j]==10)&&GraftBuf[i][j]!=0)
- GraftBuf[i][j]=0;
- if(FINISHED[i][j]==NOTSEARCH)
- {
- sum=0;
- ResetNoise(Closed[i][j],i,j,sum);
- }
- }
- }
- this->Graft2BmpData();
- Invalidate(FALSE);
- }
- void CImageObject::EdgeTest()
- {
- nDir=NODIRECT;
- int i,j;
- for(i=0;i<COL;i++)
- for(j=0;j<ROW;j++)
- {
- Closed[i][j]=NOTSEARCH;
- FINISHED[i][j]=NOTSEARCH;
- }
- for(i=1;i<COL-1;i++)
- for(j=1;j<ROW-1;j++)
- {
- if(GraftBuf[i][j]==0)
- Closed[i][j]=NODIRECT;
- if(GraftBuf[i][j]!=0 && Closed[i][j]==NOTSEARCH)
- {
- int zigzag=6;
- sort(i,j,NODIRECT,zigzag);
- if(Closed[i][j]==NODIRECT)
- GraftBuf[i][j]=0;
- }
- VetorRec[i][j]=Closed[i][j];
- }
- }
- int CImageObject::sort(int i, int j, int direct, int &zigzag)
- {
- int x,y;
- x=j;
- y=i;
- Closed[i][j]=GetSimiUnSearchPix(x,y);
- ZigzagFilter(zigzag,direct,Closed[i][j],y,x);
- if(Closed[i][j]==NODIRECT||zigzag<=0)
- {
- ///////////GetSimiUnSearchPix
- if(zigzag<=0)
- {
- Closed[y][x]=NODIRECT;//degrade gradually
- GraftBuf[y][x]=0;
- }
- return Closed[i][j];//graftbuf[i][j]可能不为0
- }
- if(GraftBuf[y][x]==0&&Closed[y][x]==NODIRECT)
- {
- Closed[i][j]=NODIRECT;
- return Closed[i][j];
- }
- if(x==ROW-1||y==COL-1||x==0||y==0)
- return Closed[i][j];
- else{
- //{
- int TAG=sort(y,x,Closed[i][j],zigzag);
- if(TAG!=NODIRECT)
- {
- if(zigzag<=0)
- {//degrade gradually,compare 2 directions
- if(Closed[i][j]!=TAG)
- {
- GraftBuf[y][x]=0;
- Closed[y][x]=NODIRECT;
- return Closed[i][j];
- }
- else{
- return Closed[i][j];//a sequence of same directions but zigzag=0
- }
- }
- else{//degrade suddently,zigzag!=0
- return Closed[i][j];
- }
- }
- else
- {
- return Closed[i][j];
- }
- }
- }
- int CImageObject::GetSimiUnSearchPix(int &xPos, int &yPos)
- {
- int Simi=255,x,y;
- x=xPos;
- y=yPos;
- for(int i=y-1;i<=y+1;i++)
- for(int j=x-1;j<=x+1;j++)
- {
- if(GraftBuf[i][j]==0)
- Closed[i][j]=NODIRECT;
- if((i!=y||j!=x)&&Closed[i][j]==NOTSEARCH)
- {
- if(Simi>abs(GraftBuf[y][x]-GraftBuf[i][j]))
- {
- Simi=abs(GraftBuf[y][x]-GraftBuf[i][j]);
- yPos=i;
- xPos=j;
- }
- }
- }
- return GetDirection(x,y,xPos,yPos);
- }
- void CImageObject::ZigzagFilter(int &zigzag, int direct0, int direct1, int i, int j)
- {
- switch(direct0)
- {
- case UP:switch (direct1)
- {
- case UP:zigzag++; return;
- case LEFT:
- case RIGHT:zigzag-=1;return;//zig-=2
- case UPRIGHT:
- case UPLEFT:return;
- default:GraftBuf[i][j]=0;Closed[i][j]=NODIRECT;return;
- }
- case UPRIGHT:switch (direct1)
- {
- case UP:
- case RIGHT: return;
- case UPRIGHT:zigzag-=1;return;
- default:GraftBuf[i][j]=0;Closed[i][j]=NODIRECT;return;
- }
- case UPLEFT:switch (direct1)
- {
- case UP:
- case LEFT: return;
- case UPLEFT:zigzag-=1;return;
- default:GraftBuf[i][j]=0;Closed[i][j]=NODIRECT;return;
- }
- case LEFT:switch (direct1)
- {
- case LEFT: zigzag++; return;
- case UP:
- case DOWN:zigzag-=1;return;
- case DOWNLEFT:
- case UPLEFT:return;
- default:GraftBuf[i][j]=0;Closed[i][j]=NODIRECT;return;
- }
- case RIGHT:switch (direct1)
- {
- case RIGHT:zigzag++; return;
- case UP:
- case DOWN:zigzag-=1;return;
- case DOWNRIGHT:
- case UPRIGHT:return;
- default:GraftBuf[i][j]=0;Closed[i][j]=NODIRECT;return;
- }
- case DOWN:switch (direct1)
- {
- case DOWN:zigzag++; return;
- case LEFT:
- case RIGHT:zigzag-=1;return;
- case DOWNRIGHT:
- case DOWNLEFT:return;
- default:GraftBuf[i][j]=0;Closed[i][j]=NODIRECT;return;
- }
- case DOWNRIGHT:switch (direct1)
- {
- case DOWN:
- case RIGHT: return;
- case DOWNRIGHT:zigzag-=1;return;
- default:GraftBuf[i][j]=0;Closed[i][j]=NODIRECT;return;
- }
- case DOWNLEFT:switch (direct1)
- {
- case DOWN:
- case LEFT: return;
- case DOWNLEFT:zigzag-=1;return;
- default:GraftBuf[i][j]=0;Closed[i][j]=NODIRECT;return;
- }
- default:return;
- }
- }
- void CImageObject::FilterDirectionPix(int direct, int i, int j, int &tol)
- {
- int nDir;
- switch(direct)
- {
- case UP:nDir=GetVetor(i+1,j);
- if(nDir==direct)
- {
- tol++;
- FilterDirectionPix(nDir,i+1,j,tol);
- }
- break;
- case DOWN:nDir=GetVetor(i-1,j);
- if(nDir==direct)
- {
- tol++;
- FilterDirectionPix(nDir,i-1,j,tol);
- }
- break;
- case LEFT:nDir=GetVetor(i,j-1);
- if(nDir==direct)
- {
- tol++;
- FilterDirectionPix(nDir,i,j-1,tol);
- }
- break;
- case RIGHT:nDir=GetVetor(i,j+1);
- if(nDir==direct)
- {
- tol++;
- FilterDirectionPix(nDir,i,j+1,tol);
- }
- break;
- case UPRIGHT:nDir=GetVetor(i+1,j+1);
- if(nDir==direct)
- {
- tol++;
- FilterDirectionPix(nDir,i+1,j+1,tol);
- }
- break;
- case UPLEFT:nDir=GetVetor(i+1,j-1);
- if(nDir==direct)
- {
- tol++;
- FilterDirectionPix(nDir,i+1,j-1,tol);
- }
- break;
- case DOWNRIGHT:nDir=GetVetor(i-1,j+1);
- if(nDir==direct)
- {
- tol++;
- FilterDirectionPix(nDir,i-1,j+1,tol);
- }
- break;
- case DOWNLEFT:nDir=GetVetor(i-1,j-1);
- if(nDir==direct)
- {
- tol++;
- FilterDirectionPix(nDir,i-1,j-1,tol);
- }
- break;
- default:break;
- }
- FINISHED[i][j]=SEARCHED;
- if(tol<5||tol>80)
- {
- GraftBuf[i][j]=0;
- Closed[i][j]=NODIRECT;
- }
- return;
- }
- int CImageObject::GetVetor(int i, int j)
- {
- return Closed[i][j];
- }
- void CImageObject::ResetNoise(int direct, int i, int j, int &tol)
- {
- switch(direct) {
- case LEFT:
- if(GetVetor(i,j-1)==direct)
- {
- tol++;
- if(j-1>0)
- ResetNoise(Closed[i][j-1],i,j-1,tol);
- }
- break;
- case RIGHT:
- if(GetVetor(i,j+1)==direct)
- {
- tol++;
- if(j+1<ROW)
- ResetNoise(Closed[i][j+1],i,j+1,tol);
- }
- break;
- default:break;
- }
- FINISHED[i][j]=SEARCHED;
- if(tol>80)
- {
- GraftBuf[i][j]=0;
- Closed[i][j]=NODIRECT;
- }
- return;
- }
- int CImageObject::GetDirection(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1)
- {
- bool forward=(x1-x0)>0;
- switch (forward)
- {
- case true:{
- if((y1-y0)==0)
- return RIGHT;
- if((y1-y0)>0)
- return UPRIGHT;
- else
- return DOWNRIGHT;
- }
- case false:{
- if((x1-x0)==0)
- {
- if((y1-y0)>0)
- return UP;
- if(y1-y0==0)
- {
- // GraftBuf[x0][y0]=0;
- return NODIRECT;
- }
- else
- return DOWN;
- }
- if(y1-y0>0)
- return UPLEFT;
- if(y1-y0==0)
- return LEFT;
- if(y1-y0<0)
- return DOWNLEFT;
- }
- }
- }
- LPCandidate * CImageObject::GetLPPos()
- {
- int i,j,tolerance;
- plumb *cur,*p;
- cur=&LPYBorder;
- for(j=0;j<ROW;j++)
- for(i=0;i<COL;i++)
- FINISHED[i][j]=NOTSEARCH;
- for(j=1;j<ROW;j++)
- for(i=1;i<COL;i++)
- {
- if((Closed[i][j]==UP||Closed[i][j]==DOWN)&&FINISHED[i][j]==NOTSEARCH)
- {
- int x=j,y=i;
- p=new plumb;
- p->direct=Closed[i][j];
- p->i0=i;
- p->j0=j;
- Cruiser(Closed[i][j],y,x);
- p->i1=y;
- p->j1=x;
- p->next=NULL;
- cur->next=p;
- cur=cur->next;
- }
- }
- cur=&LPXBorder;
- for(j=0;j<ROW;j++)
- for(i=0;i<COL;i++)
- FINISHED[i][j]=NOTSEARCH;
- for(i=1;i<COL;i++)
- for(j=1;j<ROW;j++)
- {
- if((Closed[i][j]==LEFT||Closed[i][j]==RIGHT)&&FINISHED[i][j]==NOTSEARCH)
- {
- int x=j,y=i;
- p=new plumb;
- p->direct=Closed[i][j];
- p->i0=i;
- p->j0=j;
- Cruiser(Closed[i][j],y,x);
- p->i1=y;
- p->j1=x;
- p->next=NULL;
- cur->next=p;
- cur=cur->next;
- }
- }
- LPCandidate *pLP=&LPC;
- p=LPYBorder.next;
- while(p!=NULL)
- {
- plumb *cur=p->next;
- while(cur!=NULL)
- {
- if(abs(cur->j0-p->j0)>70&&abs(cur->j0-p->j0)<=150&&((p->i0>=cur->i0&&p->i0<=cur->i1)||(p->i1>=cur->i0&&p->i1<=cur->i1)))
- {
- LPCandidate * LPP=new LPCandidate;
- plumb *ptr0=SelectEdge(p->j0,p->i0,cur->j0,cur->i0,DOWN);
- LPP->x1=cur->j0;LPP->y1=ptr0->i0;
- plumb *ptr1=SelectEdge(p->j0,p->i1,cur->j0,cur->i1,UP);
- LPP->x0=p->j0;LPP->y0=ptr1->i0;
- if(LPP->y0-LPP->y1<30&&LPP->y0-LPP->y1>15)
- {
- LPP->next=NULL;
- pLP->next=LPP;
- pLP=pLP->next;
- }
- else
- {
- delete LPP;
- }
- }
- cur=cur->next;
- }
- p=p->next;
- }
- /////纹理分析,叠加直方图均衡化图
- double C=0,max=0;
- P=NULL;
- pLP=LPC.next;
- if(pLP!=NULL)
- while (pLP!=NULL) {
- C=double(RAStat(pLP->x0,pLP->y0,pLP->x1,pLP->y1))/double(pLP->x1-pLP->x0);//double(pLP->x1-pLP->x0);
- if((C>max||double(C/max)>0.9)&&double(pLP->x1-pLP->x0)/double(pLP->y0-pLP->y1)>3.2)//&&double(pLP->x1-pLP->x0)/double(pLP->y0-pLP->y1)<4)
- {
- if(P!=NULL&&max>C)
- {
- if(abs(160-(pLP->x1+pLP->x0)/2)<abs(160-(P->x1+P->x0)/2))
- {
- max=C;
- P=pLP;
- }
- }
- else
- {
- max=C;
- P=pLP;
- }
- }
- pLP=pLP->next;
- }
- if(P!=NULL)
- {
- LPLocated=true;
- if(double(P->x1-P->x0)/double(P->y0-P->y1)>3.8)//扩充边界到上一边缘,修改LPLOCATE 结构
- {
- P->x0-=12;
- P->x1+=6;
- P->y0+=10;
- P->y1-=10;
- }
- else
- {
- P->x0-=16;
- P->x1+=10;
- P->y0+=6;
- P->y1-=6;
- }
- int k=0;
- for(i=P->y1;i<=P->y0;i++)
- {
- BMPRec[i*ROW*3+P->x0*3]=0;
- BMPRec[i*ROW*3+P->x0*3+1]=255;
- BMPRec[i*ROW*3+P->x0*3+2]=0;
- BMPRec[i*ROW*3+P->x1*3]=0;
- BMPRec[i*ROW*3+P->x1*3+1]=255;
- BMPRec[i*ROW*3+P->x1*3+2]=0;
- BMPRec[i*ROW*3+(P->x0-1)*3]=0;
- BMPRec[i*ROW*3+(P->x0-1)*3+1]=255;
- BMPRec[i*ROW*3+(P->x0-1)*3+2]=0;
- BMPRec[i*ROW*3+(P->x1-1)*3]=0;
- BMPRec[i*ROW*3+(P->x1-1)*3+1]=255;
- BMPRec[i*ROW*3+(P->x1-1)*3+2]=0;
- BMPRec[i*ROW*3+(P->x0+11)*3]=0;
- BMPRec[i*ROW*3+(P->x0+1)*3+1]=255;
- BMPRec[i*ROW*3+(P->x0+1)*3+2]=0;
- BMPRec[i*ROW*3+(P->x1+1)*3]=0;
- BMPRec[i*ROW*3+(P->x1+1)*3+1]=255;
- BMPRec[i*ROW*3+(P->x1+1)*3+2]=0;
- }
- for(j=P->x0;j<=P->x1;j++)
- {
- BMPRec[P->y0*ROW*3+j*3]=0;
- BMPRec[P->y0*ROW*3+j*3+1]=255;
- BMPRec[P->y0*ROW*3+j*3+2]=0;
- BMPRec[P->y1*ROW*3+j*3]=0;
- BMPRec[P->y1*ROW*3+j*3+1]=255;
- BMPRec[P->y1*ROW*3+j*3+2]=0;
- BMPRec[(P->y1-1)*ROW*3+j*3]=0;
- BMPRec[(P->y1-1)*ROW*3+j*3+1]=255;
- BMPRec[(P->y1-1)*ROW*3+j*3+2]=0;
- BMPRec[(P->y1-1)*ROW*3+j*3]=0;
- BMPRec[(P->y1-1)*ROW*3+j*3+1]=255;
- BMPRec[(P->y1-1)*ROW*3+j*3+2]=0;
- BMPRec[(P->y0+1)*ROW*3+j*3]=0;
- BMPRec[(P->y0+1)*ROW*3+j*3+1]=255;
- BMPRec[(P->y0+1)*ROW*3+j*3+2]=0;
- BMPRec[(P->y0+1)*ROW*3+j*3]=0;
- BMPRec[(P->y0+1)*ROW*3+j*3+1]=255;
- BMPRec[(P->y0+1)*ROW*3+j*3+2]=0;
- }
- }
- if(!LPLocated)
- Graft2BmpData();
- else
- Graft2BmpData(BMPRec);
- Invalidate(FALSE);
- return P;
- }
- void CImageObject::Cruiser(int direct, int &i, int &j)
- {
- int x=j,y=i;
- if(direct==DOWN||direct==UP)
- switch(direct) {
- case DOWN:
- if(GetVetor(i-1,j)==direct&&i-1>0)
- {
- i--;
- Cruiser(DOWN,i,j);
- }
- break;
- case UP:
- if(GetVetor(i+1,j)==direct&&i+1<COL)
- {
- i++;
- Cruiser(UP,i,j);
- }
- break;
- default:break;
- }
- else
- switch(direct) {
- case LEFT:
- if(GetVetor(i,j-1)==direct&&j-1>0)
- {
- j--;
- Cruiser(LEFT,i,j);
- }
- break;
- case RIGHT:
- if(GetVetor(i,j+1)==direct&&j+1<ROW)
- {
- j++;
- Cruiser(RIGHT,i,j);
- }
- break;
- default:break;
- }
- FINISHED[y][x]=SEARCHED;
- return;
- }
- plumb * CImageObject::SelectEdge(int SX, int SY, int EX, int EY, int tag)
- {
- plumb *p=LPXBorder.next;
- struct plumb tmp;
- CDL.i0=0;
- CDL.i1=0;
- CDL.j0=0;
- CDL.j1=0;
- CDL.next=NULL;
- tmp=CDL;
- if(tag==DOWN)
- {
- SY=(SY<EY)?SY:EY;
- while (p!=NULL)
- {
- if(p->i0>SY)
- break;
- if((p->j1<=EX&&p->j0>SX)&&p->i0>tmp.i0)
- {
- tmp=*p;
- p=p->next;
- }
- else
- p=p->next;
- }
- CDL=tmp;
- return &CDL;
- }
- else
- {
- SY=(SY>EY)?SY:EY;
- while (p!=NULL)
- {
- if(p->i0<SY)
- {
- p=p->next;
- continue;
- }
- if(p->j1<=EX&&p->j0>SX)
- {
- tmp=*p;
- break;
- }
- else
- p=p->next;
- }
- CDL=tmp;
- return &CDL;
- }
- }
- int CImageObject::RAStat(int x0, int y0, int x1, int y1)
- {
- int nCount=0;
- int i,j;
- for(i=y1;i<=y0;i++)
- for(j=x0;j<=x1;j++)
- FINISHED[i][j]=NOTSEARCH;
- for(i=y1;i<y0;i++)
- for(j=x0;j<x1;j++)
- {
- if(VetorRec[i][j]==NODIRECT)
- {
- FINISHED[i][j]=SEARCHED;
- }
- else
- if(FINISHED[i][j]==NOTSEARCH)
- {
- RASearch(VetorRec[i][j],i,j,x0,y0,x1,y1,nCount);
- }
- }
- return nCount;
- }
- void CImageObject::Graft2BmpData(BYTE BMP[])
- {
- int i,j,k;
- for(i=0;i<COL;i++)
- {
- for(j=0;j<ROW;j++)
- {
- k = i*ROW+j;
- lpBuf[k*3]=BMP[k*3];
- lpBuf[k*3+1]=BMP[k*3+1];
- lpBuf[k*3+2]=BMP[k*3+2];
- }
- }
- }
- void CImageObject::RASearch(int direct, int i, int j, int x0, int y0, int x1, int y1, int &count)
- {
- switch(direct) {
- case UP:if(i+1<=y0)
- {
- FINISHED[i][j]=SEARCHED;
- if(VetorRec[i+1][j]==NODIRECT||FINISHED[i+1][j]==SEARCHED)
- return;
- if(VetorRec[i+1][j]==LEFT||VetorRec[i+1][j]==RIGHT||VetorRec[i+1][j]==UP)
- count+=5;//count++;
- RASearch(VetorRec[i+1][j],i+1,j,x0,y0,x1,y1,count);
- }
- break;
- case DOWN:if(i-1>=y1)
- {
- FINISHED[i][j]=SEARCHED;
- if(VetorRec[i-1][j]==NODIRECT||FINISHED[i-1][j]==SEARCHED)
- return;
- if(VetorRec[i-1][j]==LEFT||VetorRec[i-1][j]==RIGHT||VetorRec[i-1][j]==DOWN)
- count+=5;
- RASearch(VetorRec[i-1][j],i-1,j,x0,y0,x1,y1,count);
- }
- break;
- case RIGHT:if(j+1<=x1)
- {
- FINISHED[i][j]=SEARCHED;
- if(VetorRec[i][j+1]==NODIRECT||FINISHED[i][j+1]==SEARCHED)
- return;
- if(VetorRec[i][j+1]==UP||VetorRec[i][j+1]==DOWN)
- count+=5;
- RASearch(VetorRec[i][j+1],i,j+1,x0,y0,x1,y1,count);
- }
- break;
- case LEFT:if(j-1>=x0)
- {
- FINISHED[i][j]=SEARCHED;
- if(VetorRec[i][j-1]==NODIRECT||FINISHED[i][j-1]==SEARCHED)
- return;
- if(VetorRec[i][j-1]==UP||VetorRec[i][j-1]==DOWN)
- count+=5;
- RASearch(VetorRec[i][j-1],i,j-1,x0,y0,x1,y1,count);
- }
- break;
- case UPRIGHT:if(j+1<=x1&&i+1<=y0)
- {
- FINISHED[i][j]=SEARCHED;
- if(VetorRec[i+1][j+1]==NODIRECT||FINISHED[i+1][j+1]==SEARCHED)
- return;
- // if(VetorRec[i+1][j+1]==UPLEFT||VetorRec[i+1][j+1]==DOWNRIGHT||VetorRec[i+1][j+1]==UPRIGHT)
- if(VetorRec[i+1][j+1]!=UPRIGHT)
- count+=3;
- RASearch(VetorRec[i+1][j+1],i+1,j+1,x0,y0,x1,y1,count);
- }
- break;
- case DOWNRIGHT:if(j+1<=x1&&i-1>=y1)
- {
- FINISHED[i][j]=SEARCHED;
- if(VetorRec[i-1][j+1]==NODIRECT||FINISHED[i-1][j+1]==SEARCHED)
- return;
- // if(VetorRec[i-1][j+1]==DOWNLEFT||VetorRec[i-1][j+1]==UPRIGHT)
- if(VetorRec[i-1][j+1]!=DOWNRIGHT)
- count+=3;
- RASearch(VetorRec[i-1][j+1],i-1,j+1,x0,y0,x1,y1,count);
- }
- break;
- case UPLEFT:if(i+1<=y0&&j-1>=x0)
- {
- FINISHED[i][j]=SEARCHED;
- if(VetorRec[i+1][j-1]==NODIRECT||FINISHED[i+1][j-1]==SEARCHED)
- return;
- // if(VetorRec[i+1][j-1]==UPRIGHT||VetorRec[i+1][j-1]==DOWNLEFT)
- if(VetorRec[i+1][j-1]!=UPLEFT)
- count+=3;
- RASearch(VetorRec[i+1][j-1],i+1,j-1,x0,y0,x1,y1,count);
- }
- break;
- case DOWNLEFT:if(j-1>=x0&&i-1>=y1)
- {
- FINISHED[i][j]=SEARCHED;
- if(VetorRec[i-1][j-1]==NODIRECT||FINISHED[i-1][j-1]==SEARCHED)
- return;
- // if(VetorRec[i-1][j-1]==UPRIGHT||VetorRec[i-1][j-1]==DOWNRIGHT)
- if(VetorRec[i-1][j-1]!=DOWNLEFT)
- count+=3;
- RASearch(VetorRec[i-1][j-1],i-1,j-1,x0,y0,x1,y1,count);
- }
- break;
- default:break;
- }
- return;
- }
- BITMAPINFO CImageObject::WriteDibFile(int Height, int Width, BYTE buf[])
- {
- ForLPR.bmiHeader.biBitCount=24;
- ForLPR.bmiHeader.biHeight=Height;
- ForLPR.bmiHeader.biWidth=Width;
- ForLPR.bmiHeader.biClrUsed=0;
- ForLPR.bmiHeader.biClrImportant=0;
- ForLPR.bmiHeader.biSizeImage=Height*Width*3;
- ForLPR.bmiHeader.biCompression=0;
- ForLPR.bmiHeader.biPlanes=1;
- ForLPR.bmiHeader.biXPelsPerMeter=3780;
- ForLPR.bmiHeader.biYPelsPerMeter=3780;
- ForLPR.bmiHeader.biSize=40;
- ForLPR.bmiColors[0]=*quad;
- int k=0;
- for(int i=P->y1;i<=P->y0;i++)
- {
- for(int j=P->x0;j<=P->x1;j++)
- {
- buf[k++]=BMPRec[i*ROW*3+j*3];
- buf[k++]=BMPRec[i*ROW*3+j*3+1];
- buf[k++]=BMPRec[i*ROW*3+j*3+2];
- }
- while((k*8)%32!=0)
- buf[k++]=0;
- }
- return ForLPR;
- }
- void CImageObject::Binary(BITMAPINFO DIBINFO,BYTE buf[])
- {
- BYTE GrayLevel;
- int i,j,k;
- int w = DIBINFO.bmiHeader.biWidth;
- int h = DIBINFO.bmiHeader.biHeight;
- for(i = 0; i < h ;i++)
- {
- for(j = 0; j < w;j++)
- {
- k = i*w+j;
- GrayLevel = BYTE(0.299*buf[k*3] + 0.587*buf[k*3+1] + 0.114 * buf[k*3+2]);
- if(GrayLevel<120)
- {
- buf[k*3] = 0;
- buf[k*3+1] = 0;
- buf[k*3+2] = 0;
- }
- else
- {
- buf[k*3] = 255;
- buf[k*3+1] = 255;
- buf[k*3+2] = 255;
- }
- }
- }
- }