Skip to content

Commit 6f02a48

Browse files
committed
ImageProcessing examples
1 parent 1d54076 commit 6f02a48

File tree

10 files changed

+374
-0
lines changed

10 files changed

+374
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The [image_processing library][image_processing] was created by [Nick 'Milchreis' Müller][Milchreis] and needs to be installed in your `~/.propane/libraries`, NB I use the convention of converting
2+
camelcase names to snakecase, I suggest you do the same for your propane libraries.
3+
4+
[image_processing]:https://github.com/Milchreis/processing-imageprocessing
5+
[Milchreis]:https://milchreisjunkie.wordpress.com/
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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
378 KB
Loading
70.6 KB
Loading
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env jruby -v -W2
2+
require 'propane'
3+
4+
# Example for dithering an image to get a retro look.
5+
#
6+
# Use the mouse wheel to change the dithering algorithm
7+
# and press spacebar to show it in gray scale for the perfect
8+
# retro look.
9+
#
10+
# Author: Nick 'Milchreis' Müller
11+
class DitheringSketch < Propane::App
12+
load_library :image_processing
13+
java_import 'milchreis.imageprocessing.Dithering'
14+
java_import 'milchreis.imageprocessing.Grayscale'
15+
attr_reader :my_image, :processed, :label, :index
16+
17+
def settings
18+
size(550, 550)
19+
end
20+
21+
def setup
22+
sketch_title 'Dithering'
23+
@index = 0
24+
@my_image = load_image(data_path('example.jpg'))
25+
end
26+
27+
def draw
28+
@processed = my_image
29+
@label = ''
30+
if key_pressed? && key == ' '
31+
@processed = Grayscale.apply(processed)
32+
end
33+
return image(my_image, 0, 0) if mouse_pressed?
34+
case index
35+
when -1
36+
@processed = Dithering.apply(processed)
37+
@label = 'BAYER_4x4 on default'
38+
when 0
39+
@processed = Dithering.apply(processed, Dithering::Algorithm::BAYER_2x2)
40+
@label = 'BAYER_2x2'
41+
when 1
42+
@processed = Dithering.apply(processed, Dithering::Algorithm::BAYER_4x4)
43+
@label = 'BAYER_4x4'
44+
when 2
45+
@processed = Dithering.apply(processed, Dithering::Algorithm::BAYER_8x8)
46+
@label = 'BAYER_8x8'
47+
end
48+
image(processed, 0, 0)
49+
fill(0)
50+
text(label, width / 2 - text_width(label) / 2, 30)
51+
end
52+
53+
def mouseWheel(event)
54+
@index += event.get_count
55+
56+
if index >= Dithering::Algorithm.values.length
57+
@index = -1
58+
end
59+
end
60+
end
61+
62+
DitheringSketch.new
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env jruby -v -W2
2+
require 'propane'
3+
4+
# Example for an artificial glitch effect.
5+
# Move the mouse from left to right to see different intensity
6+
# or press the left mouse button to see the original image.
7+
#
8+
# Author: Nick 'Milchreis' Müller
9+
# Translated to JRubyArt by Martin Prout
10+
class GlitchExample < Propane::App
11+
load_library :image_processing
12+
java_import 'milchreis.imageprocessing.Glitch'
13+
14+
attr_reader :my_image
15+
16+
def setup
17+
sketch_title 'Glitch'
18+
# Load image
19+
@my_image = load_image(data_path('example.jpg'))
20+
end
21+
22+
def draw
23+
return image(my_image, 0, 0) if mouse_pressed?
24+
intensity = map1d(mouseX, 0..width, 0..4)
25+
image(Glitch.apply(my_image, intensity), 0, 0)
26+
# You can also use:
27+
# Glitch.apply(image)
28+
# Glitch.apply(image, intensity, scanlineHeight)
29+
end
30+
31+
def settings
32+
size(550, 550)
33+
end
34+
end
35+
36+
GlitchExample.new
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env jruby -v -W2
2+
require 'propane'
3+
4+
# Example for halftone an image to get a old school print look.
5+
#
6+
# The dot size can be changed by moving the mouse left and right.
7+
# The spacing between the dots can be changed by moving the mouse up and down.
8+
# If you press the mouse button the dots will be shown in a grid.
9+
#
10+
# Author: Nick 'Milchreis' Müller
11+
class HalfTone < Propane::App
12+
load_library :image_processing
13+
java_import 'milchreis.imageprocessing.Halftone'
14+
attr_reader :my_image
15+
16+
def settings
17+
size(550, 550)
18+
end
19+
20+
def setup
21+
sketch_title 'Half Tone'
22+
# Load image
23+
@my_image = load_image(data_path('example.jpg'))
24+
end
25+
26+
def draw
27+
# Simple usage:
28+
# image = Halftone.apply(image, dotsize)
29+
30+
# dotsize by mouseX
31+
dotsize = map1d(mouseX, 0..width, 3..10)
32+
33+
# dots in grid or honeycomb style by mousePressed
34+
inGrid = mouse_pressed?
35+
36+
# Foreground color
37+
foreground = '#335764'
38+
39+
# Space between dots by mouseY
40+
space = map1d(mouseY, 0..height, 1..3)
41+
42+
# Draw image
43+
image(Halftone.apply(my_image, dotsize, color(foreground), 255, space, inGrid), 0, 0)
44+
end
45+
end
46+
47+
HalfTone.new
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env jruby -v -W2
2+
require 'propane'
3+
4+
# Example for lookup tables (LUT).
5+
# Use the mouse wheel to switch the different tables/styles
6+
# and press the left mouse button to see the original image.
7+
#
8+
# Author: Nick 'Milchreis' Müller
9+
# Translated to JRubyArt by Martin Prout
10+
class LUTExample < Propane::App
11+
load_library :image_processing
12+
java_import 'milchreis.imageprocessing.LUT'
13+
14+
attr_reader :my_image, :lookuptables, :current_index, :enums
15+
16+
def settings
17+
size(550, 550)
18+
end
19+
20+
def setup
21+
sketch_title 'LUT Example'
22+
@current_index = 0
23+
# Load image
24+
@my_image = load_image(data_path('example.jpg'))
25+
# Create an array with all lookup-tables
26+
# LUT Styles:
27+
# RETRO, CONTRAST, CONTRAST_STRONG, ANALOG1, WINTER, SPRING, SUMMER, AUTUMN
28+
@enums = LUT::STYLE.values
29+
@lookuptables = enums.map do |enum|
30+
LUT.load_lut(enum.java_object)
31+
end
32+
# Load one style:
33+
# LUT style = LUT.loadLut(LUT.STYLE.ANALOG1)
34+
end
35+
36+
def draw
37+
return image(my_image, 0, 0) if mouse_pressed?
38+
image(LUT.apply(my_image, lookuptables[current_index]), 0, 0)
39+
fill(0)
40+
stylename = enums[current_index].name
41+
text(stylename, width / 2 - textWidth(stylename) / 2, 30)
42+
end
43+
44+
def mouseWheel(event) # keep camelcase for poxy java reflection
45+
@current_index += 1
46+
@current_index = 0 if current_index >= lookuptables.length
47+
end
48+
end
49+
50+
LUTExample.new
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env jruby -v -W2
2+
require 'propane'
3+
4+
# Example for sharpening an image to get a crunchy look.
5+
# The intensity of this effect increases slowly.
6+
# Press the left mouse button to see the original image.
7+
#
8+
# Author: Nick 'Milchreis' Müller
9+
class Sharpening < Propane::App
10+
load_library :image_processing
11+
12+
java_import 'milchreis.imageprocessing.Sharpen'
13+
14+
attr_reader :my_image, :sharp_intensity
15+
16+
def settings
17+
size(550, 550)
18+
end
19+
20+
def setup
21+
sketch_title 'Sharpen'
22+
@sharp_intensity = 0
23+
@my_image = load_image(data_path('example.jpg'))
24+
end
25+
26+
def draw
27+
return image(my_image, 0, 0) if mouse_pressed?
28+
image(Sharpen.apply(my_image, sharp_intensity), 0, 0)
29+
# Reset the intensity or increase it
30+
@sharp_intensity = (sharp_intensity > 6) ? 0 : sharp_intensity + 0.05
31+
end
32+
end
33+
34+
Sharpening.new
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env jruby -v -W2
2+
require 'propane'
3+
4+
# Example for stacking images.
5+
# Generates an average face by 10 example faces.
6+
# Normally the median pixel is selected.
7+
# Use the mouse button to see the average mode.
8+
#
9+
# Author: Nick 'Milchreis' Müller
10+
class Stacking < Propane::App
11+
12+
load_library :image_processing
13+
java_import 'milchreis.imageprocessing.Stacker'
14+
15+
# Dimensions for each face
16+
GRID_WIDTH = 128
17+
GRID_HEIGHT = 155
18+
attr_reader :faces
19+
20+
def settings
21+
size(128, 155)
22+
end
23+
24+
def setup
25+
sketch_title 'Stacker'
26+
# Load faces grid
27+
yale_faces = load_image(data_path('yaleBfaces.jpg'))
28+
@faces = []
29+
grid(yale_faces.width, yale_faces.height, GRID_WIDTH, GRID_HEIGHT) do |x, y|
30+
faces << yale_faces.get(x, y, GRID_WIDTH, GRID_HEIGHT)
31+
end
32+
end
33+
34+
def draw
35+
faces_java = faces.to_java(Java::ProcessingCore::PImage)
36+
# Alternative algorithm is average
37+
return image(Stacker.apply(Stacker::ALGORITHM::AVERAGE, faces_java), 0, 0) if mouse_pressed?
38+
# Default algorithm is median
39+
image(Stacker.apply(faces_java), 0, 0)
40+
# The array is not essential. If you save your images in different variables use this:
41+
# Stacker.apply(image1, image2, image3) # works also
42+
end
43+
end
44+
45+
Stacking.new

0 commit comments

Comments
 (0)