#include "math.h"
// Robert算子
/*
-----------
| N2 | N3 |
-----------
| N0 | N1 |
-----------
计算公式
sqrt((N0 - N3)2 + (N1 - N2)2)
void Robert(BYTE* image0, BYTE* image1, unsigned int w, unsigned int h)
{
int x, y, aR, aG, aB, a;
long n;
for(y = 0; y < h -1; ++y)
{
for(x = 0; x < w -1; ++x)
{
n = (y * w + x) * 4;
aR = sqrt((double)((image0[n] - image0[n - w * 4 + 4]) * (image0[n] -image0[n - w * 4 + 4]) + (image0[n + 4] - image0[n - w * 4]) * (image0[n + 4] - image0[n - w * 4])));
aG = sqrt((double)((image0[n + 1] - image0[n - w * 4 + 4 + 1]) * (image0[n + 1] -image0[n - w * 4 + 4 + 1]) + (image0[n + 4 + 1] - image0[n - w * 4 + 1]) * (image0[n + 4 + 1] - image0[n - w * 4 + 1])));
aB = sqrt((double)((image0[n + 2] - image0[n - w * 4 + 4 + 2]) * (image0[n + 2] -image0[n - w * 4 + 4 + 2]) + (image0[n + 4 + 2] - image0[n - w * 4 + 2]) * (image0[n + 4 + 2] - image0[n - w * 4 + 2])));
a = aR + aG + aB;
a = a>255?255:a;
//生成边缘扫描结果
SetPixel(image1,n,a);
}
}
}
原图:
Robert 边缘算子检测图: