vb.net中边缘检测 边缘检测函数( 四 )


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);
//如果相邻的八个点都是黑点
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;
}
/*************************************************************************
*
* 函数名称:
*TraceDIB()
*
* 参数:
*LPSTR lpDIBBits- 指向源DIB图像指针
*LONGlWidth- 源图像宽度(象素数,必须是4的倍数)
*LONGlHeight- 源图像高度(象素数)
* 返回值:
*BOOL- 运算成功返回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;
//像素值
unsigned char pixel;
//是否找到起始点及回到起始点
bool bFindStartPoint;
//是否扫描到一个边界点
bool bFindPoint;
//起始边界点与当前边界点
Point StartPoint,CurrentPoint;
//八个方向和起始扫描方向
int Direction[8][2]={{-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, lLineBytes * lHeight);
if (hNewDIBBits == NULL)
{
// 分配内存失败
return FALSE;
}
// 锁定内存
lpNewDIBBits = (char * )LocalLock(hNewDIBBits);
// 初始化新分配的内存 , 设定初始值为255
lpDst = (char *)lpNewDIBBits;
memset(lpDst, (BYTE)255, lLineBytes * lHeight);
//先找到最左上方的边界点
bFindStartPoint = false;
for (j = 0;jlHeight!bFindStartPoint;j++)
{
for(i = 0;ilWidth!bFindStartPoint;i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = https://www.04ip.com/post/(char *)lpDIBBits + lLineBytes * j + i;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = (unsigned char)*lpSrc;
if(pixel == 0)
{
bFindStartPoint = true;
StartPoint.Height = j;
StartPoint.Width = i;
// 指向目标图像倒数第j行,第i个象素的指针
lpDst = (char *)lpNewDIBBits + lLineBytes * j + i;
*lpDst = (unsigned char)0;
}
}
}
//由于起始点是在左下方,故起始扫描沿左上方向
BeginDirect = 0;
//跟踪边界
bFindStartPoint = false;
//从初始点开始扫描
CurrentPoint.Height = StartPoint.Height;
CurrentPoint.Width = StartPoint.Width;
while(!bFindStartPoint)
{
bFindPoint = false;
while(!bFindPoint)
{
//沿扫描方向查看一个像素
lpSrc = https://www.04ip.com/post/(char *)lpDIBBits + lLineBytes * ( CurrentPoint.Height + Direction[BeginDirect][1])
+ (CurrentPoint.Width + Direction[BeginDirect][0]);

推荐阅读