All Projects → mxgmn → Convchain

mxgmn / Convchain

Licence: other
Bitmap generation from a single example with convolutions and MCMC

Projects that are alternatives of or similar to Convchain

Wavefunctioncollapse
Bitmap & tilemap generation from a single example with the help of ideas from quantum mechanics
Stars: ✭ 17,156 (+2852.84%)
Mutual labels:  algorithm, procedural-generation, gamedev
Texturesynthesis
Texture synthesis from examples
Stars: ✭ 709 (+22.03%)
Mutual labels:  algorithm, procedural-generation, gamedev
Gpwfc
openCL-accelerated python implementation of the Wave Function Collapse procgen algorithm
Stars: ✭ 37 (-93.63%)
Mutual labels:  algorithm, procedural-generation, gamedev
Blog
About math, programming and procedural generation
Stars: ✭ 37 (-93.63%)
Mutual labels:  procedural-generation, gamedev
Godot3 procgen demos
Exploring Procedural Generation algorithms in Godot
Stars: ✭ 85 (-85.37%)
Mutual labels:  procedural-generation, gamedev
Utymap
Highly customizable library for procedural world generation based on real map data
Stars: ✭ 825 (+42%)
Mutual labels:  procedural-generation, gamedev
wfc
Go port of the Wave Function Collapse algorithm
Stars: ✭ 47 (-91.91%)
Mutual labels:  gamedev, procedural-generation
Fantasymapgenerator
A fantasy map generator based on Martin O'Leary's "Generating fantasy map" notes
Stars: ✭ 538 (-7.4%)
Mutual labels:  procedural-generation
Python String Similarity
A library implementing different string similarity and distance measures using Python.
Stars: ✭ 546 (-6.02%)
Mutual labels:  algorithm
Laminar
A simple semi-reliable UDP protocol for multiplayer games
Stars: ✭ 530 (-8.78%)
Mutual labels:  gamedev
Lean
Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
Stars: ✭ 5,675 (+876.76%)
Mutual labels:  algorithm
Jumper
Fast, lightweight and easy-to-use pathfinding library for grid-based games
Stars: ✭ 540 (-7.06%)
Mutual labels:  gamedev
Crypto Trader
💰 Cryptocurrency trading bot library with a simple example strategy (trading via Gemini).
Stars: ✭ 554 (-4.65%)
Mutual labels:  algorithm
Texturepanner
This repository hosts a shader for Unity3D whose main goal is to facilitate the creation of neon-like signs, conveyor belts and basically whatever based on scrolling textures
Stars: ✭ 528 (-9.12%)
Mutual labels:  gamedev
Entt
Gaming meets modern C++ - a fast and reliable entity component system (ECS) and much more
Stars: ✭ 6,017 (+935.63%)
Mutual labels:  gamedev
3dworld
3D Procedural Game Engine Using OpenGL
Stars: ✭ 527 (-9.29%)
Mutual labels:  procedural-generation
Lintcode
📘 C++11 Solutions of All 289 LintCode Problems (No More Updates)
Stars: ✭ 570 (-1.89%)
Mutual labels:  algorithm
Entitas Csharp
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
Stars: ✭ 5,393 (+828.23%)
Mutual labels:  gamedev
Solid
🎯 A comprehensive gradient-free optimization framework written in Python
Stars: ✭ 546 (-6.02%)
Mutual labels:  algorithm
Rlsl
Rust to SPIR-V compiler
Stars: ✭ 546 (-6.02%)
Mutual labels:  gamedev

ConvChain is a Markov chain of images that converges to input-like images. That is, the distribution of NxN patterns in the outputs converges to the distribution of NxN patterns in the input as the process goes on.

In the examples a typical value of N is 3.

main collage

main gif

How to construct such a process? We want the final probability of a given pattern to be proportional to the pattern's weight, where pattern's weight is the number of such patterns in the input. For this it is sufficient that a stronger condition is satisfied: the probability of a given state (image) S should be proportional to the product of pattern weights over all patterns in S.

p(S) ~ product of pattern weights over all patterns in S

Fortunately, there are general methods to build a Markov chain that has the desired probability distribution over states as its stationary distribution.

Additional definitions:

  1. For the ease of reasoning about the algorithm, it's convenient to introduce an energy function E, E(S) := - sum over all patterns P in S of log(weight(P)) so the probability distribution over states becomes p(S) ~ exp(-E(S)). Note that this energy function is a generalization of the Ising model energy. In the Ising model patterns are 1x2 instead of NxN.
  2. To expand possible applications, it's convenient to introduce a temperature parameter T so the probability distribution over states becomes p(S) ~ exp(-E(S)/T). Low temperatures make the distribution more concentrated in energy wells, high temperatures make the distribution more uniform. If one uses ConvChain to generate dungeons, low temperatures correspond to accurate newly built dungeons while high temperatures correspond to ruins.
  3. For the speed of convergence, it's convenient for weights of all patterns to be nonzero. So let's redefine the weight(P) of a pattern P to be the number of patterns P in the input if that number is more than zero and some small number eps otherwise, 0 < eps < 1.

Algorithm

  1. Read the input image and count NxN patterns.
    1. (optional) Augment pattern data with rotations and reflections.
  2. Initialize the image (for example, with independent random values) in some state S0.
  3. Repeat the Metropolis step:
    1. Compute the energy E of the current state S.
    2. Choose a random pixel and change its value. Let's call the resulting state S'.
    3. Compute the energy E' of the state S'.
    4. Compare E' to E. If E' < E assign the current state to be E'. Otherwise, assign the current state to be E' with probability exp(-(E'-E)/T).

If there are more than 2 colors, Gibbs sampling may converge faster than Metropolis:

  1. Repeat the Gibbs step: change the current state S to a state S' according to the probability distribution p(S'|S) ~ exp(-E'/T).

Comments

ConvChain supports constraints, so you can easily combine it with other generators or handcrafted content.

constrained-convchain

In the language of WFC readme ConvChain satisfies strong condition 2 (Strong C2), but not condition 1 (C1).

If you freeze out the system as the Metropolis simulation goes on, you'll get a variant of the simulated annealing algorithm.

The detailed balance condition for ConvChain is exp(-E1/T)p(S2|S1) = exp(-E2/T)p(S1|S2), so both Gibbs p(S2|S1) ~ exp(-E2/T) and Metropolis p(S2|S1) = min(1, exp(-(E2-E1)/T)) chains converge to the desired distribution over states.

Related work

  1. Stuart Geman and Donald Geman, Stochastic Relaxation, Gibbs Distributions, and the Bayesian Restoration of Images, 1984.
  2. Kris Popat and Rosalind W. Picard, Novel cluster-based probability model for texture synthesis, classiffcation, and compression, 1993.
  3. Rupert Paget and I. Dennis Longstaf, Texture Synthesis via a Non-parametric Markov Random Field, 1995.
  4. Vivek Kwatra, Irfan Essa, Aaron Bobick and Nipun Kwatra, Texture Optimization for Example-based Synthesis, 2005.

How to build

ConvChain is a console application that depends only on the standard library. Get .NET Core for Windows, Linux or macOS and run

dotnet run ConvChain.csproj

ConvChain.cs contains the basic program, ConvChainFast.cs contains an equivalent faster program (~100 times faster on a 4-core CPU), but in a less human-readable form.

Notable ports, forks and spinoffs

mix

convchain-3d-collage

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].