关于数字图像模板匹配问题

jwy2014 2014-09-19 10:51:36
#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;
}
...全文
91 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jwy2014 2014-09-20
  • 打赏
  • 举报
回复
关键是这不是主要问题,我想问怎么出不来最终保存的照片“?
勤奋的小游侠 2014-09-19
  • 打赏
  • 举报
回复
看看,你readbmp里面都干了什么 bmpWidth1=head.biWidth; bmpHeight1=head.biHeight; bmpWidth2=head.biWidth; bmpHeight2=head.biHeight; 你这读二张图片还不是得到最后图片数据而已? 其它的错误就不帮你找了。
jwy2014 2014-09-19
  • 打赏
  • 举报
回复
为甚莫不能实现模板图与参考图匹配?求大神指点!!

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