image - 如何在 Julia 中模糊图像?

标签 image julia blur

我有一个图像,已作为像素数组加载(使用 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

例如: enter image description here enter image description here

关于image - 如何在 Julia 中模糊图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68788453/

相关文章:

optimization - 在 Julia 中使用导入的 Scipy 函数时出现语法错误

julia - UndefVar错误: Images not defined VS Code Julia

image-processing - Julia 的索贝尔算子

keyboard - 单击 Titanium 中 tableView 内的文本字段时隐藏键盘(模糊)

CSS3技巧: blur entire webpage but without softened borders?

image - PyQT4 - 在图像上绘画以进行区域选择

php - 图片 - 上传没有响应,无法访问 $_FILES

Reactjs 如何模糊滚动上的背景图像?

swift - 如何在下载后使用照片而无需再次下载?(Swift)

image - Aframe - 实体前透明的 PNG