The math
Like derivation method, the template-based methods also try to detect edges by detecting sudden grayscale changes. As the name suggests, these methods use templates to model edges. The Sobel edge detector uses two templates, modeling changes along x-axis and y-axis. The Sobel templates are defined as following:
![clip_image002[4] clip_image002[4]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYrCXdXdEotHt9oGLtujMUjhdefT4_2-6H69cObUb-WZAuE9wPazkDZCEkOWP_t6K9KixOT-oTkjH1XUtP59G09OTAmmCVETAeuCepreNCLmAW2eDSGJwNiX-s7vdGlgUskQQIqqd893tS/?imgmax=800)
For each pixel on the image, both templates are applied to the 3X3 area around the pixel. The grayscale values of surrounding pixels are multiplied by corresponding matrix values and then summed together:
![clip_image002[6] clip_image002[6]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHwSA9YUvIGDYaMlfL3vEIHIpRtzRWB7ejE8nMj2iE0PrfaoatfmoQKVQbweCt7xRJttl_ddZP0RptNLfYhrc-Eml81lqc4i5B0Dq8gmjHe7LhcCgbMUfZyc3TQyg8VWMOI7PLfLUGOQrz/?imgmax=800)
,where T is either Sx (for x-axis) or Sy (for y-axis). Then the absolute values of Ix and Iy are summed:
![clip_image002[14] clip_image002[14]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizTZ_AlBVpkZZk7lhAxhIyhLR38s7qOuRD7a3hEKpyiHneRypwUnNvr2wYel2a-wz9g-WZTP8LRdKS4IoaPTWF3xhWdt2SYGqwKQL28qBAEKU9IcmYfdMU_yTavK0xyx8962ob4a1LeM5n/?imgmax=800)
At last, I is compare with threshold and the pixel is marked as edge pixel if I is larger than threshold.
Another template-based method, Kirsch method, uses 8 templates instead of 2. The templates model 8 different possible edge directions: left-right, up-down, and upperleft-lowerright, etc. For each pixel of the image, all 8 templates are applied and maximum I is chosen to be compared with the threshold.
The code
The version (v0.2) of my image processing tool that include these two template-based methods can be downloaded here. The implementation of the two methods are very similar. Here I’m showing the Krisch code that works on the R channel of the image:
The result
The following result is my implementation of Krisch detector with threshold 185.

So far all the 3 edge detectors we’ve implemented only consider local grayscale changes. This works fine for simpler images – as shown in the following example the result is very accurate:
However the algorithms are greatly challenged by some of natural images. In the following example the hairs of the koala are detected as edges as they do appear as sudden grayscale changes at pixel level. However, the edges around the arm and the tree branches are not detected because they are blurred in the picture, messing up our 3-by-3 local detection. To better simulate biological vision (like we have) more sophisticated methods are needed. In the coming posts I’ll introduce some of such algorithms.

Like derivation method, the template-based methods also try to detect edges by detecting sudden grayscale changes. As the name suggests, these methods use templates to model edges. The Sobel edge detector uses two templates, modeling changes along x-axis and y-axis. The Sobel templates are defined as following:
For each pixel on the image, both templates are applied to the 3X3 area around the pixel. The grayscale values of surrounding pixels are multiplied by corresponding matrix values and then summed together:
,where T is either Sx (for x-axis) or Sy (for y-axis). Then the absolute values of Ix and Iy are summed:
At last, I is compare with threshold and the pixel is marked as edge pixel if I is larger than threshold.
Another template-based method, Kirsch method, uses 8 templates instead of 2. The templates model 8 different possible edge directions: left-right, up-down, and upperleft-lowerright, etc. For each pixel of the image, all 8 templates are applied and maximum I is chosen to be compared with the threshold.
The code
The version (v0.2) of my image processing tool that include these two template-based methods can be downloaded here. The implementation of the two methods are very similar. Here I’m showing the Krisch code that works on the R channel of the image:
public override Bitmap FilterProcessImage(Bitmap image) { Bitmap ret = new Bitmap(image.Width, image.Height); for (int i = 1; i < image.Width - 1; i++) { for (int j = 1; j < image.Height - 1; j++) { Color cr = image.GetPixel(i + 1, j); Color cl = image.GetPixel(i - 1, j); Color cu = image.GetPixel(i, j - 1); Color cd = image.GetPixel(i, j + 1); Color cld = image.GetPixel(i - 1, j + 1); Color clu = image.GetPixel(i - 1, j - 1); Color crd = image.GetPixel(i + 1, j + 1); Color cru = image.GetPixel(i + 1, j - 1); int power = getMaxD(cr.R, cl.R, cu.R, cd.R, cld.R, clu.R, cru.R, crd.R); if (power > 50) ret.SetPixel(i, j, Color.Yellow); else ret.SetPixel(i, j, Color.Black); } } return ret; }And getMaxD is defined as:
private int getD(int cr, int cl, int cu, int cd, int cld, int clu, int cru, int crd, int[,] matrix) { return Math.Abs( matrix[0, 0]*clu + matrix[0, 1]*cu + matrix[0, 2]*cru + matrix[1, 0]*cl + matrix[1, 2]*cr + matrix[2, 0]*cld + matrix[2, 1]*cd + matrix[2, 2]*crd); } private int getMaxD(int cr, int cl, int cu, int cd, int cld, int clu, int cru, int crd) { int max = int.MinValue; for (int i = 0; i < templates.Count; i++) { int newVal = getD(cr, cl, cu, cd, cld, clu, cru, crd, templates[i]); if (newVal > max) max = newVal; } return max; } private List<int[,]> templates = new List<int[,]> { new int[,] {{ -3, -3, 5 }, { -3, 0, 5 }, { -3, -3, 5 } }, new int[,] {{ -3, 5, 5 }, { -3, 0, 5 }, { -3, -3, -3 } }, new int[,] {{ 5, 5, 5 }, { -3, 0, -3 }, { -3, -3, -3 } }, new int[,] {{ 5, 5, -3 }, { 5, 0, -3 }, { -3, -3, -3 } }, new int[,] {{ 5, -3, -3 }, { 5, 0, -3 }, { 5, -3, -3 } }, new int[,] {{ -3, -3, -3 }, { 5, 0, -3 }, { 5, 5, -3 } }, new int[,] {{ -3, -3, -3 }, { -3, 0, -3 }, { 5, 5, 5 } }, new int[,] {{ -3, -3, -3 }, { -3, 0, 5 }, { -3, 5, 5 } } };Note the templates variable contains all 8 Krisch templates. In the sample code above the threshold is hard-coded to 50.
The result
The following result is my implementation of Krisch detector with threshold 185.
So far all the 3 edge detectors we’ve implemented only consider local grayscale changes. This works fine for simpler images – as shown in the following example the result is very accurate:
However the algorithms are greatly challenged by some of natural images. In the following example the hairs of the koala are detected as edges as they do appear as sudden grayscale changes at pixel level. However, the edges around the arm and the tree branches are not detected because they are blurred in the picture, messing up our 3-by-3 local detection. To better simulate biological vision (like we have) more sophisticated methods are needed. In the coming posts I’ll introduce some of such algorithms.
No comments:
Post a Comment