2 qq 34801452 qq_34801452 于 2016.05.06 20:47 提问

图像边缘检测用VC如何实现 2C

这个程序我调不对,求大神帮忙 怎么实现把老师给的图检测出来,还有就是这个程序用VC建立什么类型的工程和文件

#define ff(x,y) pBmpBuf0[256*(y)+(x)]
#define gg(x,y) pBmpBuf9[256*(y)+(x)]
#include
#include
#include
#include
unsigned char *pBmpBuf;//读入图像数据的指针
int bmpWidth;//图像的宽
int bmpHeight;//图像的高
RGBQUAD *pColorTable;//颜色表指针
int biBitCount;//图像类型,每像素位
//读图像的位图数据,宽,高,颜色表及每像素位等数据进内存,存放在相应的全局变量中
bool readBmp(char*bmpName)
{
FILE*fp=fopen(bmpName,"rb");//二进制读方式打开指定的图像文件
if(fp==0)
return 0;
//跳过图像文件头结构BITMAPFILEHEADER
fseek(fp,sizeof(BITMAPFILEHEADER),0);
//定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中
BITMAPFILEHEADER head;
fread(&head,sizeof(BITMAPFILEHEADER),1,fp);//获取图像宽,高,每像素所占位数等信息
bmpWidth=head.biWidth;
bmpHeight=head.biHeight;
biBitCount=head.biBitCount;
//定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
int lineByte=(bmpWidth*biBitCount/8+3)/4*4;//灰度图像有颜色表,且颜色表项为256
if(biBitCount==8)
{
//申请颜色表所需要的空间,读颜色表进内存
pColorTable=new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
}
//申请位图数据所需要的空间,读位图数据内存
pBmpBuf=new unsigned char[lineByte*bmpHeight];
fread(pBmpBuf,1,lineByte*bmpHeight,fp);
fclose(fp);//关闭文件
return 1;//读取文件成功
}
//给定一个图像位图数据,宽,高,颜色表指针以及每像素所占的位数等信息,将其写到指定文件中
bool saveBmp (char*bmpName,unsigned char *imgBuf,int width,int height,int biBitCount,RGBQUAD*pColorTable)
{
//如果位图数据指针为0,则没有数据传入,函数返回
if(!imgBuf)
return 0;
//颜色表大小,以字节为单位,灰度图颜色表为1024字节
int colorTablesize=0;
if(biBitCount==8)
colorTablesize=1024;
//待存储图像数据每行字节数为4的倍数
int lineByte=(width*biBitCount/8+3)/4*4;
//以二进制写的方式打开文件
FILE*fp=fopen(bmpName,"wb");
if(fp==0) return 0;
//申请位图文件头结构变量,填写文件头信息
BITMAPFILEHEADER fileHead;
fileHead.bfType=0x4D42;//bmp类型
//bfsize是图像文件4个组成部分之和
fileHead.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+colorTablesize+lineByte*height;
fileHead.bfReservedl=0;
fileHead.bfReserved2=0;
//bfOffBits是图像文件前3个部分所需空间之和
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);
//如有灰度图像,有颜色表,写入文件
fwrite(pColorTable,sizeof(RGBQUAD),256,fp);
//写进位图数据进文件
fwrite(imgBuf,height*lineByte,1,fp);
//关闭文件
fclose(fp);
return 1;
}

//各边缘检测算子算法实现
// 函数声明
void roberts(unsigned char *pBmpBufroberts);
void sobel(unsigned char *pBmpBufsobel);
void robinson(unsigned char *pBmpBufrobinson);
void prewitt(unsigned char *pBmpBufprewitt);
void main()
{
//读入指定BMP文件进内存
char readPath[]="c:/text.BMP";
readBmp(readPath);
//输出图像信息
printf("width=%d,height=%d,biBitCount=%d\n",bmpWidth,bmpHeight,biBitCount);
//函数调用
roberts(pBmpBuf);
sobel(pBmpBuf);
robinson(pBmpBuf);
prewitt(pBmpBuf);
//清楚缓冲区,pBmpBuf和pcolorTable是全局变量在文件读入时申请的空间
delete []pBmpBuf;
delete []pColorTable;
}
//各函数定义
//roberts算子
void roberts(unsigned char *pBmpBufroberts)
{
unsigned char *pBmpBuf0;
unsigned char *pBmpBuf9[256*256];
int ix,iy,vx,vy,lx=256;
pBmpBuf0=pBmpBufroberts;
//模板1
for(ix=1;ix<255;ix++)
{
for(iy=1;iy<255;iy++)
{
vx=ff(ix+1,iy)-ff(ix,iy);
vy=ff(ix,iy+1)-ff(ix,iy);
gg(ix,iy)=abs(vx)+abs(vy);
}
}
//将数据图像存盘
char writePath[]="textroberts1.BMP";
saveBmp(writePath,pBmpBuf9,bmpWidth,bmpHeight,biBitCount,pColorTable);
//模板2
for(ix=1;ix<255;ix++)
for(iy=1;iy<255;iy++)
{
vx=ff(ix,iy)-ff(ix+1,iy+1);
vy=ff(ix+1,iy)-ff(ix,iy+1);
gg(ix,iy)=abs(vx)+abs(vy);
}
//将图像数据存盘
char writePath2[]="textroberts2.BMP";
saveBmp(writePath2,pBmpBuf9,bmpWidth,bmpHeight,biBitCount,pColorTable);
}

