OpenCV BFMatcher 在 Torch 中对卷积图像描述符进行匹配时遇到的错误

我正在使用 FAST 提取图像的描述符,并通过自定义网络将其卷积以提供每个图像的 64 个描述符进行暴力匹配。

进行比较时,我会卷积传入的图像,然后将其与我从文件中加载的一个图像的描述符集进行比较。该图像之前已被卷积并保存。

我得到了一个 OpenCV 错误: OpenCV Error:在 batchDistance 中,文件/home/ec2-user/opencv/modules/core/src/stat.cpp,第 3749 行,断言失败(type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U))

我尝试通过使用 print(#loaded_descriptors)print(#conv) 打印张量的类型,分别返回:

134
64
[torch.LongStorage of size 2]

&

134
64
[torch.LongStorage of size 2]

----------------------------------------------CODE -------------------------------------------

CNN 模型

local model_params = torch.load(path[i])
net = dofile("../training/models/3x32x32_to_64.lua")(model_params):float())
net:evaluate()
net = net:cuda()
net = cudnn.convert(net, cudnn)
print(net)

从文件中加载描述符

function load_descriptors(csv_path)
  -- 循环遍历文件夹并从 csv 文件中加载描述符
  local csv_files = io.popen('ls ' .. csv_path .. '/*.csv')
  for path in csv_files:lines() do
    local keypoints = table.load(path)
    -- 将表格更改为数字特征 x 64 描述符的张量
    local tensor_data = torch.CudaTensor(#keypoints,64)
    for i=1, #keypoints, 1 do
      for j=1, 64, 1 do
        tensor_data[i][j] = keypoints[i][j]
      end
    end
  return tensor_data:cuda()
end

运行匹配

-- 加载图像并卷积
img = image.load("img.jpg", 3, 'float'):mul(255):cuda()
img = img:view(3,img:size(2)/img:size(3), img:size(3),img:size(3)):transpose(1, 2):contiguous()
conv = net:forward(img):clone()

-- 加载描述符
local loaded_descriptors = load_descriptors(descriptor_path)

-- BF 匹配
print(#loaded_descriptors)
print(#conv)

matcher = cv.BFMatcher{cv.NORM_L2, False}
local match_ptr = matcher:match{conv, loaded_descriptors}
local res = {}
for i=1, match_ptr.size, 1 do
  table.insert(res, match_ptr.data[i].distance)
end
点赞
用户5244410
用户5244410

问题在于lua openCV绑定不能使用CudaTensor,必须将CudaTensors转换回FloatTensor。这个转换可以通过将张量重新赋值为float进行完成:

conv = conv:float()

在我的脚本中,我还必须更改数据的加载方式:

local tensor_data = torch.Tensor(#keypoints,64) (默认为FloatTensor)

2016-07-24 00:20:30