Skip to content

Commit

Permalink
update lowpoly example
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomascountz committed Feb 25, 2024
1 parent d2d91c4 commit 1755048
Show file tree
Hide file tree
Showing 255 changed files with 12,881 additions and 2,721 deletions.
Binary file added examples/low_poly_reconstruction/TARGET.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 27 additions & 19 deletions examples/low_poly_reconstruction/low_poly_reconstruction.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
RubyVM::InstructionSequence.compile_option = {
tailcall_optimization: true,
trace_instruction: false
}

require_relative "../../lib/petri_dish"
require "bundler/inline"

$stdout.sync = true
# $stdout.sync = true

gemfile do
source "https://rubygems.org"
gem "rmagick", require: "rmagick"
gem "delaunator", require: true
gem "pry"
end

LOW_POLY_RECONSTUCTION_PATH = "examples/low_poly_reconstruction".freeze
INPUT_IMAGE_PATH = "#{LOW_POLY_RECONSTUCTION_PATH}/ruby.svg".freeze
CONVERTED_INPUT_IMAGE_PATH = "#{LOW_POLY_RECONSTUCTION_PATH}/input_convert.png".freeze
OUT_DIR = "#{LOW_POLY_RECONSTUCTION_PATH}/out".freeze
IMAGE_HEIGHT_PX = 100
IMAGE_WIDTH_PX = 100
GREYSCALE_VALUES = (0..255).to_a
OUT_DIR = "#{LOW_POLY_RECONSTUCTION_PATH}/out3".freeze
IMAGE_HEIGHT_PX = 250
IMAGE_WIDTH_PX = 250
GRAYSCALE_VALUES = (0..255).to_a

