【转】Prewitt 算子、Roberts 算子、Laplace 算子

Prewitt 算子采用以下算子分别计算一阶 方向和 方向的图像差分: 
Prewitt 算子 - illidan - illidan的博客 

#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            nDx          = 0
    int            nDy          = 0
    int            nValue       = 0
    unsigned char *pLine[3     = { NULL, NULL, NULL }; 
    for (j = 1j < nHeight - 1j++
    { 
        pLine[0  = pImageData + nWidthStep * (j - 1); 
        pLine[1  = pImageData + nWidthStep * j
        pLine[2  = pImageData + nWidthStep * (j + 1); 
        for (i = 1i < nWidth - 1i++
        { 
            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 = (intsqrt((float(nDx * nDx + nDy * nDy)); 
            if (nValue > 0xFF
            { 
                nValue = 0xFF
            } 
            pLine[0][i-1 = (unsigned charnValue
        } 
    } 
    return TRUE
} 


Prewitt 边缘检测效果: 
 
Prewitt 算子 - illidan - illidan的博客Prewitt 算子 - illidan - illidan的博客
 
 
 
 
 
 
 
 
 
Roberts 算子计算交叉差分,取两种差分的较大值: 
Roberts 算子 - illidan - illidan的博客 

// Roberts 算子 
// 1. pImageData   图像数据 
// 2. nWidth       图像宽度 
// 3. nHeight      图像高度 
// 4. nWidthStep   图像行大小 
BOOL Roberts(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[2     = { NULL, NULL }; 
    for (j = 0j < nHeight - 1j++
    { 
        pLine[0  = pImageData + nWidthStep * j
        pLine[1  = pImageData + nWidthStep * (j + 1); 
        for (i = 0i < nWidth - 1i++
        { 
            nDx = abs(pLine[0][i - pLine[1][i+1]); 
            nDy = abs(pLine[1][i - pLine[0][i+1]); 
            nValue = nDx > nDy ? nDx : nDy
            pLine[0][i-1 = (unsigned charnValue
        } 
    } 
    return TRUE
} 


Roberts 检测效果: 
Roberts 算子 - illidan - illidan的博客Roberts 算子 - illidan - illidan的博客
 
 
 
 
 
 
 
Laplace 算子可以卷积模板表示: 

Laplace 算子 - illidan - illidan的博客 


// Laplace 算子 
// 1. pImageData   图像数据 
// 2. nWidth       图像宽度 
// 3. nHeight      图像高度 
// 4. nWidthStep   图像行大小 
BOOL Laplace(unsigned char *pImageData, int nWidth, int nHeight, int nWidthStep
{ 
    int            I            = 0
    int            j            = 0
    int            nValue       = 0
    unsigned char *pLine[3     = { NULL, NULL, NULL }; 
    for (j = 1j < nHeight - 1j++
    { 
        pLine[0  = pImageData + nWidthStep * (j - 1); 
        pLine[1  = pImageData + nWidthStep * j
        pLine[2  = pImageData + nWidthStep * (j + 1); 
        for (I = 1I < nWidth - 1i++
        { 
            nValue = 
                pLine[0][i + pLine[1][i-1 + pLine[1][i+1 + pLine[2][i - 
                pLine[1][i * 4
            pLine[0][i-1 = (unsigned charabs(nValue); 
        } 
    } 
    return TRUE
} 

Laplace 变换效果: 
Laplace 算子 - illidan - illidan的博客Laplace 算子 - illidan - illidan的博客
 

posted on 2012-05-23 09:54  龙猫先生  阅读(1119)  评论(0编辑  收藏  举报

导航