收藏 分销(赏)

数学形态学的腐蚀和膨胀运算.doc

上传人:pc****0 文档编号:7563328 上传时间:2025-01-09 格式:DOC 页数:8 大小:32KB 下载积分:10 金币
下载 相关 举报
数学形态学的腐蚀和膨胀运算.doc_第1页
第1页 / 共8页
数学形态学的腐蚀和膨胀运算.doc_第2页
第2页 / 共8页


点击查看更多>>
资源描述
数学形态学的腐蚀和膨胀运算   "形态学"是描述动植物形态和结构的一门生物学分支,这里借用来指一种图像处理的方法.图像处理的形态学方法是一种"数学形态学"方法,用来提取图像成分,并据此来对图像区域的形状进行表示和描述.它的数学语言是集合理论,其中的集合代表图像中物体的形状.图像处理中常用的数学形态学方法包括腐蚀,膨胀,开,闭,边缘提取和图像细化.我依次给出Visual C++源代码: /************************************************************************* * * ErosionDIB() * * Parameters: * * HDIB hDib        - objective DIB handle * BOOL bHori     - erosion direction * * Return Value: * * BOOL             - True is success, else False * * Description: * * This function do erosion with the specified direction * ************************************************************************/ BOOL ErosionDIB(HDIB hDib, BOOL bHori) { // start wait cursor WaitCursorBegin();     // Old DIB buffer if (hDib == NULL) {   WaitCursorEnd();         return FALSE; } // only support 256 color image WORD wBitCount = DIBBitCount(hDib); if (wBitCount != 8) {   WaitCursorEnd();         return FALSE; } // new DIB HDIB hNewDIB = CopyHandle(hDib); if (! hNewDIB) {   WaitCursorEnd();         return FALSE; } // source dib buffer     LPBITMAPINFO lpSrcDIB = (LPBITMAPINFO)GlobalLock(hDib); if (! lpSrcDIB) {   WaitCursorBegin();   return FALSE; }     // New DIB buffer     LPBITMAPINFO lpbmi = (LPBITMAPINFO)GlobalLock(hNewDIB); if (! lpbmi) {   WaitCursorBegin();   return FALSE; } // start erosion... LPSTR lpPtr; LPSTR lpTempPtr; LONG  x,y; BYTE  num, num0; int   i; LONG lHeight = DIBHeight(lpSrcDIB); LONG lWidth = DIBWidth(lpSrcDIB); DWORD dwBufferSize = GlobalSize(lpSrcDIB); int nLineBytes = BytesPerLine(lpSrcDIB); if(bHori) {   for (y=0; y<lHeight; y++)   {    lpPtr=(char *)lpbmi+(dwBufferSize-nLineBytes-y*nLineBytes)+1;    lpTempPtr=(char *)lpSrcDIB+(dwBufferSize-nLineBytes-y*nLineBytes)+1;    for (x=1; x<lWidth-1; x++)    {     num0 = num = 0 ;     for(i=0;i<3;i++)     {      num=(unsigned char)*(lpPtr+i-1);      if(num > num0)       num0 = num;     }     *lpTempPtr=(unsigned char)num0;     /*     num=(unsigned char)*lpPtr;     if (num==0)     {      *lpTempPtr=(unsigned char)0;      for(i=0;i<3;i++)      {       num=(unsigned char)*(lpPtr+i-1);       if(num==255)       {        *lpTempPtr=(unsigned char)255;        break;       }      }     }     else      *lpTempPtr=(unsigned char)255;     */     lpPtr++;     lpTempPtr++;    }   } } else  // Vertical {   for (y=1; y<lHeight-1; y++)   {    lpPtr=(char *)lpbmi+(dwBufferSize-nLineBytes-y*nLineBytes);    lpTempPtr=(char *)lpSrcDIB+(dwBufferSize-nLineBytes-y*nLineBytes);    for (x=0; x<lWidth; x++)    {     num0 = num = 0 ;     for(i=0;i<3;i++)     {      num=(unsigned char)*(lpPtr+i-1);      if(num > num0)       num0 = num;     }     *lpTempPtr=(unsigned char)num0;     /*     num=(unsigned char)*lpPtr;     if (num==0)     {      *lpTempPtr=(unsigned char)0;      for(i=0;i<3;i++)      {       num=(unsigned char)*(lpPtr+(i-1)*nLineBytes);       if(num==255)       {        *lpTempPtr=(unsigned char)255;        break;       }      }     }     else      *lpTempPtr=(unsigned char)255;     */     lpPtr++;     lpTempPtr++;    }   } } // cleanup GlobalUnlock(hDib); GlobalUnlock(hNewDIB); GlobalFree(hNewDIB); WaitCursorEnd(); return TRUE; } /************************************************************************* * * DilationDIB() * * Parameters: * * HDIB hDib        - objective DIB handle * BOOL bHori     - dilation direction * * Return Value: * * BOOL             - True is success, else False * * Description: * * This function do dilation with the specified direction * ************************************************************************/ BOOL DilationDIB(HDIB hDib, BOOL bHori) { // start wait cursor WaitCursorBegin();     // Old DIB buffer if (hDib == NULL) {   WaitCursorEnd();         return FALSE; } // only support 256 color image WORD wBitCount = DIBBitCount(hDib); if (wBitCount != 8) {   WaitCursorEnd();         return FALSE; } // new DIB HDIB hNewDIB = CopyHandle(hDib); if (! hNewDIB) {   WaitCursorEnd();         return FALSE; } // source dib buffer     LPBITMAPINFO lpSrcDIB = (LPBITMAPINFO)GlobalLock(hDib); if (! lpSrcDIB) {   WaitCursorBegin();   return FALSE; }     // New DIB buffer     LPBITMAPINFO lpbmi = (LPBITMAPINFO)GlobalLock(hNewDIB); if (! lpbmi) {   WaitCursorBegin();   return FALSE; } // start erosion... LPSTR lpPtr; LPSTR lpTempPtr; LONG  x,y; BYTE  num, num0; int   i; LONG lHeight = DIBHeight(lpSrcDIB); LONG lWidth = DIBWidth(lpSrcDIB); DWORD dwBufferSize = GlobalSize(lpSrcDIB); int nLineBytes = BytesPerLine(lpSrcDIB); if(bHori) {   for(y=0;y<lHeight;y++)   {    lpPtr=(char *)lpbmi+(dwBufferSize-nLineBytes-y*nLineBytes)+1;    lpTempPtr=(char *)lpSrcDIB+(dwBufferSize-nLineBytes-y*nLineBytes)+1;    for(x=1;x<lWidth-1;x++)    {     num0 = num = 255;     for(i=0;i<3;i++)     {      num=(unsigned char)*(lpPtr+i-1);      if(num < num0)       num0 = num;     }     *lpTempPtr=(unsigned char)num0;     /*     num=(unsigned char)*lpPtr;     if (num==255)     {      *lpTempPtr=(unsigned char)255;      for(i=0;i<3;i++)      {       num=(unsigned char)*(lpPtr+i-1);       if(num==0)       {        *lpTempPtr=(unsigned char)0;        break;       }      }     }     else      *lpTempPtr=(unsigned char)0;     */     lpPtr++;     lpTempPtr++;    }   } } else {   for(y=1;y<lHeight-1;y++)   {    lpPtr=(char *)lpbmi+(dwBufferSize-nLineBytes-y*nLineBytes);    lpTempPtr=(char *)lpSrcDIB+(dwBufferSize-nLineBytes-y*nLineBytes);    for(x=0;x<lWidth;x++)    {     num0 = num = 255;     for(i=0;i<3;i++)     {      num=(unsigned char)*(lpPtr+i-1);      if(num < num0)       num0 = num;     }     *lpTempPtr=(unsigned char)num0;     /*     num=(unsigned char)*lpPtr;     if (num==255)     {      *lpTempPtr=(unsigned char)255;      for(i=0;i<3;i++)      {       num=(unsigned char)*(lpPtr+(i-1)*nLineBytes);       if(num==0)       {        *lpTempPtr=(unsigned char)0;        break;       }      }     }     else      *lpTempPtr=(unsigned char)0;     */     lpPtr++;     lpTempPtr++;    }   } } // cleanup GlobalUnlock(hDib); GlobalUnlock(hNewDIB); GlobalFree(hNewDIB); WaitCursorEnd(); return TRUE; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服