|
| 1 | +#!/usr/bin/env jruby -v -W2 |
| 2 | +require 'propane' |
| 3 | + |
| 4 | +# Example for basic image processing algorithms. |
| 5 | +# Use the mouse wheel to switch the different algorithms |
| 6 | +# and press the left mouse button to see the original image. |
| 7 | +# |
| 8 | +# Author: Nick 'Milchreis' Müller |
| 9 | +class BasicImageProcessing < Propane::App |
| 10 | + |
| 11 | + load_library :image_processing |
| 12 | + |
| 13 | + include_package 'milchreis.imageprocessing' |
| 14 | + |
| 15 | + NUMBER_OF_ALGORITHMS = 11 |
| 16 | + attr_reader :current_algorithm, :processed_image, :my_image |
| 17 | + |
| 18 | + def settings |
| 19 | + size(550, 550) |
| 20 | + end |
| 21 | + |
| 22 | + def setup |
| 23 | + sketch_title 'Image Processing' |
| 24 | + @my_image = loadImage(data_path('example.jpg')) |
| 25 | + @current_algorithm = 0 |
| 26 | + end |
| 27 | + |
| 28 | + def draw |
| 29 | + return image(my_image, 0, 0) if mouse_pressed? |
| 30 | + |
| 31 | + # Grayscale converts the image in to 256 shades of gray |
| 32 | + case current_algorithm |
| 33 | + when 0 |
| 34 | + @processed_image = Grayscale.apply(my_image)# if current_algorithm == 0 |
| 35 | + |
| 36 | + # Threshold converts a pixel in bright or dark for a specific color value |
| 37 | + when 1 |
| 38 | + # automatic |
| 39 | + # @processed_image = Threshold.apply(my_image) |
| 40 | + # by mouseX |
| 41 | + @processed_image = Threshold.apply(my_image, map1d(mouse_x, 0..width, 0..255)) |
| 42 | + when 2, 3, 10 |
| 43 | + quant = map1d(mouse_x, 0..width, 1..10) |
| 44 | + if current_algorithm == 10 |
| 45 | + @processed_image = Quantization.apply(my_image, quant) |
| 46 | + else |
| 47 | + @processed_image = Threshold.apply(my_image) |
| 48 | + if current_algorithm == 2 |
| 49 | + # Dilation expands the white regions by a radius |
| 50 | + # works best with threshold/binary images |
| 51 | + @processed_image = Dilation.apply(processed_image, quant) |
| 52 | + else |
| 53 | + # Erosion expands the dark regions by a radius |
| 54 | + # works best with threshold/binary images |
| 55 | + @processed_image = Erosion.apply(processed_image, quant) |
| 56 | + end |
| 57 | + end |
| 58 | + # Brightness correction |
| 59 | + when 4 |
| 60 | + intensity = map1d(mouse_x, 0..width, -255..255) |
| 61 | + @processed_image = Brightness.apply(my_image, intensity) |
| 62 | + # AutoBalance for simple color correction |
| 63 | + when 5 |
| 64 | + @processed_image = AutoBalance.apply(my_image) |
| 65 | + |
| 66 | + # Pixelation |
| 67 | + when 6 |
| 68 | + pixelsize = map1d(mouse_x, 0..width, 0..100) |
| 69 | + @processed_image = Pixelation.apply(my_image, pixelsize) |
| 70 | + |
| 71 | + # Gaussian for blurred images |
| 72 | + when 7 |
| 73 | + @processed_image = Gaussian.apply(my_image, 7, 0.84089642) |
| 74 | + |
| 75 | + # Edge detection with Canny's algorithm |
| 76 | + when 8 |
| 77 | + @processed_image = CannyEdgeDetector.apply(my_image) |
| 78 | + |
| 79 | + # Edge detection with Sobel's algorithm |
| 80 | + when 9 |
| 81 | + # SobelEdgeDetector.apply(image, false) creates a colored image |
| 82 | + @processed_image = SobelEdgeDetector.apply(my_image) |
| 83 | + end |
| 84 | + # show image |
| 85 | + image(processed_image, 0, 0) |
| 86 | + end |
| 87 | + |
| 88 | + def mouseWheel(event) # keep camelcase poxy reflection |
| 89 | + @current_algorithm += event.getCount |
| 90 | + @current_algorithm = 0 if current_algorithm >= NUMBER_OF_ALGORITHMS |
| 91 | + @current_algorithm = NUMBER_OF_ALGORITHMS - 1 if current_algorithm < 0 |
| 92 | + end |
| 93 | +end |
| 94 | + |
| 95 | +BasicImageProcessing.new |
0 commit comments