1

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.

Community
  • 1
  • 1
  • What would you expect the result to be? In one of the examples you've linked to the question, the output of the Sobel operator application is consists of black and white pixels as well. – tkroman Nov 21 '15 at 21:36
  • The result we expect was a new image with the edges. Source image : http://www.koenigguillaume.me/6.jpg Result : http://www.koenigguillaume.me/outputImage.jpg But the result is nit good, I have no edges in this new image like here : http://stackoverflow.com/questions/17815687/image-processing-implementing-sobel-filter. – Guillaume Koenig Nov 22 '15 at 10:05

1 Answers1

2

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.
Community
  • 1
  • 1
Chosmos
  • 596
  • 9
  • 11
  • Thank you Chomos. I created a new transparent image with the same height and width and it works ! For the replacement of my vars I prefer wait because the project isn't complete but I keep that in my mind. – Guillaume Koenig Nov 24 '15 at 16:36