task 4.2
This commit is contained in:
parent
ddddbfe558
commit
54fa312113
@ -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
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user