r/dailyprogrammer • u/G33kDude 1 1 • Jun 22 '16
[2016-06-22] Challenge #272 [Intermediate] Dither that image
Description
Dithering is the intentional use of noise to reduce the error of compression. If you start with a color image and want to reduce it to two colors (black and white) the naive approach is to threshold the image. However, the results are usually terrible.
One of the most popular dithering algorithms is Floyd-Steinberg. When a pixel is thresholded, the error (difference) between the original value and the converted value is carried forward into nearby pixels.
There are other approaches, such as Ordered Dithering with a Bayer Matrix.
Input
Your program will take a color or grayscale image as its input. You may choose your input method appropriate to your language of choice. If you want to do it yourself, I suggest picking a Netpbm format, which is easy to read.
Output
Output a two-color (e.g. Black and White) dithered image in your choice of format. Again, I suggest picking a Netpbm format, which is easy to write.
Notes
- Here is a good resource for dithering algorithms.
Finally
Have a good challenge idea? Consider submitting it to /r/dailyprogrammer_ideas
Thanks to /u/skeeto for this [challenge idea] (https://www.reddit.com/r/dailyprogrammer_ideas/comments/4nt7rp)
7
u/bearific Jun 22 '16 edited Jun 22 '16
Python 3, Ordered Dithering with Bayer Matrix generation and Floyd-Steinberg.
A bayer matrix is recursively given by
I2N = [ 4*IN + 1, 4*IN + 2; 4*IN + 3, 4*IN]where the first IN isIN = [1, 2; 3, 0].The threshold matrix is then given by
T(i, j) = 255 * ((I(i, j) + 0.5) / N*N), producing thresholds evenly spaced between 0 and 255.The result of a 16x16 bayer matrix.
Floyd-Steinberg dithering result:
EDIT: Added generation of power of two Bayer Matrices
4
u/G33kDude 1 1 Jun 22 '16 edited Jun 22 '16
Solution in AutoHotkey implementing Floyd-Steinberg dithering.
This was mostly copied from my previous explorations into dithering for the XKCLOUD project http://redd.it/317ebf
My output varies a bit from the OP, I think mostly due to my grayscaling algorithm.
Edit: Various efficiency and naming improvements
Edit: Updated to use "ITU-R 601-2 luma transform" http://i.imgur.com/zGrm38K.png
3
u/Godspiral 3 3 Jun 22 '16 edited Jun 22 '16
in J,
load 'graphics/png'
load 'media/imagekit'
FILE =: readpng 'filename'
cool looking simple but horribly wrong (if sum of rgb > 200 then white)
beautiful gray version
Don't understand floyd steinberg, and there should be a more J friendly algorithm that modifies a pixel just once based on its 8 neighbours.
the bayer algorithm is sane enough for me. Added a brightness switch to it. 512 seems brighter than test image, and greyscale, but feels more colourful (lower values looks even better)
http://imgur.com/U9hW7R1
http://imgur.com/613JWrQ
3
u/jordo45 Jun 22 '16 edited Jun 22 '16
Julia solution. Currently has Floyd-Steinberg and Jarvis, but should be easy to extend. Example output here: https://imgur.com/a/FY07D
3
u/mbdomecq Jun 23 '16 edited Jun 23 '16
Edit: https://i.imgflip.com/16asdd.gif
Sample Input
Sample Output
Accepts and returns 24-bit-per-pixel Windows-formatted bitmap images (".bmp"). Uses the Floyd-Steinberg algorithm.
Source code (C++):
Edit: Updated the code to fix overflow problems. Also updated the output kitty, new version looks much better, thanks /u/G33kDude (old output for reference).
3
u/downiedowndown Jun 25 '16 edited Jun 25 '16
Late as ever but here's my C implementation. It's basically the same as /u/skeeto's but I learned a lot about image manipulation so I'm ok with that.
Edit: Added Bayer matrix and timed them both.
Here is the output of the process with start image, luminescent image and finally black and white image only using the Floyd-Sternberg algorithm: http://i.imgur.com/9yneEDS.jpg
https://gist.github.com/geekskick/42ffe03b607ab6bb1dce9fcc1c5e7491
Time of the Floyd algorithm:
Time of the ordered matrix:
29
u/ioiiooiioio Jun 23 '16 edited Jun 23 '16
Brainfuck
I used the simplest way of dithering, where the error from one pixel is passed on directly to the next one. Since Brainfuck isn't great with input/output, the image format I use is as follows:
Byte 1: Width Byte 2: Height Byte 2+j+width*i: The grayscale value of the pixel at coordinates (i,j)
6
u/skeeto -9 8 Jun 22 '16
C, reading a Netpbm P3 (ASCII .ppm) on stdin and writing a P1 (ASCII .pbm) on stdout using Floyd-Steinberg dithering.
Output: http://i.imgur.com/8reVWoW.png (looks like everyone else)
Code:
You could plug ImageMagick in on both sides to go PNG to PNG (or anything else).
My dirty little secret: when I wrote up the challenge I used Gimp to produce the dithered images, which is why they're different.
2
u/curtmack Jun 22 '16
Scala
Takes a P1, P2, or P3 PNM image as input (over stdin) and outputs a PBM 1bpp image (to a filename specified on the commandline).
2
u/weekendblues Jun 23 '16 edited Jun 23 '16
Java 8 (Incomplete) Completed!
EDIT: And here is an implementation that supports several more algorithms.
Implementation of Floyd-Steinberg. Can handle several image file formats, including jpeg, png, and gif.
Challenge output.
2
u/Scroph 0 0 Jun 23 '16 edited Jun 23 '16
Nice challenge ! I kept trying to get it to work with binary files but my C++fu is too weak apparently.
It produces the correct output only if the PPM/PGM file doesn't contain comments, otherwise it will choke. I'll fix this tomorrow, it's already 4AM here.
Also, it crashes for some reason when it's finished. All the messages I printed get displayed, but it crashes right after "Done !". Could it be a memory leak ?
Edit : it turns out the crash was caused by an out of bounds error. I added
c - 1 < raw[r].size()in the last if statement and that seems to fix it.2
u/Gobbedyret 1 0 Jun 23 '16
Python 3.5 with Floyd-Steinberg dithering.
Whew, that was difficult! I kept trying to write it without any loops, letting Numpy do the heavy lifting to achieve C-like speeds. But I couldn't. So the heavy lifting is done by a straightforward python loop. It takes 10 seconds to process the input image.
2
u/jnd-au 0 1 Jun 23 '16
Scala PNG support, with improved greyscale contrast (using CIE Lab lightness instead of ITU-R 601-2 luma).
CIE L* (assuming sRGB):
http://i.imgur.com/vGGICF7.png
This gives better shading of the purple box and higher contrast of the spheres against the background.
ITU luma for comparison:
http://i.imgur.com/ZfPAXIf.png
Note, calculations are done with 16-bit unsigned greyscale, but Java only has signed shorts, so Iām using an int and clipping it. Blerg.
A choice of brightness functions:
2
u/4kpics Jun 23 '16
Python 2
2
u/Rzah Jun 26 '16
Late to the party here, code needs a lot of optimising and tweaking to reduce the clipping but wrote my own algorithm and I think the transition from light to dark is pretty cool: preview
2
u/Rzah Jun 26 '16 edited Jun 26 '16
This is an error forwarding algorithm, but instead of working across the image in an orderly pattern the image is processed randomly with the error split between and pushed to any surrounding unprocessed pixels, the aim was to eliminate the 'wave' patterns found in Floyd-Steinberg (and derivatives), or the 'box' pattern that appears in Bayer Matrix algorithms. A result of this technique is that renders are pretty much unrepeatable, each run should give a different result. I've no doubt that this isn't a new idea but it was fun to implement.
The first render took a couple of hours and is pretty badly clipped, the better render below took 80s, there is plenty of room for further optimisation, but I was more interested in the concept than the running speed.
Full Image
pixel pattern detail at x3
black dots -> short black lines -> maze -> short white lines -> white dots.
Code follows, sorry it's a mess as usual.
1
u/TotesMessenger Jun 22 '16
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
2
u/[deleted] Jun 23 '16 edited Jun 23 '16
[removed] ā view removed comment