Convert orx-shader-phrases and orx-noise to MPP
This commit is contained in:
74
orx-jvm/orx-boofcv/src/demo/kotlin/DemoContours01.kt
Normal file
74
orx-jvm/orx-boofcv/src/demo/kotlin/DemoContours01.kt
Normal file
@@ -0,0 +1,74 @@
|
||||
import boofcv.alg.filter.binary.BinaryImageOps
|
||||
import boofcv.alg.filter.binary.GThresholdImageOps
|
||||
import boofcv.alg.filter.binary.ThresholdImageOps
|
||||
import boofcv.struct.ConnectRule
|
||||
import boofcv.struct.image.GrayU8
|
||||
import org.openrndr.application
|
||||
import org.openrndr.boofcv.binding.toGrayF32
|
||||
import org.openrndr.boofcv.binding.toShapeContours
|
||||
import org.openrndr.color.ColorRGBa
|
||||
import org.openrndr.color.rgb
|
||||
import org.openrndr.draw.loadImage
|
||||
import org.openrndr.extensions.SingleScreenshot
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.sin
|
||||
|
||||
suspend fun main() {
|
||||
application {
|
||||
program {
|
||||
|
||||
// -- this block is for automation purposes only
|
||||
if (System.getProperty("takeScreenshot") == "true") {
|
||||
extend(SingleScreenshot()) {
|
||||
this.outputFile = System.getProperty("screenshotPath")
|
||||
}
|
||||
}
|
||||
|
||||
// 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: 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: 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)
|
||||
|
||||
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
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
33
orx-jvm/orx-boofcv/src/demo/kotlin/DemoResize01.kt
Normal file
33
orx-jvm/orx-boofcv/src/demo/kotlin/DemoResize01.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
import org.openrndr.application
|
||||
import org.openrndr.boofcv.binding.*
|
||||
import org.openrndr.color.ColorRGBa
|
||||
import org.openrndr.draw.loadImage
|
||||
import org.openrndr.extensions.SingleScreenshot
|
||||
|
||||
|
||||
suspend fun main() {
|
||||
application {
|
||||
program {
|
||||
if (System.getProperty("takeScreenshot") == "true") {
|
||||
extend(SingleScreenshot()) {
|
||||
this.outputFile = System.getProperty("screenshotPath")
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
orx-jvm/orx-boofcv/src/demo/kotlin/DemoResize02.kt
Normal file
32
orx-jvm/orx-boofcv/src/demo/kotlin/DemoResize02.kt
Normal file
@@ -0,0 +1,32 @@
|
||||
import org.openrndr.application
|
||||
import org.openrndr.boofcv.binding.*
|
||||
import org.openrndr.color.ColorRGBa
|
||||
import org.openrndr.draw.loadImage
|
||||
import org.openrndr.extensions.SingleScreenshot
|
||||
|
||||
|
||||
suspend fun main() {
|
||||
application {
|
||||
program {
|
||||
if (System.getProperty("takeScreenshot") == "true") {
|
||||
extend(SingleScreenshot()) {
|
||||
this.outputFile = System.getProperty("screenshotPath")
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
109
orx-jvm/orx-boofcv/src/demo/kotlin/DemoSimplified01.kt
Normal file
109
orx-jvm/orx-boofcv/src/demo/kotlin/DemoSimplified01.kt
Normal file
@@ -0,0 +1,109 @@
|
||||
import boofcv.alg.filter.binary.BinaryImageOps
|
||||
import boofcv.alg.filter.binary.GThresholdImageOps
|
||||
import boofcv.alg.filter.binary.ThresholdImageOps
|
||||
import boofcv.struct.ConnectRule
|
||||
import boofcv.struct.image.GrayU8
|
||||
import org.openrndr.application
|
||||
import org.openrndr.boofcv.binding.toGrayF32
|
||||
import org.openrndr.boofcv.binding.toShapeContours
|
||||
import org.openrndr.color.ColorRGBa
|
||||
import org.openrndr.draw.ColorBuffer
|
||||
import org.openrndr.draw.isolatedWithTarget
|
||||
import org.openrndr.draw.renderTarget
|
||||
import org.openrndr.extensions.SingleScreenshot
|
||||
import org.openrndr.math.CatmullRomChain2
|
||||
import org.openrndr.math.Vector2
|
||||
import org.openrndr.shape.Rectangle
|
||||
import org.openrndr.shape.ShapeContour
|
||||
import org.openrndr.shape.simplify
|
||||
import org.openrndr.shape.toContour
|
||||
|
||||
suspend fun main() {
|
||||
application {
|
||||
program {
|
||||
|
||||
// -- this block is for automation purposes only
|
||||
if (System.getProperty("takeScreenshot") == "true") {
|
||||
extend(SingleScreenshot()) {
|
||||
this.outputFile = System.getProperty("screenshotPath")
|
||||
}
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// 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 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")
|
||||
}
|
||||
|
||||
extend {
|
||||
drawer.run {
|
||||
fill = null // ColorRGBa.PINK.opacify(0.15)
|
||||
stroke = ColorRGBa.PINK.opacify(0.7)
|
||||
contours(polygonal)
|
||||
contours(smooth)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun imageToContours(input: ColorBuffer): List<ShapeContour> {
|
||||
val bitmap = input.toGrayF32()
|
||||
// BoofCV: calculate a good threshold for the loaded image
|
||||
val threshold = GThresholdImageOps.computeOtsu(bitmap, 0.0, 255.0)
|
||||
|
||||
// BoofCV: use the threshold to convert the image to black and white
|
||||
val binary = GrayU8(bitmap.width, bitmap.height)
|
||||
ThresholdImageOps.threshold(bitmap, 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: Calculate contours as vector data
|
||||
val contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT, null)
|
||||
|
||||
// orx-boofcv: convert vector data to OPENRNDR ShapeContours
|
||||
return contours.toShapeContours(true, internal = true, external = true)
|
||||
}
|
||||
Reference in New Issue
Block a user