From 54fa3121138ca5e1a1b5575051d3f77ac193c3b5 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 4 Dec 2023 17:16:24 +0100 Subject: [PATCH] task 4.2 --- src/imagefilters/ImageFilters.scala | 19 ++++++++++++++++++- src/imagefilters/ImageProcessingApp.scala | 3 +++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/imagefilters/ImageFilters.scala b/src/imagefilters/ImageFilters.scala index de946c6..7656a94 100644 --- a/src/imagefilters/ImageFilters.scala +++ b/src/imagefilters/ImageFilters.scala @@ -21,7 +21,7 @@ object ImageFilters { 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 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 else { var avg: Double = 0 @@ -33,4 +33,21 @@ object ImageFilters { (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 + } + }) + } } diff --git a/src/imagefilters/ImageProcessingApp.scala b/src/imagefilters/ImageProcessingApp.scala index e92af5d..f01b05e 100644 --- a/src/imagefilters/ImageProcessingApp.scala +++ b/src/imagefilters/ImageProcessingApp.scala @@ -17,4 +17,7 @@ object ImageProcessingApp extends App { val mean = new ImageGraphics("./res/imageProcessing.jpg", "Mean", 0, 250) mean.setPixelsBW(ImageFilters.mean(mean.getPixelsBW())) + + val mean2 = new ImageGraphics("./res/imageProcessing.jpg", "Mean 2", 250, 250) + mean2.setPixelsBW(ImageFilters.mean(mean2.getPixelsBW(), 4)) }