c语言提取图片的轮廓,这个是实现将一个图片进行轮廓提取的算法

没有语法错误,仅供参考。

//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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值