【问题标题】:what does "argmax().I" mean in Julia“argmax().I”在 Julia 中是什么意思
【发布时间】:2021-07-12 22:42:40
【问题描述】:

这是来自StatWithJuliaBook 的一个很好的例子(请查找以下内容)

它演示了如何平滑星空stars.png

我的问题是关于argmax().I。根据作者的说法,“请注意在每个 argmax 末尾使用尾随“.I”,它会提取列主坐标的值。”

这是什么意思?还有其他参数吗?我在文档中找不到任何描述。

根据作者,这似乎是按列最大值的位置,但是当我尝试argmax(gImg, dims=2)时,结果不同。

#julia> yOriginal, xOriginal = argmax(gImg).I
#(192, 168)


#julia> yy, xx = argmax(gImg, dims = 2)
#400×1 Matrix{CartesianIndex{2}}:
# CartesianIndex(1, 187)
# CartesianIndex(2, 229)
 ⋮
# CartesianIndex(399, 207)
# CartesianIndex(400, 285)

#julia> yy, xx
#(CartesianIndex(1, 187), CartesianIndex(2, 229))

请指教。

using Plots, Images; pyplot()

img = load("stars.png")
gImg = red.(img)*0.299 + green.(img)*0.587 + blue.(img)*0.114
rows, cols = size(img)

println("Highest intensity pixel: ", findmax(gImg))

function boxBlur(image,x,y,d)
    if x<=d || y<=d || x>=cols-d || y>=rows-d
        return image[x,y]
    else
        total = 0.0
        for xi = x-d:x+d
            for yi = y-d:y+d
                total += image[xi,yi]
            end
        end
        return total/((2d+1)^2)
    end
end

blurImg = [boxBlur(gImg,x,y,5) for x in 1:cols, y in 1:rows]

yOriginal, xOriginal = argmax(gImg).I
yBoxBlur, xBoxBlur   = argmax(blurImg).I

p1 = heatmap(gImg, c=:Greys, yflip=true)
p1 = scatter!((xOriginal, yOriginal), ms=60, ma=0, msw=4, msc=:red) 
p2 = heatmap(blurImg, c=:Greys, yflip=true)
p2 = scatter!((xBoxBlur, yBoxBlur), ms=60, ma=0, msw=4, msc=:red)

plot(p1, p2, size=(800, 400), ratio=:equal, xlims=(0,cols), ylims=(0,rows), 
    colorbar_entry=false, border=:none, legend=:none)

【问题讨论】:

    标签: julia


    【解决方案1】:

    ICartesianIndex 类型对象中的一个字段,当其参数具有多于一维时,argmax 会返回该字段。

    如有疑问,请始终尝试使用dump

    请考虑以下代码:

    julia> arr = rand(4,4)
    4×4 Matrix{Float64}:
     0.971271  0.0350186  0.20805   0.284678
     0.348161  0.19649    0.30343   0.291894
     0.385583  0.990593   0.216894  0.814146
     0.283823  0.750008   0.266643  0.473104
    
    julia> el = argmax(arr)
    CartesianIndex(3, 2)
    
    julia> dump(el)
    CartesianIndex{2}
      I: Tuple{Int64, Int64}
        1: Int64 3
        2: Int64 2
    

    但是,通过其内部结构获取CartesianIndex 对象数据并不是很优雅。 Julian 的好方法是使用适当的方法:

    julia> Tuple(el)
    (3, 2)
    

    或者直接访问索引:

    julia> el[1], el[2]
    (3, 2)
    

    【讨论】:

    • 我对元组类型不是很熟悉。根据定义,它是一个“有序且不可变”的序列。对我来说,元组是原始序列的有序版本。不仅如此,它还给出了位置标记,比如说,哪一个是 argmax() 中的最大值。伟大的!我想我的图片更好,感谢“转储”提示,非常感谢。
    猜你喜欢
    • 2019-06-25
    • 2015-09-18
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 2015-10-08
    • 2014-05-06
    相关资源
    最近更新 更多