轮廓提取算法_第1页
轮廓提取算法_第2页
轮廓提取算法_第3页
轮廓提取算法_第4页
轮廓提取算法_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、轮廓提取算法(contour extraction)  轮廓提取的目的就是获得图像的外部特征,是进行图像分析的有用手段之一.二值图像轮廓提取的算法非常简单,就是掏空内部点:如果原图中有一点为黑,且它的8个相临点都是黑色时,则将该黑点删掉.下面是轮廓提取和跟踪的简单实现:/* 函数名称:*   ContourDIB()* 参数:*   LPSTR lpDIBBits    - 指向源DIB图像指针*   LONG  lWidth  

2、60;    - 源图像宽度(象素数,必须是4的倍数)*   LONG  lHeight      - 源图像高度(象素数)* 返回值:*   BOOL               - 运算成功返回TRUE,否则返回FALSE。* 说明:* 该函数用于对图像进行轮廓提取运算。* * 要求目标图像为只有0和25

3、5两个灰度值的灰度图像。*/BOOL WINAPI ContourDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)/ 指向源图像的指针LPSTR lpSrc;/ 指向缓存图像的指针LPSTR lpDst;/ 指向缓存DIB图像的指针LPSTR lpNewDIBBits;HLOCAL hNewDIBBits;/循环变量long i;long j;unsigned char n,e,s,w,ne,se,nw,sw;/像素值unsigned char pixel;/ 暂时分配内存,以保存新图像hNewDIBBits = LocalAlloc(LHND,

4、lWidth * lHeight);if (hNewDIBBits = NULL)  / 分配内存失败  return FALSE;/ 锁定内存lpNewDIBBits = (char * )LocalLock(hNewDIBBits);/ 初始化新分配的内存,设定初始值为255lpDst = (char *)lpNewDIBBits;memset(lpDst, (BYTE)255, lWidth * lHeight);for(j = 1; j <lHeight-1; j+)  for(i = 1;i <lWidth-1;

5、 i+)        / 指向源图像倒数第j行,第i个象素的指针      lpSrc = (char *)lpDIBBits + lWidth * j + i;      / 指向目标图像倒数第j行,第i个象素的指针      lpDst = (char *)lpNewDIBBits + lWidth * j + i;      /取得当前指针处的像素值,注意要转换为uns

6、igned char型   pixel = (unsigned char)*lpSrc;   /目标图像中含有0和255外的其它灰度值/   if(pixel != 255 && pixel != 0)/    return FALSE;   if(pixel = 0)       *lpDst = (unsigned char)0;    nw = (unsi

7、gned char)*(lpSrc + lWidth -1);    n  = (unsigned char)*(lpSrc + lWidth );    ne = (unsigned char)*(lpSrc + lWidth +1);    w = (unsigned char)*(lpSrc -1);    e = (unsigned char)*(lpSrc +1);   

8、60;sw = (unsigned char)*(lpSrc - lWidth -1);    s  = (unsigned char)*(lpSrc - lWidth );    se = (unsigned char)*(lpSrc - lWidth +1);    /如果相邻的八个点都是黑点    if(nw+n+ne+w+e+sw+s+se=0)     &#

9、160;   *lpDst = (unsigned char)255;         / 复制腐蚀后的图像memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight);/ 释放内存LocalUnlock(hNewDIBBits);LocalFree(hNewDIBBits);/ 返回return TRUE;/* 函数名称:*   TraceDIB()* 参数:*   LPSTR lpDIBBits 

10、0;  - 指向源DIB图像指针*   LONG  lWidth       - 源图像宽度(象素数,必须是4的倍数)*   LONG  lHeight      - 源图像高度(象素数)* 返回值:*   BOOL            

11、   - 运算成功返回TRUE,否则返回FALSE。* 说明:* 该函数用于对图像进行轮廓跟踪运算。* * 要求目标图像为只有0和255两个灰度值的灰度图像。*/BOOL WINAPI TraceDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)/ 指向源图像的指针LPSTR lpSrc;/ 指向缓存图像的指针LPSTR lpDst;/ 指向缓存DIB图像的指针LPSTR lpNewDIBBits;HLOCAL hNewDIBBits;/ 图像每行的字节数LONG lLineBytes;/循环变量long i;long j;/像素

12、值unsigned char pixel;/是否找到起始点及回到起始点bool bFindStartPoint;/是否扫描到一个边界点bool bFindPoint;/起始边界点与当前边界点Point StartPoint,CurrentPoint;/八个方向和起始扫描方向int Direction82=-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0;int BeginDirect;/ 计算图像每行的字节数lLineBytes = WIDTHBYTES(lWidth * 8);/ 暂时分配内存,以保存新图像hNewDIBBits = LocalAlloc(LHND,

13、 lLineBytes * lHeight);if (hNewDIBBits = NULL)  / 分配内存失败  return FALSE;/ 锁定内存lpNewDIBBits = (char * )LocalLock(hNewDIBBits);/ 初始化新分配的内存,设定初始值为255lpDst = (char *)lpNewDIBBits;memset(lpDst, (BYTE)255, lLineBytes * lHeight);/先找到最左上方的边界点bFindStartPoint = false;for (j = 0;j < lHeig

14、ht && !bFindStartPoint;j+)  for(i = 0;i < lWidth && !bFindStartPoint;i+)     / 指向源图像倒数第j行,第i个象素的指针      lpSrc = (char *)lpDIBBits + lLineBytes * j + i;      /取得当前指针处的像素值,注意要转换为unsigned char型   pixel =

15、(unsigned char)*lpSrc;      if(pixel = 0)       bFindStartPoint = true;    StartPoint.Height = j;    StartPoint.Width = i;    / 指向目标图像倒数第j行,第i个象素的指针       lpDst

16、 = (char *)lpNewDIBBits + lLineBytes * j + i;     *lpDst = (unsigned char)0;       /由于起始点是在左下方,故起始扫描沿左上方向BeginDirect = 0;/跟踪边界bFindStartPoint = false;/从初始点开始扫描CurrentPoint.Height = StartPoint.Height;CurrentPoint.Width = StartPoint.Width;while(!bFindS

17、tartPoint)  bFindPoint = false;  while(!bFindPoint)     /沿扫描方向查看一个像素   lpSrc = (char *)lpDIBBits + lLineBytes * ( CurrentPoint.Height + DirectionBeginDirect1)    + (CurrentPoint.Width + DirectionBeginDirect0);   pixel =

18、(unsigned char)*lpSrc;   if(pixel = 0)       bFindPoint = true;    CurrentPoint.Height = CurrentPoint.Height + DirectionBeginDirect1;    CurrentPoint.Width = CurrentPoint.Width + DirectionBeginDirect0;   &

19、#160;if(CurrentPoint.Height = StartPoint.Height && CurrentPoint.Width = StartPoint.Width)         bFindStartPoint = true;        lpDst = (char *)lpNewDIBBits + lLineBytes * CurrentPoint.Height + CurrentPoint.Width;    *lpDst = (unsigned char)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论