Demos: ensure all use fun main() = application {

- Adjust some demo window sizes.
- Replace Random.double by Double.uniform
- Tweak some demos so screenshots look more interesting
This commit is contained in:
Abe Pazos
2025-01-26 20:57:04 +01:00
parent 1975a820fc
commit c8f7dd52c6
116 changed files with 2889 additions and 2942 deletions

View File

@@ -12,54 +12,56 @@ import org.openrndr.draw.loadImage
import kotlin.math.cos
import kotlin.math.sin
fun main() {
application {
program {
// Load an image, convert to BoofCV format using orx-boofcv
val input = loadImage("demo-data/images/image-001.png").toGrayF32()
fun main() = application {
program {
// Load an image, convert to BoofCV format using orx-boofcv
val input = loadImage("demo-data/images/image-001.png").toGrayF32()
// BoofCV: calculate a good threshold for the loaded image
val threshold = GThresholdImageOps.computeOtsu(input, 0.0, 255.0)
// BoofCV: calculate a good threshold for the loaded image
val threshold = GThresholdImageOps.computeOtsu(input, 0.0, 255.0)
// BoofCV: use the threshold to convert the image to black and white
val binary = GrayU8(input.width, input.height)
ThresholdImageOps.threshold(input, binary, threshold.toFloat(), false)
// BoofCV: use the threshold to convert the image to black and white
val binary = GrayU8(input.width, input.height)
ThresholdImageOps.threshold(input, binary, threshold.toFloat(), false)
// BoofCV: Contract and expand the white areas to remove noise
var filtered = BinaryImageOps.erode8(binary, 1, null)
filtered = BinaryImageOps.dilate8(filtered, 1, null)
// BoofCV: Contract and expand the white areas to remove noise
var filtered = BinaryImageOps.erode8(binary, 1, null)
filtered = BinaryImageOps.dilate8(filtered, 1, null)
// BoofCV: Calculate contours as vector data
val contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT, null)
// BoofCV: Calculate contours as vector data
val contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT, null)
// orx-boofcv: convert vector data to OPENRNDR ShapeContours
val externalShapes = contours.toShapeContours(true,
internal = false, external = true)
val internalShapes = contours.toShapeContours(true,
internal = true, external = false)
// orx-boofcv: convert vector data to OPENRNDR ShapeContours
val externalShapes = contours.toShapeContours(
true,
internal = false, external = true
)
val internalShapes = contours.toShapeContours(
true,
internal = true, external = false
)
extend {
drawer.run {
// Zoom in and out over time
translate(bounds.center)
scale(1.5 + 0.5 * cos(seconds * 0.2))
translate(-bounds.center)
extend {
drawer.run {
// Zoom in and out over time
translate(bounds.center)
scale(1.5 + 0.5 * cos(seconds * 0.2))
translate(-bounds.center)
stroke = null
stroke = null
// Draw all external shapes
fill = rgb(0.2)
contours(externalShapes)
// Draw all external shapes
fill = rgb(0.2)
contours(externalShapes)
// Draw internal shapes one by one to set unique colors
internalShapes.forEachIndexed { i, shp ->
val shade = 0.2 + (i % 7) * 0.1 +
0.1 * sin(i + seconds)
fill = ColorRGBa.PINK.shade(shade)
contour(shp)
}
// Draw internal shapes one by one to set unique colors
internalShapes.forEachIndexed { i, shp ->
val shade = 0.2 + (i % 7) * 0.1 +
0.1 * sin(i + seconds)
fill = ColorRGBa.PINK.shade(shade)
contour(shp)
}
}
}
}
}
}

View File

@@ -3,24 +3,21 @@ import org.openrndr.boofcv.binding.resizeBy
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.loadImage
fun main() = application {
program {
// Load an image, convert to BoofCV format using orx-boofcv
val input = loadImage("demo-data/images/image-001.png")
fun main() {
application {
program {
// Load an image, convert to BoofCV format using orx-boofcv
val input = loadImage("demo-data/images/image-001.png")
val scaled = input.resizeBy(0.5)
val scaled2 = input.resizeBy(0.25, convertToGray = true)
val scaled3 = input.resizeBy(0.1)
val scaled = input.resizeBy(0.5)
val scaled2 = input.resizeBy(0.25, convertToGray = true)
val scaled3 = input.resizeBy(0.1)
extend {
drawer.clear(ColorRGBa.BLACK)
drawer.translate(0.0, (height - scaled.bounds.height) / 2.0)
drawer.image(scaled)
drawer.image(scaled2, scaled.bounds.width, scaled.bounds.height - scaled2.height)
drawer.image(scaled3, scaled.bounds.width + scaled2.bounds.width, scaled.bounds.height - scaled3.height)
}
extend {
drawer.clear(ColorRGBa.BLACK)
drawer.translate(0.0, (height - scaled.bounds.height) / 2.0)
drawer.image(scaled)
drawer.image(scaled2, scaled.bounds.width, scaled.bounds.height - scaled2.height)
drawer.image(scaled3, scaled.bounds.width + scaled2.bounds.width, scaled.bounds.height - scaled3.height)
}
}
}
}

View File

@@ -3,23 +3,20 @@ import org.openrndr.boofcv.binding.resizeTo
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.loadImage
fun main() = application {
program {
// Load an image, convert to BoofCV format using orx-boofcv
val input = loadImage("demo-data/images/image-001.png")
val scaled = input.resizeTo(input.width / 3)
val scaled2 = input.resizeTo(newHeight = input.height / 4, convertToGray = true)
val scaled3 = input.resizeTo(input.width / 5, input.height / 5)
fun main() {
application {
program {
// Load an image, convert to BoofCV format using orx-boofcv
val input = loadImage("demo-data/images/image-001.png")
val scaled = input.resizeTo(input.width / 3)
val scaled2 = input.resizeTo(newHeight = input.height / 4, convertToGray = true)
val scaled3 = input.resizeTo(input.width / 5, input.height / 5)
extend {
drawer.clear(ColorRGBa.BLACK)
drawer.translate(0.0, (height - scaled.bounds.height) / 2.0)
drawer.image(scaled)
drawer.image(scaled2, scaled.bounds.width, scaled.bounds.height - scaled2.height)
drawer.image(scaled3, scaled.bounds.width + scaled2.bounds.width, scaled.bounds.height - scaled3.height)
}
extend {
drawer.clear(ColorRGBa.BLACK)
drawer.translate(0.0, (height - scaled.bounds.height) / 2.0)
drawer.image(scaled)
drawer.image(scaled2, scaled.bounds.width, scaled.bounds.height - scaled2.height)
drawer.image(scaled3, scaled.bounds.width + scaled2.bounds.width, scaled.bounds.height - scaled3.height)
}
}
}
}

View File

@@ -17,63 +17,65 @@ import org.openrndr.math.Vector2
import org.openrndr.shape.Rectangle
import org.openrndr.shape.ShapeContour
fun main() {
application {
program {
// Create a buffer where to draw something for boofcv
val rt = renderTarget(width, height) {
colorBuffer()
depthBuffer()
}
// Draw some shapes on that buffer
drawer.isolatedWithTarget(rt) {
clear(ColorRGBa.BLACK)
fill = ColorRGBa.WHITE
stroke = null
rectangle(Rectangle.fromCenter(bounds.position(0.33, 0.5),
150.0, 150.0))
translate(bounds.position(0.62, 0.5))
rotate(30.0)
rectangle(Rectangle.fromCenter(Vector2.ZERO, 200.0, 200.0))
rectangle(0.0, -200.0, 60.0, 60.0)
circle(0.0, 190.0, 60.0)
}
// Convert the bitmap buffer into ShapeContours
val vectorized = imageToContours(rt.colorBuffer(0))
fun main() = application {
program {
// Create a buffer where to draw something for boofcv
val rt = renderTarget(width, height) {
colorBuffer()
depthBuffer()
}
// Draw some shapes on that buffer
drawer.isolatedWithTarget(rt) {
clear(ColorRGBa.BLACK)
fill = ColorRGBa.WHITE
stroke = null
rectangle(
Rectangle.fromCenter(
bounds.position(0.33, 0.5),
150.0, 150.0
)
)
translate(bounds.position(0.62, 0.5))
rotate(30.0)
rectangle(Rectangle.fromCenter(Vector2.ZERO, 200.0, 200.0))
rectangle(0.0, -200.0, 60.0, 60.0)
circle(0.0, 190.0, 60.0)
}
// Convert the bitmap buffer into ShapeContours
val vectorized = imageToContours(rt.colorBuffer(0))
// Show amount of segments in each shape (high number)
vectorized.forEachIndexed { i, it ->
println("boofcv shape $i: ${it.segments.size} segments")
}
// Show amount of segments in each shape (high number)
vectorized.forEachIndexed { i, it ->
println("boofcv shape $i: ${it.segments.size} segments")
}
// Make a simplified list of points
val simplePoints = vectorized.map {
simplify(it.adaptivePositions(), 4.0)
}.filter { it.size >= 3 }
// Make a simplified list of points
val simplePoints = vectorized.map {
simplify(it.adaptivePositions(), 4.0)
}.filter { it.size >= 3 }
// Use the simplified list to make a smooth contour
val smooth = simplePoints.map {
CatmullRomChain2(it, 0.0, true).toContour()
}
// Use the simplified list to make a smooth contour
val smooth = simplePoints.map {
CatmullRomChain2(it, 0.0, true).toContour()
}
// Use the simplified list to make a polygonal contour
val polygonal = simplePoints.map {
ShapeContour.fromPoints(it, true)
}
// Use the simplified list to make a polygonal contour
val polygonal = simplePoints.map {
ShapeContour.fromPoints(it, true)
}
// Show amount of segments in simplified shapes (low number).
// Note: `smooth` and `polygonal` have the same number of segments
smooth.forEachIndexed { i, it ->
println("simplified shape $i: ${it.segments.size} segments")
}
// Show amount of segments in simplified shapes (low number).
// Note: `smooth` and `polygonal` have the same number of segments
smooth.forEachIndexed { i, it ->
println("simplified shape $i: ${it.segments.size} segments")
}
extend {
drawer.run {
fill = null // ColorRGBa.PINK.opacify(0.15)
stroke = ColorRGBa.PINK.opacify(0.7)
contours(polygonal)
contours(smooth)
}
extend {
drawer.run {
fill = null // ColorRGBa.PINK.opacify(0.15)
stroke = ColorRGBa.PINK.opacify(0.7)
contours(polygonal)
contours(smooth)
}
}
}
@@ -97,4 +99,4 @@ fun imageToContours(input: ColorBuffer): List<ShapeContour> {
// orx-boofcv: convert vector data to OPENRNDR ShapeContours
return contours.toShapeContours(true, internal = true, external = true)
}
}