This commit is contained in:
Louis Heredero 2023-12-04 21:44:05 +01:00
parent 0943bfa193
commit cdd9a08635
2 changed files with 40 additions and 3 deletions

View File

@ -1,5 +1,7 @@
package imagefilters
import java.awt.Color
/**
* This class implements the various image filters
*/
@ -16,8 +18,19 @@ object ImageFilters {
}
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 colorFilter(src: Array[Array[Color]], func: (Color, Int, Int, Int, Int) => Color): Array[Array[Color]] = {
val height: Int = src.length
val width: Int = if (height == 0) 0 else src(0).length
val dst: Array[Array[Color]] = 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, width, height)
}
}
return dst
}
def colorFilter(src: Array[Array[Color]], func: (Color) => Color): Array[Array[Color]] = colorFilter(src, (value, x, y, width, height) => func(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)
@ -72,4 +85,21 @@ object ImageFilters {
def noise(a: Array[Array[Int]], intensity: Double): Array[Array[Int]] = filter(a, (value) => {
Math.max(0, Math.min(255, value + (Math.random()*2-1)*intensity)).toInt
})
def mask(a: Array[Array[Color]], maskImg: Array[Array[Int]]): Array[Array[Color]] = {
val maskHeight: Int = maskImg.length
val maskWidth: Int = if (maskHeight == 0) 0 else maskImg(0).length
colorFilter(a, (col, x, y, width, height) => {
if (x >= maskWidth || y >= maskHeight) col
else {
val factor: Double = maskImg(y)(x)/255.0
new Color(
(col.getRed * factor).toInt,
(col.getGreen * factor).toInt,
(col.getBlue * factor).toInt
)
}
})
}
}

View File

@ -24,7 +24,7 @@ object ImageProcessingApp extends App {
val edges = new ImageGraphics("./res/rice.jpg", "Edges", 500, 250)
edges.setPixelsBW(ImageFilters.edges(edges.getPixelsBW()))*/
val imageFile: String = "./res/grace_hopper.jpg"
/*val imageFile: String = "./res/grace_hopper.jpg"
val org = new ImageGraphics(imageFile, "Original", -768, -512)
val bw = new ImageGraphics(imageFile, "Black & White", -256, -512)
val threshold = new ImageGraphics(imageFile, "Threshold", 256, -512)
@ -38,5 +38,12 @@ object ImageProcessingApp extends App {
blur.setPixelsBW(ImageFilters.mean(org.getPixelsBW(), 3))
edges.setPixelsBW(ImageFilters.edges(org.getPixelsBW()))
sobel.setPixelsBW(ImageFilters.sobel(org.getPixelsBW(), 0.3))
noise.setPixelsBW(ImageFilters.noise(org.getPixelsBW(), 30))
noise.setPixelsBW(ImageFilters.noise(org.getPixelsBW(), 30))*/
val imageFile: String = "./res/collins_eileen.png"
val maskFile: String = "./res/mask.png"
val org = new ImageGraphics(imageFile, "Original", -768, -512)
val mask = new ImageGraphics(maskFile, "Mask", -256, -512)
val masked = new ImageGraphics(imageFile, "Masked", 256, -512)
masked.setPixelsColor(ImageFilters.mask(org.getPixelsColor(), mask.getPixelsBW()))
}