Search code examples
imagescalaimage-processingsobel

Scala implementation of sobel filter


I'm looking for some help in a IT school project. We need to create a programm which can detect roads in a satelite photograph. Our group decided to use a function for detect edges. We search differents solutions and filters on Internet and we decides to use Sobel filter.

We have tried to implement this filter in Scala but it didn't work. We use differents webpages to help us, some of these are on StackOverflow (here). We use this one to help us and try to translate the code : Sobel filter in Ruby.

Start Code --

codeGrey(); // This function transform the RGB in grey level

  var sobel_x: Array[Array[Double]] = Array(
    Array(-1, 0, 1),
    Array(-2, 0, 2),
    Array(-1, 0, 1))

  var sobel_y: Array[Array[Double]] = Array(
    Array(1, 2, 1),
    Array(0, 0, 0),
    Array(-1, -2, 1))

  for (x <- 1 to wrappedImage.height - 2) {
    for (y <- 1 to wrappedImage.width - 2) {

      var a = (image2D(x - 1)(y - 1) & 0x00FF0000) >> 16
      var b = (image2D(x)(y - 1) & 0x00FF0000) >> 16
      var c = (image2D(x + 1)(y - 1) & 0x00FF0000) >> 16
      var d = (image2D(x - 1)(y) & 0x00FF0000) >> 16
      var e = (image2D(x)(y) & 0x00FF0000) >> 16
      var f = (image2D(x + 1)(y) & 0x00FF0000) >> 16
      var g = (image2D(x - 1)(y + 1) & 0x00FF0000) >> 16
      var h = (image2D(x)(y + 1) & 0x00FF0000) >> 16
      var i = (image2D(x + 1)(y + 1) & 0x00FF0000) >> 16

      var pixel_x =
        (sobel_x(0)(0) * a) + (sobel_x(0)(1) * b) + (sobel_x(0)(2) * c) +
          (sobel_x(1)(0) * d) + (sobel_x(1)(1) * e) + (sobel_x(1)(2) * f) +
          (sobel_x(2)(0) * g) + (sobel_x(2)(1) * h) + (sobel_x(2)(2) * i);
      var pixel_y =
        (sobel_y(0)(0) * a) + (sobel_x(0)(1) * b) + (sobel_x(0)(2) * c) +
          (sobel_y(1)(0) * d) + (sobel_x(1)(1) * e) + (sobel_x(1)(2) * f) +
          (sobel_y(2)(0) * g) + (sobel_x(2)(1) * h) + (sobel_x(2)(2) * i);

      var res = (Math.sqrt((pixel_x * pixel_x) + (pixel_y *     pixel_y)).ceil).toInt

      image2D(x)(y) = 0xFF000000 + (res * 65536 + res * 256 + res);

    }
  }

End Code --

The image returned by this implementation is just an image with black and white pixels and I don't know why. I've got no experience in image processing and we learned Scala 8 weeks ago so that doesn't help. I'm sorry, my english is not perfect so please forgive me if I didn't write correctly.


Solution

  • I'm not sure I grasp all the details of your solution, anyway here some observation:

    1. consider using vals instead of vars: Scala prefers immutables and you are not really changing any of those variables.
    2. In scala you can write nested for cycles as a single one over two variables (check here for details: Nested iteration in Scala). I think it makes code cleaner.
    3. I presume image2D is the array of arrays in which you are holding your image. In the last line of your nested for loop you are changing the current pixel value. This is not good because you will access that same pixel later when you calculate your a,b,..,h,i values. The center pixel during current iteration is the side pixel during next iteration. I think you should write the result in a different matrix.