关于数字图像模板匹配问题
#include"windows.h"
#include<stdio.h>
unsigned char *pBmpBuf;/*cankao*/
int bmpWidth1;
int bmpHeight1;
int bmpWidth2;
int bmpHeight2;
int bmpWidthout;
int bmpHeightout;
RGBQUAD *pColorTable;
int biBitCount;
int min,minx,miny;
unsigned char *m_pimgdataout;
bool readBmp(char *bmpName);
bool saveBmp(char *bmpname,unsigned char *pBmpBuf,int width,int height,int bibitcount,RGBQUAD *pcolortable);
int main(void)
{
char readPath1[]="MOBAN.bmp";
readBmp(readPath1);
printf("width=%d,height=%d,biBitCount=%d\n",bmpWidth1,bmpHeight1,biBitCount);
char readPath2[]="cankaotu.bmp";
readBmp(readPath2);
printf("width=%d,height=%d,biBitCount=%d\n",bmpWidth2,bmpHeight2,biBitCount);
int i,j,i0,j0;
int sum;
int t1,t2;
int linebyte=(bmpWidth2+3)/4*4;
int blocklinebyte=(bmpWidth1+3)/4*4;
RGBQUAD *m_lpColorTableOut;
for(i=0;i<bmpHeight2-bmpHeight1;i+=3){
for(j=0;j<bmpWidth2-bmpHeight1;j+=3){
sum=0;
for(i0=0;i0<bmpHeight1;i0++){
for(j0=0;j0<bmpWidth1;j0++){
t1=*(pBmpBuf+(i+i0)*linebyte+j+j0);
t2=*(m_pimgdataout+i0*blocklinebyte+j0);
sum+=abs(t1-t2);
}
}
if(i==0&&j==0){
min=sum;
minx=0;
miny=0;
}
else{
if(min>sum){
min=sum;minx=j;miny=i;
}
}
}
}
return minx;
return miny;
int m_nColorTableLengthOut=1024;
if(m_nColorTableLengthOut!=0)
{
m_lpColorTableOut=new RGBQUAD[m_nColorTableLengthOut];
memcpy(m_lpColorTableOut,pColorTable,sizeof(RGBQUAD)*m_nColorTableLengthOut);
}
bmpWidthout=bmpWidth2;
bmpHeightout=bmpHeight2;
int linebyteout=(bmpWidth2*biBitCount/8+3)/4*4;
m_pimgdataout=new unsigned char[linebyteout*bmpHeightout];
for(int i=0;i<bmpWidth1;i++){
*(m_pimgdataout+(miny+0)*linebyteout+minx+i)=0;
*(m_pimgdataout+(miny+bmpHeight1-1)*linebyte+minx+i)=0;}
for(int i=0;i<bmpHeight1;i++){
*(m_pimgdataout+(miny+0)*linebyteout+minx+i)=0;
*(m_pimgdataout+(miny+i)*linebyteout+minx+bmpWidth1)=0;}
return true;
char writePath[]="55.bmp";
saveBmp(writePath,m_pimgdataout,bmpWidth2,bmpHeight2,biBitCount,m_lpColorTableOut);
delete []m_pimgdataout;
}
bool readBmp(char *bmpName)
{
FILE *fp=fopen(bmpName,"rb");
if(0==fp)
return 0;
fseek(fp,sizeof(BITMAPFILEHEADER),0);
BITMAPINFOHEADER head;
fread(&head,sizeof(BITMAPINFOHEADER),1,fp);
bmpWidth1=head.biWidth;
bmpHeight1=head.biHeight;
bmpWidth2=head.biWidth;
bmpHeight2=head.biHeight;
biBitCount=head.biBitCount;
int lineByte=(bmpWidth2*biBitCount/8+3)/4*4;
if(biBitCount==8)
{
pColorTable=new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
}
pBmpBuf=new unsigned char[lineByte*bmpHeight2];//申请空间
fread(pBmpBuf,1,lineByte*bmpHeight2,fp);
fclose(fp);
return 1;
}
bool saveBmp(char *bmpname,unsigned char *imgbuf,int width,int height,int bibitcount,RGBQUAD *pcolortable)
{
if(!imgbuf)
return 0;
int colortablesize = 0;
if(bibitcount == 8)
colortablesize = 1024;
int linebyte = (width * bibitcount/8+3)/4*4;
FILE *fp = fopen(bmpname,"wb");
if(fp==0)
return 0;
BITMAPFILEHEADER fileHead;
fileHead.bfType = 0x4D42;
fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colortablesize + linebyte*height;
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;
fileHead.bfOffBits = 54+colortablesize;
fwrite(&fileHead,sizeof(BITMAPFILEHEADER),1,fp);
BITMAPINFOHEADER head;
head.biBitCount = bibitcount;
head.biClrImportant = 0;
head.biClrUsed = 0;
head.biCompression = 0;
head.biHeight = height;
head.biPlanes = 1;
head.biSize = 40;
head.biSizeImage = linebyte*height;
head.biWidth = width;
head.biXPelsPerMeter = 0;
head.biYPelsPerMeter = 0;
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp);
if(bibitcount == 8)
fwrite(pcolortable,sizeof(RGBQUAD),256,fp);
fwrite(imgbuf,height*linebyte,1,fp);
fclose(fp);
return 1;
}