This commit is contained in:
Louis Heredero 2023-12-04 22:38:37 +01:00
parent f8db769ec3
commit ee6999b095
2 changed files with 51 additions and 2 deletions

View File

@ -102,4 +102,48 @@ object ImageFilters {
}
})
}
def getAvgInPixel(a: Array[Array[Color]], pixelSize: Int, px: Int, py: Int): Color = {
val width: Int = a.length
val height: Int = if (width == 0) 0 else a(0).length
var r: Double = 0
var g: Double = 0
var b: Double = 0
var cnt: Int = 0
for (x: Int <- px*pixelSize until (px+1)*pixelSize) {
for (y: Int <- py*pixelSize until (py+1)*pixelSize) {
if (x < width && y < height) {
cnt += 1
r += a(x)(y).getRed
g += a(x)(y).getGreen
b += a(x)(y).getBlue
}
}
}
return new Color((r/cnt).toInt, (g/cnt).toInt, (b/cnt).toInt)
}
def pixelize(a: Array[Array[Color]], pixelSize: Int): Array[Array[Color]] = {
val width: Int = a.length
val height: Int = if (width == 0) 0 else a(0).length
val hPixels: Int = Math.ceil(width.toDouble/pixelSize).toInt
val vPixels: Int = Math.ceil(height.toDouble/pixelSize).toInt
val pixels: Array[Array[Color]] = Array.ofDim[Color](hPixels, vPixels)
for (x: Int <- 0 until hPixels) {
for (y: Int <- 0 until vPixels) {
pixels(x)(y) = getAvgInPixel(a, pixelSize, x, y)
}
}
colorFilter(a, (col, x, y, w, h) => pixels(x/pixelSize)(y/pixelSize))
}
def pixelize(a: Array[Array[Color]], pixelSize: Int, x1: Int, y1: Int, x2: Int, y2: Int): Array[Array[Color]] = {
val pixelated: Array[Array[Color]] = pixelize(a, pixelSize)
colorFilter(a, (col, x, y, w, h) => {
if (x1 <= x && x <= x2 && y1 <= y && y <= y2) {
pixelated(x)(y)
}
else col
})
}
}

View File

@ -40,10 +40,15 @@ object ImageProcessingApp extends App {
sobel.setPixelsBW(ImageFilters.sobel(org.getPixelsBW(), 0.3))
noise.setPixelsBW(ImageFilters.noise(org.getPixelsBW(), 30))*/
val imageFile: String = "./res/collins_eileen.png"
/*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()))
masked.setPixelsColor(ImageFilters.mask(org.getPixelsColor(), mask.getPixelsBW()))*/
val imageFile: String = "./res/grace_hopper.jpg"
val org = new ImageGraphics(imageFile, "Original", -768, -512)
val pixelated = new ImageGraphics(imageFile, "Pixelated", -256, -512)
pixelated.setPixelsColor(ImageFilters.pixelize(org.getPixelsColor(), 30, 130, 120, 400, 230))
}