This commit is contained in:
Louis Heredero 2023-12-04 16:14:07 +01:00
parent 9454948799
commit ddddbfe558
2 changed files with 22 additions and 4 deletions

View File

@ -4,18 +4,33 @@ package imagefilters
* This class implements the various image filters * This class implements the various image filters
*/ */
object ImageFilters { object ImageFilters {
def filter(src: Array[Array[Int]], func: (Int, Int, Int) => Int): Array[Array[Int]] = {
def filter(src: Array[Array[Int]], func: (Int, Int, Int, Int, Int) => Int): Array[Array[Int]] = {
val height: Int = src.length val height: Int = src.length
val width: Int = if (height == 0) 0 else src(0).length val width: Int = if (height == 0) 0 else src(0).length
val dst: Array[Array[Int]] = Array.ofDim(height, width) val dst: Array[Array[Int]] = Array.ofDim(height, width)
for (y: Int <- 0 until height) { for (y: Int <- 0 until height) {
for (x: Int <- 0 until width) { for (x: Int <- 0 until width) {
dst(y)(x) = func(src(y)(x), x, y) dst(y)(x) = func(src(y)(x), x, y, width, height)
} }
} }
return dst return dst
} }
def filter(src: Array[Array[Int]], func: (Int, Int, Int) => Int): Array[Array[Int]] = filter(src, (value, x, y, width, height) => func(value, x, y))
def filter(src: Array[Array[Int]], func: (Int) => Int): Array[Array[Int]] = filter(src, (value, x, y, width, height) => func(value))
def duplicate(a: Array[Array[Int]]): Array[Array[Int]] = filter(a, (value, x, y) => 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, x, y) => 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) => {
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)
}
}
(avg/9.0).toInt
}
})
} }

View File

@ -14,4 +14,7 @@ object ImageProcessingApp extends App {
dest.setPixelsBW(newPixels) dest.setPixelsBW(newPixels)
thresh.setPixelsBW(ImageFilters.threshold(newPixels, 128)) thresh.setPixelsBW(ImageFilters.threshold(newPixels, 128))
val mean = new ImageGraphics("./res/imageProcessing.jpg", "Mean", 0, 250)
mean.setPixelsBW(ImageFilters.mean(mean.getPixelsBW()))
} }