- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想找到最多几个图像:将它们加载到一个数组中并沿第一维找到最大值。
Python代码例如:
import cv2
import sys
import numpy as np
imgs_paths = sys.argv[1:]
imgs = list(map(cv2.imread, imgs_paths))
imgs_arr = np.array(imgs, dtype=np.float32)
imgs_max = np.max(imgs_arr, 0)
using Colors, Images
function im_to_array(im)
img_array = permutedims(channelview(im), (2,3,1))
img_array = Float32.(img_array)
return img_array
end
imgs = map(Images.load, imgs_paths)
imgs_arr = map(im_to_array, imgs)
a = imgs_arr
b = reshape(cat(a..., dims=1), tuple(length(a), size(a[1])...))
imgs_max = maximum(b, dims=1)
function im_to_array(im)
img_array = permutedims(channelview(im), (2,3,1))
img_array = Float32.(img_array)
return img_array
end
imgs = map(Images.load, imgs_paths)
imgs_arr = map(im_to_array, imgs)
imgs_max = max.(imgs_arr...)
a = [zeros(Int8, 8, 8, 3), zeros(Int8, 8, 8, 3), zeros(Int8, 8, 8, 3)] # 3 black images with shape 8x8
max.(a) #doesn't work
max.(a...) #works with this simple input but when I test it on 120 FHD images it's extremely slow
function max1(imgs_arr)
a = imgs_arr
b = reshape(cat(a..., dims=1), tuple(length(a), size(a[1])...))
imgs_max = maximum(b, dims=1)
return imgs_max
end
function max2(imgs_arr)
return max.(imgs_arr...)
end
imgs_arr = my_imgs_arrays[1:5]
@time max1(imgs_arr)
@time max2(imgs_arr)
0.247060 seconds (5.29 k allocations: 142.657 MiB)
0.154158 seconds (44.85 k allocations: 26.388 MiB)
imgs_arr = my_imgs_arrays[1:15]
@time max1(imgs_arr)
@time max2(imgs_arr)
0.600093 seconds (72.38 k allocations: 382.923 MiB)
0.769446 seconds (1.24 M allocations: 71.374 MiB)
imgs_arr = my_imgs_arrays[1:25]
@time max1(imgs_arr)
@time max2(imgs_arr)
1.057548 seconds (23.08 k allocations: 618.309 MiB)
5.270050 seconds (151.52 M allocations: 2.329 GiB, 4.77% gc time)
最佳答案
似乎您希望对多个图像进行成对最大缩减。首先,这是一个生成随机“图像”的函数:
rand_images(k, dims...) = [rand(UInt8, dims...) for _ = 1:k]
julia> images = rand_images(3, 10, 12)
3-element Array{Array{UInt8,2},1}:
[0x51 0xdc … 0xf7 0x1e; 0xe1 0x10 … 0xd8 0x98; … ; 0x54 0x45 … 0x7a 0xaf; 0x7b 0xfc … 0x0a 0x81]
[0xc8 0xa5 … 0xa8 0x81; 0x92 0x89 … 0x9f 0xbe; … ; 0x6a 0x03 … 0xb1 0xfd; 0x34 0xa9 … 0xa3 0x50]
[0x26 0x9b … 0x2a 0x7c; 0x5c 0x7d … 0x8d 0x2b; … ; 0x32 0x1b … 0x57 0xdf; 0x96 0xa1 … 0x2a 0xc9]
julia> using BenchmarkTools
julia> @btime reduce(images) do a, b
max.(a, b)
end
400.485 ns (2 allocations: 416 bytes)
10×12 Array{UInt8,2}:
0xc8 0xdc 0x82 0xa7 0xa6 0xce 0xcd 0xb2 0x6e 0xba 0xf7 0x81
0xe1 0x89 0x9f 0xeb 0x89 0xdf 0xd2 0xd2 0xab 0xea 0xd8 0xbe
0xeb 0xdd 0x9e 0xe2 0xf5 0x4b 0xd2 0xe8 0xe4 0xf8 0xb9 0xf8
0x63 0xa3 0xd7 0xea 0xf0 0x93 0xed 0xf7 0xfb 0xfb 0x9f 0xbb
0xf2 0x51 0xf0 0xd4 0xfc 0xcf 0xf4 0xdd 0xeb 0xc3 0xe9 0xf9
0xf8 0x72 0xfa 0x92 0x72 0xaa 0xa2 0xed 0xa1 0xdf 0xf1 0xd0
0xef 0xe6 0x64 0xb3 0xd0 0x6a 0xce 0x9e 0x96 0xba 0xed 0xf9
0xdb 0xc5 0x52 0xb3 0xf7 0xd1 0xdd 0xba 0xac 0xbc 0xd3 0xa1
0x6a 0x45 0x88 0xda 0xf5 0xc6 0xcf 0x64 0xbc 0xf9 0xb1 0xfd
0x96 0xfc 0xb1 0xc0 0xc4 0xcf 0x89 0xb4 0xe8 0xad 0xa3 0xc9
function max_images(images::Vector{<:Array})
M = copy(images[1])
for i = 1:length(M)
for j = 2:length(images)
M[i] = max(M[i], images[j][i])
end
end
return M
end
@inbounds M[i] = max(M[i], images[j][i])
上添加入站注释来跳过它,风险自负。 .这将时间降低到 282 ns。通过告诉编译器它可以安全地重新排序两个循环以通过放置
@simd
来利用指令级并行性,可以获得更高的速度。每个 for 循环上的宏。这将时间缩短到 240 ns。代码的最终版本是:
function max_images(images::Vector{<:Array})
M = copy(images[1])
@simd for i = 1:length(M)
@simd for j = 2:length(images)
@inbounds M[i] = max(M[i], images[j][i])
end
end
return M
end
关于arrays - Julia 中多个图像或数组的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62022231/
使用 julia 控制台时,您输入如下内容: [10,20]*[1:100,1:100]' 你会得到这样的输出: 2x200 Array{Int64,2}: 10 20 30 40 50
Julia Computing 提供的 Julia 和 Julia Pro 有什么区别? Julia Pro 是否有任何在 Julia 中不可用的企业库? 最佳答案 正如您在 project desc
我最近将我的一个模拟移植到 Julia 中,我仅在运行时发现了几个类型错误。我希望静态分析我的 Julia 代码。 MATLAB 也有类似的问题,只在运行时发现很多错误。 我发现的唯一工具 ( Typ
是否有一种简单的方法来监控 julia 和所有 julia 包的提交和开发?我知道 https://github.com/JuliaLang/julia/commits/master 最佳答案 如果您
我正在从 R 迁移,我使用 head() function很多。我在 Julia 中找不到类似的方法,所以我为 Julia Arrays 写了一个。我还将其他几个 R 函数移植到 Julia。 我需要
在某些语言(如 Python)中,有函数装饰器,它们看起来像宏,位于函数定义之上。装饰器为函数本身提供了一些额外的功能。 Julia 是否以任何方式支持函数装饰器的想法?是否可以使用宏来实现相同的目标
我用Julia中的pmap()函数写了一段并行代码。 然后我在集群上保护了四个核心并运行了一个脚本: julia -p 12 my_parallel_program.jl 我现在应该取消我的工作吗?现
谁能帮我理解接下来的事情: 1)为什么我们需要在制作链表的同时制作一个 future 结构的新抽象类? 2) 为什么有参数 T? 3)这个操作符是干什么的 struct BrokenList
我在 Julia 中有一个数组 Z,它表示二维高斯函数的图像。 IE。 Z[i,j] 是像素 i,j 处的高斯高度。我想确定高斯的参数(均值和协方差),大概是通过某种曲线拟合。 我研究了各种拟合 Z
假设,我们有如下数据结构 struct MyStruct{T} t :: Union{Nothing, T} end 并且我们希望允许用户在不添加任何数据的情况下初始化结构,例如 MyStru
我有一个包含相同类型字段的结构,我无法在创建时分配该字段。 Julia 似乎不喜欢以下内容。 (它吐出一个循环引用投诉。)我打算将问题归结为它的本质 mutable struct Test t
我正在尝试使用最大似然估计 Julia 中的正态线性模型。根据 Optim 文档中关于不更改的值,我使用以下代码通过拦截和匿名函数来模拟该过程: using Optim nobs = 500 nvar
有没有办法从命令行更新 Julia?我浏览了 documentation ,但我找不到任何东西。 最佳答案 我建议尝试 asdf如果您使用的是 MacOS、Linux 或 Linux 的 Window
我想对维度为 n 乘以 n 的矩阵 A 中的所有元素求和。该矩阵是对称的并且对角线上有 0。我发现最快的方法就是求和(A)。然而,这似乎很浪费,因为它没有使用我只需要计算矩阵的下三角这一事实。但是,s
假设你有一个向量元组 $a$,我想在 julia 中定义一个函数 p(x)=x^a。 例如,如果 a=(1,2,3),则结果函数将为 x^1 *y^2 * z^3。 我想为任何元组提供一个通用方法,但
例如,我希望能够按照以下方式做一些事情: abstract Tree abstract SupervisedModel type DecisionTree <: Tree, SupervisedMod
在 Julia 中构建复杂表达式时,是否可以使用列表推导式之类的东西? 例如,假设我有一些符号和类型,并想从它们构建一个类型。现在,我必须做类似的事情。 syms = [:a, :b, :c] typ
在 MATLAB 中,[N,edges,bin] = histcounts (___) 可以获得相应元素的 bin 索引。 Julia 有什么等价的功能吗?谢谢! 我已经尝试过 StatsBase.j
我有一个 Julia 脚本,它反复调用 C++ 程序来执行优化。 C++ 程序写入一个文本文件,然后我让 Julia 读取结果并决定下一步做什么。问题是偶尔(可能是 1000 多次)C++ 程序卡住(
我使用了一些需要特定版本的 Julia 包(即 ≥ v0.3 和 0.4 ≤)。我找不到编译 Julia 的方法来自特定版本的源代码(我正在使用 Linux )。有没有办法做到这一点,我不知道? Gi
我是一名优秀的程序员,十分优秀!