Previous | Next --- Slide 36 of 58
Back to Lecture Thumbnails
kayvonf

To check your understanding of the code, can you rewrite it as a single nested for loop? What is the total work performed by your algorithm now?

regi

As a single nested for loop:

for (int y = 0; y < in.height(); y++) {
  for (int x = 0; x < in.width(); x++) {
    blurred(x, y) = (in(x-1, y-1) + in(x, y-1) + in(x+1, y-1) +
                     in(x-1, y)   + in(x, y)   + in(x+1, y)   +
                     in(x-1, y+1) + in(x, y+1) + in(x+1, y+1)) / 9;
  }
}

This does 9 x width() x height() work, so the original two-pass implementation is preferable (for non-parallel execution).

kayvonf

@regi. Solid work!

rflood

Doesn't this blur technically have a different output than the single nested blur? Not that it makes a difference to a human eye, but this speedup is partially due to a change in the results

kayvonf

@rflood: The only difference in output is differences due to the non-associativity of finite-precision floating point math.