task 4.1
This commit is contained in:
parent
9454948799
commit
ddddbfe558
@ -4,18 +4,33 @@ package imagefilters
|
||||
* This class implements the various image filters
|
||||
*/
|
||||
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 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) {
|
||||
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
|
||||
}
|
||||
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 threshold(a: Array[Array[Int]], thresh: Int): Array[Array[Int]] = filter(a, (value, x, y) => if (value > thresh) 255 else 0)
|
||||
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) => {
|
||||
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
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -14,4 +14,7 @@ object ImageProcessingApp extends App {
|
||||
dest.setPixelsBW(newPixels)
|
||||
|
||||
thresh.setPixelsBW(ImageFilters.threshold(newPixels, 128))
|
||||
|
||||
val mean = new ImageGraphics("./res/imageProcessing.jpg", "Mean", 0, 250)
|
||||
mean.setPixelsBW(ImageFilters.mean(mean.getPixelsBW()))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user