《sobel算子边缘检测》由会员分享,可在线阅读,更多相关《sobel算子边缘检测(6页珍藏版)》请在金锄头文库上搜索。
1、 源程序如下:#include#include #include #include #include #include #include #include #define ff(x,y) pBmpBuf0256*(y)+(x)#define gg(x,y) pBmpBuf9256*(y)+(x)/-unsigned char *pBmpBuf;/读入图像数据的指针int bmpWidth;/图像的宽int bmpHeight;/图像的高RGBQUAD *pColorTable;/颜色表指针int biBitCount;/图像类型,每像素位数/-/读图像的位图数据、宽、高、颜色表及每像素位数等数
2、据进内存,存放在相应的全局变量中bool readBmp(char *bmpName) FILE *fp=fopen(bmpName,rb);/二进制读方式打开指定的图像文件if(fp=0) return 0;/跳过位图文件头结构 BITMAPFILEHEADERfseek(fp, sizeof(BITMAPFILEHEADER),0);/定义位图信息头结构变量,读取位图信息头进内存,存放在变量 head 中BITMAPINFOHEADER head; fread(&head, sizeof(BITMAPINFOHEADER), 1,fp); /获取图像宽、高、每像素所占位数等信息bmpWid
3、th = head.biWidth;bmpHeight = head.biHeight;biBitCount = head.biBitCount;/定义变量,计算图像每行像素所占的字节数(必须是 4的倍数) int lineByte=(bmpWidth * biBitCount/8+3)/4*4;/灰度图像有颜色表,且颜色表表项为256if(biBitCount=8)/申请颜色表所需要的空间,读颜色表进内存pColorTable=new RGBQUAD256;fread(pColorTable,sizeof(RGBQUAD),256,fp);/申请位图数据所需要的空间,读位图数据进内存pBmp
4、Buf=new unsigned charlineByte * 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(!
5、imgBuf)return 0;/颜色表大小,以字节为单位,灰度图像颜色表为 1024 字节,彩色图像颜色表大小为0int 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 =
6、0x4D42;/bmp 类型/bfSize 是图像文件 4 个组成部分之和fileHead.bfSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ colorTablesize + lineByte*height;fileHead.bfReserved1 = 0;fileHead.bfReserved2 = 0;/bfOffBits 是图像文件前 3 个部分所需空间之和fileHead.bfOffBits=54+colorTablesize;/写文件头进文件fwrite(&fileHead, sizeof(BITMAPFILEH
7、EADER),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;/写位图信
8、息头进内存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;void sobel(unsigned char *pBmpBufsobel);void main() char readPath=C:UsersAdministratordesktop0.bm
9、p;readBmp(readPath);sobel(pBmpBuf);delete pBmpBuf;deletepColorTable;void sobel(unsigned char *pBmpBufsobel)unsigned char *pBmpBuf0;unsigned char pBmpBuf9256*256;pBmpBuf0=pBmpBufsobel;int i,j,vx=0,vy=0;for (i=1;i255;i+)for(j=1;j255;j+)vx=(ff(i-1,j-1)+2*ff(i-1,j)+ff(i-1,j+1)-(ff(i+1,j-1)+2*ff(i-1,j)+ff(i-1,j+1);vy=(ff(i-1,j-1)+2*ff(i,j-1)+ff(i+1,j-1) -(ff(i-1,j-1)+2*ff(i,j+1)+ff(i+1,j+1);gg(i,j)=abs(vx)+abs(vy);char writePath=C:UsersAdministratordesktop3.bmp;saveBmp(writePath,pBmpBuf9,bmpWidth,bmpHeight,biBitCount,pColorTable);运行前效果图: