12
2
Your task is to write a program that takes an input image and run it through edge-detection to become an output image.
The edge-detection works as follows (if unclear, see sobel edge detection):
- The value for a pixel is the total brightness of a pixel, so if it is in color, you will need to convert it to grayscale first (to keep things simple and golf-able, you can take the average value for R, G and B).
- The formulae for Gx and Gy for pixel p(i,j) are:
- Gx = -1 * p(i-1, j-1) - 2 * p(i-1, j) - 1 * p(i-1, j+1) + 1 * p(i+1, j-1) + 2 * p(i+1, j) + 1 * p(i+1, j+1)
- Gy = -1 * p(i-1, j-1) - 2 * p(i, j-1) - 1 * p(i+1, j-1) + 1 * p(i-1, j+1) + 2 * p(i, j+1) + 1 * p(i+1, j+1)
- The value for the size of the edge at that pixel is then: √(Gx2 + Gy2)
The output image is for each pixel the size of the edge √(Gx2 + Gy2) as greyscale.
Bonuses:
- Perform a gaussian blur to smooth out the image before edge-detection kicks in, to omit any smaller edges. This gives a bonus of -30% on the end result.
- Take the angle of the edge in account. You give the output pixel some color, by taking the same greyscale value and adding color from a color wheel using the angle obtained from the formula arctan(Gy/Gx). This gives another bonus of -30% on the end result.
Rules:
- You may omit the value for the edgepixels, and set them to black, or you may use 0 for any pixel outside the image.
- Your ouput image must be in an image format that can be opened on most computers.
- Output must be written to disk or be pipeable to a file.
- Input is given as a commandline argument, in the form of a relative path to the image, or piped in from the commandline.
- This is code golf, so shortest code in bytes wins!
Can you exactly specify the gaussian blur? Is the input grayscale as well, if no, how should we apply this edge detection to coloured images? Is it correct that the output image has the exact same size as the input, but the input is only performed on the inner pixels (not the one we've set to zero)? – flawr – 2015-11-11T19:43:02.737
Have you seen the videos about edge detection from Computerphile? I can smell a connection there :)
– GiantTree – 2015-11-11T20:08:59.160@flawr I have to test what gaussian blur is good for edge detection, so I don't really know what is a good value. more on Gaussian blur here. The input image is in color, and you'll need to convert it to grayscale first if you want to perform the edge detection. The edge detection is performed either A: on the inner pixels, and you set the outer 1px border of the output image to black, or B: on all pixels, and you take 0 as the value for any pixels outside the image.
– vrwim – 2015-11-11T20:11:49.600@GiantTree nooooooo the video is totally not related :) – vrwim – 2015-11-11T20:12:43.150
4Why has this been down voted? It seems to be a perfectly valid question. – Addison Crump – 2015-11-12T11:20:44.537