图像拼接技术Doc.cpp
资源名称:txpj.rar [点击查看]
上传用户:pureled
上传日期:2013-05-27
资源大小:1078k
文件大小:21k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // 图像拼接技术Doc.cpp : implementation of the CMyDoc class
- //
- #include "stdafx.h"
- #include "图像拼接技术.h"
- #include "图像拼接技术Doc.h "
- #include "windowsx.h"
- #include "Start.h"
- #include "Other.h"
- #include "ZuoBiao.h"
- #include "Weizhi.h"
- #include "Wuizhi2.h"
- #include "Weizhi3.h"
- #include "RotateAngle.h"
- #include <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #define PI 3.1415926535
- #define RADIAN(angle) ((angle)*PI/180.0)
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc
- IMPLEMENT_DYNCREATE(CMyDoc, CDocument)
- BEGIN_MESSAGE_MAP(CMyDoc, CDocument)
- //{{AFX_MSG_MAP(CMyDoc)
- ON_COMMAND(ID_FILE_OTHEROPEN, OnFileOtheropen)
- ON_COMMAND(ID_PINJIE, OnPinjie)
- ON_COMMAND(ID_SUANFA1, OnSuanfa1)
- ON_COMMAND(ID_SUANFA2, OnSuanfa2)
- ON_COMMAND(ID_ROTATEPINJIE, OnRotatepinjie)
- ON_COMMAND(ID_SUANFA3, OnSuanfa3)
- ON_COMMAND(ID_CHUIZHIPINJIE, OnChuizhipinjie)
- ON_COMMAND(ID_SAVEBITMAP, OnSavebitmap)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc construction/destruction
- CMyDoc::CMyDoc()
- {
- // TODO: add one-time construction code here
- }
- CMyDoc::~CMyDoc()
- {
- }
- BOOL CMyDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc serialization
- void CMyDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc diagnostics
- #ifdef _DEBUG
- void CMyDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CMyDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMyDoc commands
- BOOL CMyDoc::OnOpenDocument(LPCTSTR lpszPathName)
- {
- if (!CDocument::OnOpenDocument(lpszPathName))
- return FALSE;
- // TODO: Add your specialized creation code here
- CFile file;
- LONG lBitsSize;
- HLOCAL hNew;
- if(file.Open(lpszPathName,CFile::modeRead)==NULL)
- {
- MessageBox(NULL,"打开文件失败!","提示",MB_OK);
- return FALSE;
- }
- file.Read(&lbmpHeader,sizeof(BITMAPFILEHEADER));
- if(lbmpHeader.bfType!=(WORD)(('M'<<8)|'B'))
- {
- MessageBox(NULL,"不是位图文件!","提示",MB_OK);
- file.Close();
- return FALSE;
- }
- lBitsSize=lbmpHeader.bfSize;
- lbmpInfo=(BITMAPINFO*)GlobalAllocPtr(GHND,lBitsSize-
- sizeof(BITMAPFILEHEADER));//开辟空间
- file.Read(lbmpInfo,lBitsSize-sizeof(BITMAPFILEHEADER));
- if((lbmpInfo->bmiHeader.biBitCount!=8)||(lbmpInfo->bmiHeader.biCompression!=BI_RGB))
- {
- MessageBox(NULL,"不是256色位图或是压缩的图像","提示",MB_OK);
- lbmpInfo=NULL;
- file.Close();
- return FALSE;
- }
- size.cx=lbmpInfo->bmiHeader.biWidth;
- size.cy=lbmpInfo->bmiHeader.biHeight;
- rgbPal=(LPBYTE)lbmpInfo+sizeof(BITMAPINFOHEADER);
- lpBits=(LPBYTE)lbmpInfo+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
- TrueWidth=(size.cx+3)/4*4;
- file.Close();
- //获取第一图的位置
- Start XY;
- XY.DoModal();
- m_x=XY.m_x;
- m_y=XY.m_y;
- //分配内存
- hNew=LocalAlloc(LHND,size.cy*TrueWidth);
- Lp1=(BYTE*)LocalLock(hNew);
- //存放第一幅图像
- memcpy(Lp1,lpBits,size.cy*TrueWidth);
- return TRUE;
- }
- void CMyDoc::ShowBitmap(CDC *pDC)
- {
- ::StretchDIBits(pDC->m_hDC,m_x,m_y,size.cx,size.cy,0,0,
- size.cx,size.cy,lpBits,lbmpInfo,DIB_RGB_COLORS,SRCCOPY);
- }
- void CMyDoc::OnFileOtheropen()
- {
- // TODO: Add your command handler code here
- CString FilePath;
- CFile file;
- BITMAPFILEHEADER lbmpHeader;
- LONG lBitsSize;
- HLOCAL hNew;
- CFileDialog dlgOpen(TRUE,".bmp",FilePath,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
- "位图文件(*.bmp)|*.bmp||");
- if(dlgOpen.DoModal()==IDOK)
- FilePath=dlgOpen.GetPathName();
- if(file.Open(FilePath,CFile::modeRead)==NULL)
- {
- MessageBox(NULL,"打开文件失败!","提示",MB_OK);
- return;
- }
- file.Read(&lbmpHeader,sizeof(BITMAPFILEHEADER));
- if(lbmpHeader.bfType!=(WORD)(('M'<<8)|'B'))
- {
- MessageBox(NULL,"不是位图文件!","提示",MB_OK);
- file.Close();
- return;
- }
- lBitsSize=lbmpHeader.bfSize;
- lbmpInfo2=(BITMAPINFO*)GlobalAllocPtr(GHND,lBitsSize-
- sizeof(BITMAPFILEHEADER));
- file.Read(lbmpInfo2,lBitsSize-sizeof(BITMAPFILEHEADER));
- if((lbmpInfo2->bmiHeader.biBitCount!=8)||(lbmpInfo2->bmiHeader.biCompression!=BI_RGB))
- {
- MessageBox(NULL,"不是256色位图或是压缩的图像","提示",MB_OK);
- lbmpInfo=NULL;
- file.Close();
- return;
- }
- size2.cx=lbmpInfo2->bmiHeader.biWidth;
- size2.cy=lbmpInfo2->bmiHeader.biHeight;
- lpBits2=(LPBYTE)lbmpInfo2+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
- TrueWidth2=(size2.cx+3)/4*4;
- file.Close();
- //获取第二幅图的位置
- Other XY;
- XY.DoModal();
- m2_x=XY.m_x;
- m2_y=XY.m_y;
- // 分配内存
- hNew=LocalAlloc(LHND,size2.cy*TrueWidth2);
- Lp2=(BYTE*)LocalLock(hNew);
- //存放第二幅图像
- memcpy(Lp2,lpBits2,size2.cy*TrueWidth2);
- UpdateAllViews(NULL);
- }
- void CMyDoc::ShowOther(CDC *pDC)
- {
- ::StretchDIBits(pDC->m_hDC,m2_x,m2_y,size2.cx,size2.cy,0,0,
- size2.cx,size2.cy,lpBits2,lbmpInfo2,DIB_RGB_COLORS,SRCCOPY);
- }
- void CMyDoc::Moban(int a,int b,int c,int d)
- {
- HLOCAL hNew;
- int i;
- int j;
- m3_x=a;
- m3_y=b;
- Height=c;
- Width=(d+3)/4*4;
- // 分配内存
- hNew=LocalAlloc(LHND,Height*Width);
- Lp3=(BYTE*)LocalLock(hNew);
- for (i=0;i<Height;i++)
- for (j=0;j<Width;j++)
- {
- Lp3[i*Width+j]=lpBits2[(size2.cy-m3_y-Height+i)*TrueWidth2+m3_x+j];
- }
- }
- /*void CMyDoc::TemplateMatch()
- {
- int i;
- int j;
- int m;
- int n;
- //中间结果
- double dsst;
- double dss;
- double dst;
- //相似性测度
- double R;
- //最大相似性测度
- double maxR;
- //最大相似性出现的位置
- int maxWidth;
- int maxHeight;
- dst=0;
- for (n=0;n<Height;n++)
- {
- for (m=0;m<Width;m++)
- {
- dst+=(double)DataTemp[n][m];
- }
- }
- maxR=0.0;
- for(j=0;j<m_y-Height+1;j++)
- {
- for(i=0;i<m_x-Width+1;i++)
- {
- dsst=0;
- dss=0;
- for(n=0;n<Height;n++)
- {
- for(m=0;m<Width;m++)
- {
- dss+=DataMap1[j+n][i+m];
- dsst+=DataTemp[n][m];
- }
- }
- //计算相似性
- R=dsst/(sqrt(dss)*sqrt(dst));
- //与最大相似性比较
- if(R>maxR)
- {
- maxR=R;
- maxWidth=i;
- maxHeight=j;
- }
- }
- }
- Match_x=maxWidth;
- Match_y=maxHeight;
- }*/
- void CMyDoc::OnPinjie()
- {
- // TODO: Add your command handler code here
- //预处理?
- HLOCAL hNew;
- int L;
- L=size.cx-Match_x+m3_x;
- //分配空间
- hNew=LocalAlloc(LHND,size.cy*((2*size.cx-L+3)/4*4));
- NewP=(BYTE*)LocalLock(hNew);
- //进行拼接
- for(int j=0;j<size.cy;j++)
- for(int i=0;i<TrueWidth;i++)
- {
- NewP[j*((2*size.cx-L+3)/4*4)+i]=Lp1[j*TrueWidth+i];
- }
- for(int n=0;n<size2.cy;n++)
- for(int m=L;m<TrueWidth2;m++)
- {
- NewP[n*((2*size.cx-L+3)/4*4)+TrueWidth2+m-L]=Lp2[n*TrueWidth2+m];
- }
- m_bmpHead->bfSize=(DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFO)+
- 256*sizeof(RGBQUAD)+(DWORD)(size.cy*((2*size.cx-L+3)/4*4)));
- m_bmpInfo=(BITMAPINFO*)GlobalAllocPtr(GHND,m_bmpHead->bfSize-sizeof(BITMAPFILEHEADER));
- memcpy(m_bmpInfo,lbmpInfo,m_bmpHead->bfSize-sizeof(BITMAPFILEHEADER));
- m_bmpInfo->bmiHeader.biWidth=(2*size.cx-L+3)/4*4;
- m_bmpInfo->bmiHeader.biHeight=size.cy;
- m_bmpInfo->bmiHeader.biSizeImage=size.cy*((2*size.cx-L+3)/4*4);
- //释放内存
- UpdateAllViews(NULL);
- }
- void CMyDoc::ShowNew(CDC *pDC)
- {
- ::StretchDIBits(pDC->m_hDC,0,500,m_bmpInfo->bmiHeader.biWidth,m_bmpInfo->bmiHeader.biHeight,0,0,
- m_bmpInfo->bmiHeader.biWidth,m_bmpInfo->bmiHeader.biHeight,NewP,m_bmpInfo,DIB_RGB_COLORS,SRCCOPY);
- }
- void CMyDoc::OnSuanfa1()
- {
- // TODO: Add your command handler code here
- int a1,a2,a3,a4;
- CZuoBiao XY;
- XY.DoModal();
- a1=XY.mx;
- a2=XY.my;
- x1=XY.mx;
- y1=XY.my;
- a3=XY.h;
- a4=XY.w;
- // 更改光标形状
- BeginWaitCursor();
- //获取模板图像
- Moban(a1,a2,a3,a4);
- int i;
- int j;
- int m;
- int n;
- //中间结果
- double dsst;
- double dss;
- double dst;
- //相似性测度
- double R;
- //最大相似性测度
- double maxR;
- //最大相似性出现的位置
- int maxWidth;
- int maxHeight;
- dst=0;
- for (n=0;n<Height;n++)
- {
- for (m=0;m<Width;m++)
- {
- dst+=(double)Lp3[n*Width+m]*Lp3[n*Width+m];
- }
- }
- maxR=0.0;
- for(j=0;j<size.cy-Height+1;j++)
- {
- for(i=0;i<TrueWidth-Width+1;i++)
- {
- dsst=0;
- dss=0;
- for(n=0;n<Height;n++)
- {
- for(m=0;m<Width;m++)
- {
- dss+=Lp1[(j+n)*TrueWidth+i+m]*Lp1[(j+n)*TrueWidth+i+m];
- dsst+=Lp3[n*Width+m]*Lp1[(j+n)*TrueWidth+i+m];
- }
- }
- //计算相似性
- R=dsst/(sqrt(dss)*sqrt(dst));
- //与最大相似性比较
- if(R>maxR)
- {
- maxR=R;
- maxWidth=i;
- maxHeight=size.cy-Height-j;
- }
- }
- }
- Match_x=maxWidth;
- Match_y=maxHeight;
- Weizhi d;
- d.match_x=Match_x;
- d.match_y=Match_y;
- d.DoModal();
- // 恢复光标形状
- EndWaitCursor();
- UpdateAllViews(NULL);
- }
- void CMyDoc::OnSuanfa2()
- {
- // TODO: Add your command handler code here
- //定义旋转变量
- //float srcX1,srcY1,srcX2,srcY2,srcX3,srcY3,srcX4,srcY4;
- //float dstX1,dstY1,dstX2,dstY2,dstX3,dstY3,dstX4,dstY4;
- float sina,cosa;
- //float num1,num2;
- float RotateAngle;
- //int Wnew,Hnew;
- int k;
- int x0,y0,x2,y2;
- float x1,y1,x,y;
- double zj;
- int a1,a2,a3,a4;
- CZuoBiao XY;
- XY.DoModal();
- a1=XY.mx;
- a2=XY.my;
- a3=XY.h;
- a4=XY.w;
- // 更改光标形状
- BeginWaitCursor();
- //获取模板图像
- Moban(a1,a2,a3,a4);
- int i;
- int j;
- int n;
- int m;
- //中间结果
- double dsst;
- double dss;
- double dst;
- //相似性测度
- double R;
- //最大相似性测度
- double maxR;
- double Max;
- //最大相似性出现的位置
- int maxWidth;
- int maxHeight;
- int matchWidth;
- int matchHeight;
- dst=0;
- for (n=0;n<Height;n++)
- {
- for (m=0;m<Width;m++)
- {
- dst+=(double)Lp3[n*Width+m]*Lp3[n*Width+m];
- }
- }
- maxR=0.0;
- Max=0.0;
- //以0。1度步进来旋转
- for (k=1;k<=30;k++)
- {
- RotateAngle=(float)RADIAN(k);
- cosa=(float)cos((double)RotateAngle);
- sina=(float)sin((double)RotateAngle);
- for(j=50;j<size.cy-Height+1;j++)
- {
- for(i=50;i<TrueWidth-Width+1;i++)
- {
- dsst=0;
- dss=0;
- for(n=0;n<Height;n++)
- {
- for(m=0;m<Width;m++)
- {
- x0=j+n;
- y0=i+m;
- //x1=x0*cosa-y0*sina+num1;
- //y1=x0*sina-y0*cosa+num2;
- x1=(x0-i)*cosa-(y0-j)*sina+i;
- y1=(x0-i)*sina+(y0-j)*cosa+j;
- x2=(int)(x1*10/10);
- y2=(int)(y1*18/10);
- x=x1-x2;
- y=y1-y2;
- /*zj=(Lp1[x2*TrueWidth+y2+1]-Lp1[x2*TrueWidth+y2])*y+
- (Lp1[(x2+1)*TrueWidth+y2]-Lp1[x2*TrueWidth+y2])*x+
- (Lp1[(x2+1)*TrueWidth+y2+1]+Lp1[x2*TrueWidth+y2]-
- Lp1[x2*TrueWidth+y2+1]-Lp1[(x2+1)*TrueWidth+y2])*x*y+
- Lp1[x2*TrueWidth+y2];*/
- zj=Lp1[x2*TrueWidth+y2];
- dss+=zj*zj;
- dsst+=Lp3[n*Width+m]*zj;
- }
- }
- //计算相似性
- R=dsst/(sqrt(dss)*sqrt(dst));
- //与最大相似性比较
- if(R>maxR)
- {
- maxR=R;
- maxWidth=i;
- maxHeight=size.cy-Height-j;
- }
- }
- }
- if(maxR>Max)
- {
- Max=maxR;
- matchWidth=maxWidth;
- matchHeight=maxHeight;
- matchAngle=(float)(k);
- }
- }
- //获得最佳匹配位置
- Match_x=maxWidth;
- Match_y=maxHeight;
- MatchRotateAngle=matchAngle;
- Wuizhi2 e;
- e.match2_x=Match_x;
- e.match2_y=Match_y;
- e.angle=MatchRotateAngle;
- e.DoModal();
- // 恢复光标形状
- EndWaitCursor();
- UpdateAllViews(NULL);
- }
- void CMyDoc::OnRotatepinjie()
- {
- // TODO: Add your command handler code here
- float srcX1,srcY1,srcX2,srcY2,srcX3,srcY3,srcX4,srcY4;
- float dstX1,dstY1,dstX2,dstY2,dstX3,dstY3,dstX4,dstY4;
- float sina,cosa;
- float num1,num2;
- float Rotateangle;
- int Wnew,Hnew,TrueWnew;
- int i,j,i0,j0;
- int k,l;
- int L;
- int value;
- int a;
- int bb;
- // float a,b;
- HLOCAL htemp;
- HLOCAL yuantu;
- HLOCAL hNew;
- LPBYTE lpPtr;
- LPBYTE lpsrc;
- RotateAngle A;
- A.DoModal();
- bb=A.Angle;
- Rotateangle=float(360-bb); //360-MatchRotateAngle;
- switch (bb)
- { case 1: value=5; a=0;break;
- case 2: value=7; a=0;break;
- case 4: value=15;a=0;break;
- case 3: value=12;a=0;break;
- case 5: value=20;a=0;break;
- case 6: value=26;a=0;break;
- case 7: value=28;a=0;break;
- case 8: value=32;a=0;break;
- case 9: value=37;a=0;break;
- case 10: value=41;a=0;break;
- case 11: value=44;a=0;break;
- case 12: value=49;a=0;break;
- case 13: value=52;a=0;break;
- case 14: value=54;a=0;break;
- case 15: value=56;a=0;break;
- case 16: value=60;a=0;break;
- case 17: value=65;a=0;break;
- case 18: value=70;a=0;break;
- case 19: value=72;a=0;break;
- case 20: value=74;a=0;break;
- case 21: value=77;a=0;break;
- case 22: value=79;a=0;break;
- case 23: value=82;a=0;break;
- case 24: value=85;a=0;break;
- case 25: value=89;a=0;break;
- case 26: value=92;a=0;break;
- case 27: value=95;a=0;break;
- case 28: value=98;a=0;break;
- case 29: value=102;a=0;break;
- case 30: value=105;a=3;break;
- }
- cosa=(float)cos((double)RADIAN(Rotateangle));
- sina=(float)sin((double)RADIAN(Rotateangle));
- /* cosa=(float)(((Match_x-Match3_x)*(x1-x2)+(Match_y-Match3_x)*(y1-y2))/((x1-x2)*(x1-x2)+
- (y1-y2)*(y1-y2)));
- sina=(float)(((Match_x-Match3_x)*(y1-y2)-(x1-x2)*(Match_y-Match3_x))/((x1-x2)*(x1-x2)+
- (y1-y2)*(y1-y2)));*/
- // a=acos(cosa);
- // b=asin(sina);//旋转2度是7,5度是20,30度是104,10度是41,20度是79,30度是104
- //原图的4个角点的坐标
- srcX1=(float)(-(TrueWidth-1)/2);
- srcY1=(float)( (size.cy-1)/2);
- srcX2=(float)( (TrueWidth-1)/2);
- srcY2=(float)( (size.cy-1)/2);
- srcX3=(float)(-(TrueWidth-1)/2);
- srcY3=(float)(-(size.cy-1)/2);
- srcX4=(float)( (TrueWidth-1)/2);
- srcY4=(float)(-(size.cy-1)/2);
- //新图4个角点的坐标
- dstX1= cosa*srcX1+sina*srcY1;
- dstY1=-sina*srcX1+cosa*srcY1;
- dstX2= cosa*srcX2+sina*srcY2;
- dstY2=-sina*srcX2+cosa*srcY2;
- dstX3= cosa*srcX3+sina*srcY3;
- dstY3=-sina*srcX3+cosa*srcY3;
- dstX4= cosa*srcX4+sina*srcY4;
- dstY4=-sina*srcX4+cosa*srcY4;
- //新图的高和宽
- Wnew=(int)(max(fabs(dstX4-dstX1),fabs(dstX3-dstX2))+0.5);
- Hnew=(int)(max(fabs(dstY4-dstY1),fabs(dstY3-dstY2))+0.5);
- //真实宽度
- TrueWnew=(Wnew+3)/4*4;
- num1=(float)(-0.5*(Wnew-1)*cosa-0.5*(Hnew-1)*sina+0.5*(TrueWidth-1));
- num2=(float)( 0.5*(Wnew-1)*sina-0.5*(Hnew-1)*cosa+0.5*(size.cy-1));
- //分配空间
- htemp=LocalAlloc(LHND,Hnew*TrueWnew);
- lpPtr=(BYTE*)LocalLock(htemp);
- //内存中的象素都是白色
- //memcpy(lpPtr,BYTE(255),Hnew*TrueWnew);
- for(i=0;i<Hnew;i++)
- {
- for(j=0;j<Wnew;j++)
- {
- //计算在原图中的坐标
- i0=(int)(-((float)j)*sina+((float)i)*cosa+num2+0.5);
- j0=(int)( ((float)j)*cosa+((float)i)*sina+num1+0.5);
- //判断是否在原图范围内
- if((j0>=0)&&(j0<TrueWidth)&&(i0>=0)&&(i0<size.cy))
- {
- //复制图象
- lpPtr[TrueWnew*(Hnew-1-i)+j]=Lp1[TrueWidth*(size.cy-1-i0)+j0];
- }
- else
- {
- //对于图中没有的图象,直接赋为255
- lpPtr[TrueWnew*(Hnew-1-i)+j]=255;
- }
- }
- }
- // 分配内存
- yuantu=LocalAlloc(LHND,size2.cy*TrueWidth2);
- lpsrc=(BYTE*)LocalLock(yuantu);
- for (k=0;k<size2.cy;k++)
- for (l=0;l<TrueWidth2;l++)
- {
- lpsrc[k*TrueWidth2+l]=lpPtr[(Hnew-value-size2.cy+k)*TrueWnew+value+l];
- }
- //旋转2度是7,5度是20,30度是104,10度是41,20度是79,30度是104
- //L=size.cx-Match_x+m3_x;
- L=128;
- //分配空间
- hNew=LocalAlloc(LHND,size2.cy*((2*size2.cx-L+3)/4*4));
- NewP=(BYTE*)LocalLock(hNew);
- //进行拼接
- for(int p=0;p<size2.cy;p++)
- for(int q=0;q<TrueWidth2;q++)
- {
- NewP[p*((2*size2.cx-L+3)/4*4)+q]=lpsrc[p*TrueWidth2+q];
- }
- for(int n=0;n<size2.cy;n++)
- for(int m=L;m<TrueWidth2;m++)
- {
- NewP[n*((2*size2.cx-L+3)/4*4)+TrueWidth2+m-L]=Lp2[(n+a)*TrueWidth2+m];
- }
- //开辟空间
- m_bmpHead=(BITMAPFILEHEADER*)GlobalAllocPtr(GHND,sizeof(BITMAPFILEHEADER));
- memcpy(m_bmpHead,&lbmpHeader,sizeof(BITMAPFILEHEADER));
- m_bmpHead->bfOffBits=(DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFO)+256*sizeof(RGBQUAD));
- m_bmpHead->bfSize=(DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFO)+
- 256*sizeof(RGBQUAD)+(DWORD)(size2.cy*((2*size2.cx-L+3)/4*4)));
- m_bmpInfo=(BITMAPINFO*)GlobalAllocPtr(GHND,m_bmpHead->bfSize-sizeof(BITMAPFILEHEADER));
- memcpy(m_bmpInfo,lbmpInfo,m_bmpHead->bfSize-sizeof(BITMAPFILEHEADER));
- m_bmpInfo->bmiHeader.biWidth=(2*size2.cx-L+3)/4*4;
- m_bmpInfo->bmiHeader.biHeight=size2.cy;
- m_bmpInfo->bmiHeader.biSizeImage=size2.cy*((2*size2.cx-L+3)/4*4);
- // lpBits=(LPBYTE)lbmpInfo+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
- // memcpy(lpBits,NewP,lbmpInfo->bmiHeader.biSizeImage);
- // size.cx=lbmpInfo->bmiHeader.biWidth;
- // size.cy=lbmpInfo->bmiHeader.biHeight;
- //释放内存
- UpdateAllViews(NULL);
- }
- void CMyDoc::OnSuanfa3()
- {
- // TODO: Add your command handler code here
- int a1,a2,a3,a4;
- CZuoBiao XY;
- XY.DoModal();
- a1=XY.mx;
- a2=XY.my;
- x2=XY.mx;
- y2=XY.my;
- a3=XY.h;
- a4=XY.w;
- // 更改光标形状
- BeginWaitCursor();
- //获取模板图像
- Moban(a1,a2,a3,a4);
- int i;
- int j;
- int m;
- int n;
- //中间结果
- double dsst;
- double dss;
- double dst;
- //相似性测度
- double R;
- //最大相似性测度
- double maxR;
- //最大相似性出现的位置
- int maxWidth;
- int maxHeight;
- dst=0;
- for (n=0;n<Height;n++)
- {
- for (m=0;m<Width;m++)
- {
- dst+=(double)Lp3[n*Width+m]*Lp3[n*Width+m];
- }
- }
- maxR=0.0;
- for(j=0;j<size.cy-Height+1;j++)
- {
- for(i=0;i<TrueWidth-Width+1;i++)
- {
- dsst=0;
- dss=0;
- for(n=0;n<Height;n++)
- {
- for(m=0;m<Width;m++)
- {
- dss+=Lp1[(j+n)*TrueWidth+i+m]*Lp1[(j+n)*TrueWidth+i+m];
- dsst+=Lp3[n*Width+m]*Lp1[(j+n)*TrueWidth+i+m];
- }
- }
- //计算相似性
- R=dsst/(sqrt(dss)*sqrt(dst));
- //与最大相似性比较
- if(R>maxR)
- {
- maxR=R;
- maxWidth=i;
- maxHeight=size.cy-Height-j;
- }
- }
- }
- Match3_x=maxWidth;
- Match3_y=maxHeight;
- Weizhi3 f;
- f.match3_x=Match3_x;
- f.match3_y=Match3_y;
- f.DoModal();
- // 恢复光标形状
- EndWaitCursor();
- UpdateAllViews(NULL);
- }
- void CMyDoc::OnChuizhipinjie()
- {
- // TODO: Add your command handler code here
- //预处理?
- HLOCAL hNew;
- int L;
- L=size.cy-Match_y+m3_y;
- //分配空间
- hNew=LocalAlloc(LHND,TrueWidth*(2*size.cy-L));
- NewP=(BYTE*)LocalLock(hNew);
- //进行拼接
- for(int j=0;j<size.cy;j++)
- for(int i=0;i<TrueWidth;i++)
- {
- NewP[j*TrueWidth+i]=Lp2[j*TrueWidth+i];
- }
- for(int n=L;n<size.cy;n++)
- for(int m=0;m<TrueWidth;m++)
- {
- NewP[(n+size.cy-L)*TrueWidth+m]=Lp1[n*TrueWidth+m];
- }
- m_bmpHead->bfSize=(DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFO)+
- 256*sizeof(RGBQUAD)+(DWORD)(TrueWidth*(2*size.cy-L)));
- m_bmpInfo=(BITMAPINFO*)GlobalAllocPtr(GHND,m_bmpHead->bfSize-sizeof(BITMAPFILEHEADER));
- memcpy(m_bmpInfo,lbmpInfo,m_bmpHead->bfSize-sizeof(BITMAPFILEHEADER));
- m_bmpInfo->bmiHeader.biWidth=TrueWidth;
- m_bmpInfo->bmiHeader.biHeight=2*size.cy-L;
- m_bmpInfo->bmiHeader.biSizeImage=TrueWidth*(2*size.cy-L);
- //释放内存
- UpdateAllViews(NULL);
- }
- /*void CMyDoc::DeleteContents()
- {
- // TODO: Add your specialized code here and/or call the base class
- int liCount=m_oalines.GetSize;
- int liPos;
- if(liCount)
- {
- for (liPos=0;liPos<liCount;liPos++)
- delete m_oaLines[liPos];
- m_oaLines.RemoveAll();
- }
- CDocument::DeleteContents();
- }*/
- //DEL BOOL CMyDoc::OnSaveDocument(LPCTSTR lpszPathName)
- //DEL {
- //DEL // TODO: Add your specialized code here and/or call the base class
- //DEL CFile fp;
- //DEL fp.Open(lpszPathName,CFile::modeWrite|CFile::modeCreate);
- //DEL fp.Write((LPSTR)&lbmpHeader,sizeof(BITMAPFILEHEADER));
- //DEL fp.Write(lpBits,lbmpHeader.bfSize-sizeof(BITMAPFILEHEADER));
- //DEL fp.Close();
- //DEL
- //DEL
- //DEL SetModifiedFlag(FALSE);
- //DEL return TRUE;
- //DEL
- //DEL //return CDocument::OnSaveDocument(lpszPathName);
- //DEL }
- void CMyDoc::SaveBitmap()
- {
- CFile fp;
- CFileDialog dlg(NULL,"BMP","*.bmp");
- if(dlg.DoModal()==IDOK)
- {
- fp.Open(dlg.GetPathName(),CFile::modeWrite|CFile::modeCreate);
- fp.Write(m_bmpHead,sizeof(BITMAPFILEHEADER));
- fp.Write(m_bmpInfo,sizeof(BITMAPINFO));
- fp.Write(rgbPal,256*sizeof(RGBQUAD));
- fp.Write(NewP,m_bmpHead->bfSize-sizeof(BITMAPFILEHEADER));
- }
- else
- return;
- fp.Close();
- }
- void CMyDoc::OnSavebitmap()
- {
- // TODO: Add your command handler code here
- SaveBitmap();
- }
- BOOL CMyDoc::OnSaveDocument(LPCTSTR lpszPathName)
- {
- // TODO: Add your specialized code here and/or call the base class
- return CDocument::OnSaveDocument(lpszPathName);
- }