| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1772 人关注过本帖
标题:这个是实现将一个图片进行轮廓提取的算法
只看楼主 加入收藏
风弄无声
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2008-1-30
收藏
 问题点数:0 回复次数:3 
这个是实现将一个图片进行轮廓提取的算法
就是不知道这个算法到底能不能执行,有错了没有,因为本人不会VC++,可是这个算法对我来说很有用,有没有人还会把它转换成C#的呢?
**************************************
函数名称:
   ContourDIB();
参数:
   LPSTR lpDIBBits  -指向原DIB图象指针
   LONG  lWidth    -原图象宽度(像素,必须是4的倍数)
   LONG  lHeidht   -原图象高度(像素数)
返回值:
   BOOL     -运算成功返回true,否则返回flase
说明:
   该函数用于对图象进行轮廓提取运算
    要求目标图象为只有0和255两个灰度图象
*****************************************
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,lWidth*lHeight);
    if(hNewDIBBits==NULL)
    {
      return FALSE;     
    }
    lpNewDIBBits=(char *)LocalLock(hNewDIBBits);
    //初始化新分配的内存,设定初始值为255
    lpDst=(char *)lpNewDIBBits;
    memset(lpDst,(byte)255,lWidth*lHeight);
    for(j=1;j<lHeight-1;j++)
     {
         for(i=1;i<lWidth-1;i++)
           {
                //指向原图象倒数第J行,第I个像素的指针
                 lpSrc=(char *)lpDIBBits+lWidth*j+i;
                //指向目标图象倒数第J行,第I个像素的指针
                 lpDst=(char *)lpNewDIBBits+lWidth*j+i;
                //取得当前指针处的像素值,注意要转换为unsigned char型
                  pixel=(unsigned char)* lpSrc;
                 if(pixel==0)
                 {
                    *lpDst=(unsigned char)0;
                     nw=(unsigned 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);
                     sw=(unsigned char)*(lpSrc+lWidth-1);
                     s=(unsigned char)*(lpSrc+lWidth);
                     se=(unsigned char)*(lpSrc+lWidth+1);
                   //如果相邻的8个点都是黑点
                   if(nw+n+ne+w+e+sw+s+se==0)
                     *lpDst=(unsigned char)255;
                 }
            }
            //复制运算后的图象
             memcpy(lpDIBBits,lpNewDIBBits,lWidth*lHeight);
            //释放内存
             LocalUnlock(hNewDIBBits);
             LocalFree(hNewDIBBits);
          //返回
              return TRUE;
}

 
搜索更多相关主题的帖子: 轮廓 算法 
2008-12-02 20:57
wolong
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-2-24
收藏
得分:0 
轮廓提取是什么意思呢
2008-12-03 11:44
boiledwater
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-9-3
收藏
得分:0 
没有语法错误,仅供参考。

//private bool ContourDIB(LPSTR lpDIBBits,LONG lWidth,LONG lHeight)
        private bool ContourDIB(ref byte[] lpDIBBits, int lWidth, int lHeight)
        {
            try
            {
                ////指向原图象的指针
                //  LPSTR lpSrc;
                ////指向缓存图象的指针
                //  LPSTR lpDst;
                ////指向缓存DIB图象的指针
                //  LPSTR lpNewDIBBits;
                //  HLOCAL hNewDIBBits;
                ////循环变量
                //  long i;
                //  long j;
                byte n, e, s, w, ne, se, nw, sw;
                ////像素值
                byte pixel;
                ////暂时分配内存,以保存新图象
                //  hNewDIBBits=LocalAlloc(LHND,lWidth*lHeight);
                //byte[] hNewDIBBits = new byte[lWidth * lHeight];
                //byte[] lpSrc = lpDIBBits;
                byte[] lpDst = new byte[lWidth * lHeight];
                // if(hNewDIBBits==NULL)
                // {
                //   return FALSE;     
                // }
                // lpNewDIBBits=(char *)LocalLock(hNewDIBBits);
                // //初始化新分配的内存,设定初始值为255
                // lpDst=(char *)lpNewDIBBits;
                // memset(lpDst,(byte)255,lWidth*lHeight);

                for (int j = 1; j < lHeight - 1; j++)
                {
                    for (int i = 1; i < lWidth - 1; i++)
                    {
                        //指向原图象倒数第J行,第I个像素的指针
                        //lpSrc = lpDIBBits[lWidth * j + i];
                        //指向目标图象倒数第J行,第I个像素的指针
                        //lpDst = lpNewDIBBits[lWidth * j + i];

                        //取得当前指针处的像素值,注意要转换为unsigned char型
                        //pixel = *lpSrc;
                        pixel = lpDIBBits[lWidth * j + i];
                        if (pixel == 0)
                        {
                            //*lpDst = 0;
                            lpDst[lWidth * j + i] = 0;
                            //nw = *(lpSrc + lWidth - 1);
                            nw = lpDIBBits[lWidth * j + i + lWidth - 1];
                            //n = *(lpSrc + lWidth);
                            n = lpDIBBits[lWidth * j + i + lWidth];
                            //ne = *(lpSrc + lWidth + 1);
                            ne = lpDIBBits[lWidth * j + i + lWidth + 1];
                            //w = *(lpSrc - 1);
                            w = lpDIBBits[lWidth * j + i - 1];
                            //e = *(lpSrc + 1);
                            e = lpDIBBits[lWidth * j + i + 1];
                            //sw = *(lpSrc + lWidth - 1);
                            sw = lpDIBBits[lWidth * j + i + lWidth - 1];
                            //s = *(lpSrc + lWidth);
                            s = lpDIBBits[lWidth * j + i + lWidth];
                            //se = *(lpSrc + lWidth + 1);
                            se = lpDIBBits[lWidth * j + i + lWidth + 1];
                            //如果相邻的8个点都是黑点
                            if (nw + n + ne + w + e + sw + s + se == 0)
                                //*lpDst = 255;
                                lpDst[lWidth * j + i] = 255;
                        }
                    }
                }
                //复制运算后的图象
                //memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight);
                Buffer.BlockCopy(lpDst, 0, lpDIBBits, 0, lWidth * lHeight);
                //释放内存
                //LocalUnlock(hNewDIBBits);
                //LocalFree(hNewDIBBits);
                //返回
                return true;
            }
            catch { }
            return false;
        }
2008-12-03 12:56
boiledwater
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-9-3
收藏
得分:0 
不好意思,忘了初始化lpDst了。
2008-12-03 19:40
快速回复:这个是实现将一个图片进行轮廓提取的算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017249 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved