宜宾赵一曼纪念馆 地址:Prewitt 算子
来源:百度文库 编辑:中财网 时间:2024/05/09 02:37:46
标签:Prewitt 算子
Prewitt 算子
Prewitt 算子采用以下算子分别计算一阶 x 方向和 y 方向的图像差分:
-1 0 1
-1 0 1
-1 0 1
-1 -1 -1
0 0 0
1 1 1
#include
// 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 nDx = 0;
int nDy = 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++)
{
nDx =
pLine[0][i+1] - pLine[0][i-1] +
pLine[1][i+1] - pLine[1][i-1] +
pLine[2][i+1] - pLine[2][i-1];
nDy =
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) (nDx * nDx + nDy * nDy));
if (nValue > 0xFF)
{
nValue = 0xFF;
}
pLine[0][i-1] = (unsigned char) nValue;
}
}
return TRUE;
}
Prewitt 边缘检测效果:
Prewitt 算子
Prewitt 算子采用以下算子分别计算一阶 x 方向和 y 方向的图像差分:
-1 0 1
-1 0 1
-1 0 1
-1 -1 -1
0 0 0
1 1 1
#include
// 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 nDx = 0;
int nDy = 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++)
{
nDx =
pLine[0][i+1] - pLine[0][i-1] +
pLine[1][i+1] - pLine[1][i-1] +
pLine[2][i+1] - pLine[2][i-1];
nDy =
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) (nDx * nDx + nDy * nDy));
if (nValue > 0xFF)
{
nValue = 0xFF;
}
pLine[0][i-1] = (unsigned char) nValue;
}
}
return TRUE;
}
Prewitt 边缘检测效果: