fixed inverted x and y axes

This commit is contained in:
Louis Heredero 2023-12-04 22:38:24 +01:00
parent cdd9a08635
commit f8db769ec3

View File

@ -8,24 +8,24 @@ import java.awt.Color
object ImageFilters {
def filter(src: Array[Array[Int]], func: (Int, Int, Int, Int, Int) => Int): Array[Array[Int]] = {
val height: Int = src.length
val width: Int = if (height == 0) 0 else src(0).length
val dst: Array[Array[Int]] = Array.ofDim(height, width)
for (y: Int <- 0 until height) {
val width: Int = src.length
val height: Int = if (width == 0) 0 else src(0).length
val dst: Array[Array[Int]] = Array.ofDim(width, height)
for (x: Int <- 0 until width) {
dst(y)(x) = func(src(y)(x), x, y, width, height)
for (y: Int <- 0 until height) {
dst(x)(y) = func(src(x)(y), x, y, width, height)
}
}
return dst
}
def filter(src: Array[Array[Int]], func: (Int) => Int): Array[Array[Int]] = filter(src, (value, x, y, width, height) => func(value))
def colorFilter(src: Array[Array[Color]], func: (Color, Int, Int, Int, Int) => Color): Array[Array[Color]] = {
val height: Int = src.length
val width: Int = if (height == 0) 0 else src(0).length
val dst: Array[Array[Color]] = Array.ofDim(height, width)
for (y: Int <- 0 until height) {
val width: Int = src.length
val height: Int = if (width == 0) 0 else src(0).length
val dst: Array[Array[Color]] = Array.ofDim(width, height)
for (x: Int <- 0 until width) {
dst(y)(x) = func(src(y)(x), x, y, width, height)
for (y: Int <- 0 until height) {
dst(x)(y) = func(src(x)(y), x, y, width, height)
}
}
return dst
@ -38,9 +38,9 @@ object ImageFilters {
if (x == 0 || x == width - 1 || y == 0 || y == height - 1) value
else {
var avg: Double = 0
for (dy: Int <- -1 to 1) {
for (dx: Int <- -1 to 1) {
avg += a(y+dy)(x+dx)
for (dy: Int <- -1 to 1) {
avg += a(x+dx)(y+dy)
}
}
(avg/9.0).toInt
@ -54,9 +54,9 @@ object ImageFilters {
if (x <= radius - 1 || x >= width - radius || y <= radius - 1 || y >= height - radius) value
else {
var avg: Double = 0
for (dy: Int <- -radius to radius) {
for (dx: Int <- -radius to radius) {
avg += a(y+dy)(x+dx)
for (dy: Int <- -radius to radius) {
avg += a(x+dx)(y+dy)
}
}
(avg/(diameter * diameter)).toInt
@ -67,8 +67,8 @@ object ImageFilters {
def edges(a: Array[Array[Int]]): Array[Array[Int]] = filter(a, (value, x, y, width, height) => {
if (x == 0 || x == width - 1 || y == 0 || y == height - 1) value
else {
val dx: Int = a(y)(x+1) - a(y)(x-1)
val dy: Int = a(y+1)(x) - a(y-1)(x)
val dx: Int = a(x+1)(y) - a(x-1)(y)
val dy: Int = a(x)(y+1) - a(x)(y-1)
Math.sqrt(dx*dx + dy*dy).toInt
}
})
@ -76,8 +76,8 @@ object ImageFilters {
def sobel(a: Array[Array[Int]], intensityFactor: Double): Array[Array[Int]] = filter(a, (value, x, y, width, height) => {
if (x == 0 || x == width - 1 || y == 0 || y == height - 1) value
else {
val dx: Int = a(y-1)(x+1) + 2*a(y)(x+1) + a(y+1)(x+1) - a(y-1)(x-1) - 2*a(y)(x-1) - a(y+1)(x-1)
val dy: Int = a(y+1)(x-1) + 2*a(y+1)(x) + a(y+1)(x+1) - a(y-1)(x-1) - 2*a(y-1)(x) - a(y-1)(x+1)
val dx: Int = a(x+1)(y-1) + 2*a(x+1)(y) + a(x+1)(y+1) - a(x-1)(y-1) - 2*a(x-1)(y) - a(x-1)(y+1)
val dy: Int = a(x-1)(y+1) + 2*a(x)(y+1) + a(x+1)(y+1) - a(x-1)(y-1) - 2*a(x)(y-1) - a(x+1)(y-1)
(Math.sqrt(dx*dx + dy*dy)*intensityFactor).toInt
}
})
@ -87,13 +87,13 @@ object ImageFilters {
})
def mask(a: Array[Array[Color]], maskImg: Array[Array[Int]]): Array[Array[Color]] = {
val maskHeight: Int = maskImg.length
val maskWidth: Int = if (maskHeight == 0) 0 else maskImg(0).length
val maskWidth: Int = maskImg.length
val maskHeight: Int = if (maskWidth == 0) 0 else maskImg(0).length
colorFilter(a, (col, x, y, width, height) => {
if (x >= maskWidth || y >= maskHeight) col
else {
val factor: Double = maskImg(y)(x)/255.0
val factor: Double = maskImg(x)(y)/255.0
new Color(
(col.getRed * factor).toInt,
(col.getGreen * factor).toInt,