//robinson
void robinson(unsigned char *pBmpBufrobin)
{
unsigned char *pBmpBuf0;
pBmpBuf0=pBmpBufrobin;
unsigned char pBmpBuf9[256*256];
int ix,iy,mx,my,jx,jy,jpv,mxav,im;
static int mask [8][3][3]=
{1,2,1,0,0,0,-1,-2,-1,
0,1,2,-1,0,1,-2,-1,0,
-1,0,1,-2,0,2,-1,0,1,
-2,-1,0,-1,0,1,0,-1,2,
-1,-2,-1,0,0,0,1,2,1,
0,-1,-2,1,0,-1,2,1,0,
1,0,-1,2,0,2,1,0,-1,
2,1,01,0,-1,0,-1,-2,
};

for(ix=1;ix for(iy=1;iy {
for(im=0;im {
jpv=0;
mxav=0;
for(mx=0;mx {
jx=ix-1+mx;
for(my=0;my {
jy=iy-1+my;
jpv=jpv+ff(jx,jy)*mask[im][mx][my];
}
}
if((jpv>mxav)||(im==0))mxav=jpv;
}
gg(ix,iy)=mxav;
}

char writePath[]="textrobinson.BMP";
saveBmp(writePath,pBmpBuf9,bmpWidth,bmpHeight,biBitCount,pColorTable);
}

//prewitt
void prewitt(unsigned char *pBmpBufprewitt)
{
unsigned char *pBmpBuf0;
pBmpBuf0=pBmpBufprewitt;
unsigned char *pBmpBuf9[256*256];
int ix,iy,im,mx,my;
int jx,jy,jpv,mxav;
static int mask[8][3][3]=
{ 1,1,1,1,-2,1,-1,-1,-1,
1,1,1,-1,-2,1,-1,-1,1,
-1,1,1,-1,-2,1,-1,1,1,
-1,-1,-1,-1,-2,1,1,1,1,
1,-1,-1,1,-2,,1,1,1,1,
1,1,1,1,-2,1,-1,-1,-1,
1,1,-1,1,-2,-1,1,1,-1,
1,1,1,1,-2,-1,1,-1,-1,
};

for(ix=1;ix for(iy=1;iy {
for(im=0;im {
jpv=0;
mxav=0;
for(mx=0;mx {
jx=ix-1+mx;
for(my=0;my {
jy=iy-1+my;
jpv=jpv+ff(jx,jy)*mask[im][mx][my];
}
}
if((jpv>mxav)||(im==0))mxav=jpv;
}
gg(ix,iy)=mxav;
}
//将数据图像存盘
char writePath[]="textprewitt.BMP";
saveBmp(writePath,pBmpBuf9,bmpWidth,bmpHeight,biBitCount,pColorTable)
};

//sobel
void sobel(unsigned char *pBmpBufsobel)
{
unsigned char *pBmpBuf0;
unsigned char *pBmpBuf9[256*256];
int ix,iy,vx,vy,lx=256;
pBmpBuf0=pBmpBufsobel;
for(ix=1;ix<255;ix++)
{
for(iy=1;iy<255;iy++)
{
vx=(ff(ix-1,iy-1)+2*ff(ix-1,iy)+ff(ix-1,iy+1))-(ff(ix+1,iy-1)+2*ff(ix+1,iy)+ff(ix=1,iy+1));
vy=(ff(ix-1,iy-1)+2*ff(ix,iy-1)+ff(ix+1,iy-1))-(ff(ix-1,iy+1)+2*ff(ix,iy+1)+ff(ix+1,iy+1));
gg(ix,iy)=abs(vx)+abs(vy);
}
}

//将数据图像存盘

char writePath[]="textsobel.BMP";
saveBmp(writePath,pBmpBuf9,bmpWidth,bmpHeight,biBitCount,pColorTable);
}

2个回答

caozhy
caozhy   Ds   Rxr 2016.05.07 00:06
CSDNXIAON
CSDNXIAON   2016.05.09 11:53

Sobel算子边缘检测(vc实现)
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!