Canny edge detector: Difference between revisions

Small improvements in the D entry
(More idiomatic D code)
(Small improvements in the D entry)
Line 533:
in float[] kernel,
in int nx, in int ny, in int kn)
pure nothrow in {
assert(kernel.length == kn ^^ 2);
assert(inp.length == outp.length);
} body {
//immutable int kn = sqrti(kernel.length);
immutable int khalf = kn / 2;
float pMin = float.max, pMax = -float.max;
 
static if (normalize) {
float pMin = float.max, pMax = -float.max;
 
foreach (m; khalf .. nx - khalf) {
foreach (n; khalf .. ny - khalf) {
float pixel = 0.0;
intsize_t c;
foreach (j; -khalf .. khalf + 1) {
foreach (i; -khalf .. khalf + 1) {
Line 558 ⟶ 563:
foreach (n; khalf .. ny - khalf) {
float pixel = 0.0;
intsize_t c;
foreach (j; -khalf .. khalf + 1) {
foreach (i; -khalf .. khalf + 1) {
Line 587 ⟶ 592:
*/
void gaussianFilter(in Pixel[] inp, Pixel[] outp,
in int nx, in int ny, float sigma) nothrow {
in {
assert(inp.length == outp.length);
} body {
immutable int n = 2 * cast(int)(2 * sigma) + 3;
/*enum*/ immutable float mean = cast(float)floor(n / 2.0);
Line 596 ⟶ 604:
n, sigma);
 
intsize_t c;
foreach (i; 0 .. n) {
foreach (j; 0 .. n) {
Line 629 ⟶ 637:
gaussianFilter(inp, outp, nx, ny, sigma);
 
__gshared immutable float[] Gx = [-1, 0, 1,
-2, 0, 2,
-1, 0, 1];
auto after_Gx = new Pixel[nx * ny];
convolution!(false)(outp, after_Gx, Gx, nx, ny, 3);
 
__gshared immutable float[] Gy = [ 1, 2, 1,
0, 0, 0,
-1,-2,-1];
 
auto after_Gx = new Pixel[nx * ny];
auto after_Gy = new Pixel[nx * ny];
 
convolution!(false)(outp, after_Gx, Gx, nx, ny, 3);
convolution!(false)(outp, after_Gy, Gy, nx, ny, 3);
 
Line 646 ⟶ 652:
foreach (i; 1 .. nx - 1)
foreach (j; 1 .. ny - 1) {
immutable intsize_t c = i + nx * j;
G[c] = cast(Pixel)hypot(after_Gx[c], after_Gy[c]);
}
Anonymous user