我有一个图像,已作为像素数组加载(使用 Image.jl)。如何使用一个简单的函数来模糊该图像,该函数仅将像素与一定数量的周围像素进行平均?
最佳答案
这是一个简单的 blurred(img, n)
函数,该函数使用周围的 n
像素模糊图像中的每个像素。
这里唯一棘手的一点是决定在边缘做什么。在本例中,我镜像了边缘(通过 getpixel
),我认为这提供了不错的模糊效果。
不过,这个算法非常幼稚,因此当 n 远大于 50 左右时,它的性能会很差......
# --------------------------
# using Images, FileIO
#
# include("blur.jl")
#
# img = FileIO.load("img.png")
#
# FileIO.save("blurred.png", blurred(img, 20))
# --------------------------
using Statistics: mean
function blurred(image, n)
reshape([
blurred_px(image, x,y, n)
for x in 1:size(image)[1], y in 1:size(image)[2]
], size(image))
end
function blurred_px(image, x,y, n)
mean(
getpixel(image, i, j)
for i in x-n:x+n, j in y-n:y+n
)
end
function getpixel(image, x,y)
w,h = size(image)
# mirror over top/left
x,y = abs(x-1)+1, abs(y-1)+1
# mirror over the bottom/right
if x > w
x = w+ (w - x)
end
if y > h
y = h+(h - y)
end
return image[x, y]
end
blurred
关于image - 如何在 Julia 中模糊图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68788453/