class LowPolyImageReconstruction
Point = Struct.new(:x, :y, :grayscale)
Expand All @@ -29,7 +35,7 @@ def run
PetriDish::Member.new(
genes: (0..IMAGE_WIDTH_PX).step(10).map do |x|
(0..IMAGE_HEIGHT_PX).step(10).map do |y|
Point.new(x + point_jitter, y + point_jitter, GREYSCALE_VALUES.sample)
Point.new(x + point_jitter, y + point_jitter, GRAYSCALE_VALUES.sample)
end
end.flatten,
fitness_function: calculate_fitness(target_image)
Expand All @@ -42,16 +48,15 @@ def run
def configuration
PetriDish::Configuration.configure do |config|
config.population_size = 50
config.mutation_rate = 0.1
config.elitism_rate = 0.1
config.max_generations = 2500
config.mutation_rate = 0.05
config.elitism_rate = 0.05
config.max_generations = 10_000
config.fitness_function = calculate_fitness(target_image)
config.parents_selection_function = roulette_wheel_parent_selection_function
config.crossover_function = random_midpoint_crossover_function(config)
config.mutation_function = nudge_mutation_function(config)
config.highest_fitness_callback = ->(member) { save_image(member_to_image(member, IMAGE_WIDTH_PX, IMAGE_HEIGHT_PX)) }
config.highest_fitness_callback = ->(member) { save_image(member_to_image(member)) }
config.generation_start_callback = ->(current_generation) { generation_start_callback(current_generation) }
config.end_condition_function = ->(_member) { false }
end
end

Expand Down Expand Up @@ -117,9 +122,9 @@ def nudge_mutation_function(configuration)
mutated_genes = member.genes.dup.map do |gene|
if rand < configuration.mutation_rate
Point.new(
gene.x + rand(-5..5) + point_jitter,
gene.y + rand(-5..5) + point_jitter,
(gene.grayscale + rand(-5..5)).clamp(0, 255)
(gene.x + rand(-10..10) + point_jitter).clamp(0, IMAGE_WIDTH_PX),
(gene.y + rand(-10..10) + point_jitter).clamp(0, IMAGE_HEIGHT_PX),
(gene.grayscale + rand(-25..25)).clamp(GRAYSCALE_VALUES.min, GRAYSCALE_VALUES.max)
)
else
gene
Expand All @@ -131,14 +136,15 @@ def nudge_mutation_function(configuration)

def calculate_fitness(target_image)
->(member) do
member_image = member_to_image(member, IMAGE_WIDTH_PX, IMAGE_HEIGHT_PX)
member_image = member_to_image(member)
# Difference is a tuple of [mean_error_per_pixel, normalized_mean_error, normalized_maximum_error]
1 / (target_image.difference(member_image)[0]**2) # Use the mean error per pixel as the fitness
# (target_image.difference(member_image)[0])**2 # Use the mean error per pixel as the fitness
1 / target_image.distortion_channel(member_image, Magick::MeanSquaredErrorMetric, Magick::GrayChannel)**2
end
end

def member_to_image(member, width, height)
image = Magick::Image.new(width, height) { |options| options.background_color = "white" }
def member_to_image(member)
image = Magick::Image.new(IMAGE_WIDTH_PX, IMAGE_HEIGHT_PX) { |options| options.background_color = "white" }
draw = Magick::Draw.new

# Perform Delaunay triangulation on the points
Expand All @@ -165,12 +171,14 @@ def member_to_image(member, width, height)
end

def save_image(image)
image.write("#{OUT_DIR}/gen-#{@current_generation}.png")
image.write("#{OUT_DIR}/gen-#{@current_generation.to_s.rjust(4, "0")}.png")
end

def generation_start_callback(current_generation)
@current_generation = current_generation
end
end

# Put this in the gem so users can just call a method to set the compiler options

LowPolyImageReconstruction.new.run
Binary file modified examples/low_poly_reconstruction/montage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/montage2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/low_poly_reconstruction/out/gen-0000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0016.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0019.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0022.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0023.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0029.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0034.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0045.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0046.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0047.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0052.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0057.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/low_poly_reconstruction/out/gen-0058.png
Binary file added examples/low_poly_reconstruction/out/gen-0060.png
Binary file added examples/low_poly_reconstruction/out/gen-0065.png
Binary file added examples/low_poly_reconstruction/out/gen-0070.png
Binary file added examples/low_poly_reconstruction/out/gen-0072.png
Binary file added examples/low_poly_reconstruction/out/gen-0073.png
Binary file added examples/low_poly_reconstruction/out/gen-0076.png
Binary file added examples/low_poly_reconstruction/out/gen-0078.png
Binary file added examples/low_poly_reconstruction/out/gen-0087.png
Binary file added examples/low_poly_reconstruction/out/gen-0088.png
Binary file added examples/low_poly_reconstruction/out/gen-0120.png
Binary file added examples/low_poly_reconstruction/out/gen-0132.png
Binary file added examples/low_poly_reconstruction/out/gen-0135.png
Binary file added examples/low_poly_reconstruction/out/gen-0137.png
Binary file added examples/low_poly_reconstruction/out/gen-0158.png
Binary file added examples/low_poly_reconstruction/out/gen-0176.png
Binary file added examples/low_poly_reconstruction/out/gen-0193.png
Binary file added examples/low_poly_reconstruction/out/gen-0277.png
Binary file added examples/low_poly_reconstruction/out/gen-0288.png
Binary file added examples/low_poly_reconstruction/out/gen-0294.png
Binary file added examples/low_poly_reconstruction/out/gen-0308.png
Binary file added examples/low_poly_reconstruction/out/gen-0358.png
Binary file added examples/low_poly_reconstruction/out/gen-0361.png
Binary file added examples/low_poly_reconstruction/out/gen-0378.png
Binary file added examples/low_poly_reconstruction/out/gen-0422.png
Binary file added examples/low_poly_reconstruction/out/gen-0429.png
Binary file added examples/low_poly_reconstruction/out/gen-0454.png
Binary file added examples/low_poly_reconstruction/out/gen-0510.png
Binary file added examples/low_poly_reconstruction/out/gen-0549.png
Binary file added examples/low_poly_reconstruction/out/gen-0682.png
Binary file added examples/low_poly_reconstruction/out/gen-0708.png
Binary file added examples/low_poly_reconstruction/out/gen-0779.png
Binary file added examples/low_poly_reconstruction/out/gen-0835.png
Binary file added examples/low_poly_reconstruction/out/gen-0851.png
Binary file added examples/low_poly_reconstruction/out/gen-0857.png
Binary file added examples/low_poly_reconstruction/out/gen-0860.png
Binary file added examples/low_poly_reconstruction/out/gen-0906.png
Binary file added examples/low_poly_reconstruction/out/gen-0984.png
Binary file added examples/low_poly_reconstruction/out/gen-1045.png
Binary file added examples/low_poly_reconstruction/out/gen-1102.png
Binary file added examples/low_poly_reconstruction/out/gen-1131.png
Binary file added examples/low_poly_reconstruction/out/gen-1180.png
Binary file added examples/low_poly_reconstruction/out/gen-1223.png
Binary file added examples/low_poly_reconstruction/out/gen-1241.png
Binary file added examples/low_poly_reconstruction/out/gen-1245.png
Binary file added examples/low_poly_reconstruction/out/gen-1248.png
Binary file added examples/low_poly_reconstruction/out/gen-1265.png
Binary file added examples/low_poly_reconstruction/out/gen-1268.png
Binary file added examples/low_poly_reconstruction/out/gen-1544.png
Binary file added examples/low_poly_reconstruction/out/gen-1558.png
Binary file added examples/low_poly_reconstruction/out/gen-1563.png
Binary file added examples/low_poly_reconstruction/out/gen-1568.png
Binary file added examples/low_poly_reconstruction/out/gen-1700.png
Binary file added examples/low_poly_reconstruction/out/gen-1855.png
Binary file added examples/low_poly_reconstruction/out/gen-1877.png
Binary file added examples/low_poly_reconstruction/out/gen-1897.png
Binary file added examples/low_poly_reconstruction/out/gen-1944.png
Binary file added examples/low_poly_reconstruction/out/gen-2194.png
Binary file added examples/low_poly_reconstruction/out/gen-2233.png
Binary file added examples/low_poly_reconstruction/out/gen-2236.png
Binary file added examples/low_poly_reconstruction/out/gen-2241.png
Binary file added examples/low_poly_reconstruction/out/gen-2358.png
Binary file added examples/low_poly_reconstruction/out/gen-2389.png
Binary file added examples/low_poly_reconstruction/out/gen-2461.png
Binary file removed examples/low_poly_reconstruction/out/gen-2473.png
Diff not rendered.
Binary file added examples/low_poly_reconstruction/out/gen-2548.png
Binary file added examples/low_poly_reconstruction/out/gen-2619.png
Binary file added examples/low_poly_reconstruction/out/gen-2625.png
Binary file added examples/low_poly_reconstruction/out/gen-2739.png
Binary file added examples/low_poly_reconstruction/out/gen-2740.png
Binary file added examples/low_poly_reconstruction/out/gen-3194.png
Binary file added examples/low_poly_reconstruction/out/gen-3223.png
Binary file added examples/low_poly_reconstruction/out/gen-3826.png
Binary file added examples/low_poly_reconstruction/out/gen-3988.png
Binary file added examples/low_poly_reconstruction/out/gen-4016.png
Binary file added examples/low_poly_reconstruction/out/gen-4069.png
Binary file added examples/low_poly_reconstruction/out/gen-4189.png
Binary file added examples/low_poly_reconstruction/out/gen-4308.png
Binary file added examples/low_poly_reconstruction/out/gen-4332.png
Binary file added examples/low_poly_reconstruction/out/gen-4440.png
Binary file added examples/low_poly_reconstruction/out/gen-4684.png
Binary file added examples/low_poly_reconstruction/out/gen-4924.png
Binary file added examples/low_poly_reconstruction/out/gen-5170.png
Binary file added examples/low_poly_reconstruction/out/gen-5433.png
Binary file added examples/low_poly_reconstruction/out/gen-5475.png
Binary file added examples/low_poly_reconstruction/out/gen-5560.png
Binary file added examples/low_poly_reconstruction/out/gen-5702.png
Binary file added examples/low_poly_reconstruction/out/gen-6124.png
Binary file added examples/low_poly_reconstruction/out/gen-6198.png
Loading

0 comments on commit 1755048

Please sign in to comment.