您的位置:首页 > 其它

边缘检测-Prewitt 算子

2010-12-09 14:32 183 查看

边缘检测-Prewitt 算子

  Prewitt 算子采用以下算子分别计算一阶 x 方向和 y 方向的图像差分:

-101
-101
-101
-1-1-1
000
111
#include <math.h>
// Prewitt 算子
// 1. pImageData   图像数据
// 2. nWidth       图像宽度
// 3. nHeight      图像高度
// 4. nWidthStep   图像行大小
bool Prewitt(unsigned char *pImageData, int nWidth, int nHeight, int nWidthStep)
{
int i = 0;
int j = 0;
int dx = 0;
int dy = 0;
int nValue = 0;
unsigned char *pLine[3] = { NULL, NULL, NULL };
for (j = 1; j < nHeight - 1; j++)
{
pLine[0] = pImageData + nWidthStep * (j - 1);
pLine[1] = pImageData + nWidthStep * j;
pLine[2] = pImageData + nWidthStep * (j + 1);
for (i = 1; i < nWidth - 1; i++)
{
dx =
pLine[0][i+1] - pLine[0][i-1] +
pLine[1][i+1] - pLine[1][i-1] +
pLine[2][i+1] - pLine[2][i-1];
dy =
pLine[2][i-1] - pLine[0][i-1] +
pLine[2][i]   - pLine[0][i]   +
pLine[2][i+1] - pLine[0][i+1];
nValue = (int) sqrt((float) (dx * dx + dy * dy));
if (nValue > 0xFF)
{
nValue = 0xFF;
}
pLine[0][i-1] = (unsigned char) nValue;
}
}
return true;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: