You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将RGB{N0f8}类型转换为Array{Float64}?Julia移植神经网络遇阻

Julia中RGB图像转数组适配神经网络输入的问题

我正在把Andrew Ng课程里的基础神经网络从Python移植到Julia,处理自定义数据集时卡壳了。为了和Python代码逻辑一致(把所有图像作为向量存入单个矩阵的列中),我需要将RGB格式图像转换为Array类型,但一直报错,也没找到相关解决办法。

当前用的是Julia Discourse上某方案的适配版本,代码如下:

using Images, FileIO, TestImages

cat_path = "path/Cat/"
cat_imgs = joinpath.(cat_path, readdir(cat_path))

function process_image(path_vec::Vector{String}, h::Int64, w::Int64, label::Int64)
  result = zeros((h*w), length(path_vec))
  class = []
  for i in enumerate(path_vec)
    img = load(i[2])::Array{RGB{N0f8},2}
    img = imresize(img,(h,w))::Array{RGB{N0f8},2}
    img = vec(img)::Vector{RGB{N0f8}}
    result[:,i[1]] = img # 这里是报错的核心位置
    push!(class, label) 
  end
  return result, class
end

把图像转成灰度图能正常运行(单通道容易转数组),但保留所有通道时,没法直接将Vector{RGB{N0f8}}存入矩阵;尝试用img = convert(Array{Float64,1},img)转换时,会触发错误:MethodError: Cannot convert an object of type RGB{N0f8} to an object of type Float64。

可通过两种方式复现问题:一是创建含单张图像的文件夹并修改代码中的路径;二是运行以下测试图像代码,单独验证函数内的逻辑:

using TestImages
img = testimage("mandrill")

内容的提问来源于stack exchange,提问作者Willian Leite

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 02:05:24