This commit is contained in:
Louis Heredero 2023-12-04 17:16:24 +01:00
parent ddddbfe558
commit 54fa312113
2 changed files with 21 additions and 1 deletions

View File

@ -21,7 +21,7 @@ object ImageFilters {
def duplicate(a: Array[Array[Int]]): Array[Array[Int]] = filter(a, (value) => value) def duplicate(a: Array[Array[Int]]): Array[Array[Int]] = filter(a, (value) => value)
def threshold(a: Array[Array[Int]], thresh: Int): Array[Array[Int]] = filter(a, (value) => if (value > thresh) 255 else 0) def threshold(a: Array[Array[Int]], thresh: Int): Array[Array[Int]] = filter(a, (value) => if (value > thresh) 255 else 0)
def mean(a: Array[Array[Int]]): Array[Array[Int]] = filter(a, (value: Int, x, y, width, height) => { def mean(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 if (x == 0 || x == width - 1 || y == 0 || y == height - 1) value
else { else {
var avg: Double = 0 var avg: Double = 0
@ -33,4 +33,21 @@ object ImageFilters {
(avg/9.0).toInt (avg/9.0).toInt
} }
}) })
def mean(a: Array[Array[Int]], radius: Int): Array[Array[Int]] = {
if (radius < 0) throw new IllegalArgumentException("radius must be >= 0")
val diameter: Int = 2 * radius + 1
filter(a, (value, x, y, width, height) => {
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)
}
}
(avg/(diameter * diameter)).toInt
}
})
}
} }

View File

@ -17,4 +17,7 @@ object ImageProcessingApp extends App {
val mean = new ImageGraphics("./res/imageProcessing.jpg", "Mean", 0, 250) val mean = new ImageGraphics("./res/imageProcessing.jpg", "Mean", 0, 250)
mean.setPixelsBW(ImageFilters.mean(mean.getPixelsBW())) mean.setPixelsBW(ImageFilters.mean(mean.getPixelsBW()))
val mean2 = new ImageGraphics("./res/imageProcessing.jpg", "Mean 2", 250, 250)
mean2.setPixelsBW(ImageFilters.mean(mean2.getPixelsBW(), 4))
} }