Lua 调用 Opencv 的方法

时间:2023-03-09 03:01:29
Lua 调用 Opencv 的方法

Lua 调用 Opencv 的方法

  

  最近想用 Lua 调用 Opencv 进行相关像素级操作,如:bitwise_and 或者 bitwise_or,从而完成图像 IoU 的计算。

  那么,怎么用 Lua 调用 Opencv 呢?

  查了 Torch 的官方文档,发现只有这么几个可以调用的包:

  链接: https://github.com/torch/torch7/wiki/Cheatsheet

    Lua 调用 Opencv 的方法

  然后,你点击一个进去,发现有这么一个方法,可以安装对应的 Opencv 包:

  Lua 调用 Opencv 的方法

    然后,你就在终端里输入: luarocks install cv ,发现半天没反应 。。。

  过了一会,有反应了,见下图:  

Lua 调用 Opencv 的方法

    然后,就是等待了,这个”龟速“ 真的不能忍!!!

  其实,这里并没有那么直接,因为,你发现,如果你没有下载好 Opencv 官方的软件包,安装的时候,会提示你错误,从而停止掉!

  Lua 调用 Opencv 的方法

  所以,还是需要安装这个网页上提示的过程进行:https://github.com/VisionLabs/torch-opencv/wiki/Installation 

  首先,是下载安装 Opencv 官方的 3.1 Linux 版本文件;

  然后,确保你的 Torch 是没有问题的;

  然后就开始执行刚刚那一句:luarocks install cv,这里,如果你可以直接指定 Opencv 文件的路径,就更好了,即:

  例如: OpenCV_DIR="/home/wangxiao/opencv-3.1.0" luarocks install cv 

  然后,你能做的,就还是等待,等待,再等待 。。。


  Sorry,又报错了:

    CMake Error at CMakeLists.txt:30 (FIND_PACKAGE):
    Could not find a configuration file for package "OpenCV" that is compatible
    with requested version "3.1".

    The following configuration files were considered but not accepted:

    /home/wangxiao/opencv-3.1.0/cmake/OpenCVConfig.cmake, version: unknown
    /usr/share/OpenCV/OpenCVConfig.cmake, version: 2.4.9.1

    -- Configuring incomplete, errors occurred!
    See also "/tmp/luarocks_cv-scm-1-1973/torch-opencv/build/CMakeFiles/CMakeOutput.log".
    make: *** No targets specified and no makefile found. Stop.

  具体的是:

  Lua 调用 Opencv 的方法

  

  此时的我,我特想打人。。。真的。。。

    后来找到一个关于求解 IoU 的帖子,来自于 Faster RCNN :  

 function o = boxoverlap(a, b)
% Compute the symmetric intersection over union overlap between a set of
% bounding boxes in a and a single bounding box in b.
%
% a a matrix where each row specifies a bounding box
% b a matrix where each row specifies a bounding box % AUTORIGHTS
% -------------------------------------------------------
% Copyright (C) - Ross Girshick
% Copyright (C) , , Pedro Felzenszwalb, Ross Girshick
%
% This file is part of the voc-releaseX code
% (http://people.cs.uchicago.edu/~rbg/latent/)
% and is available under the terms of an MIT-like license
% provided in COPYING. Please retain this notice and
% COPYING if you use this file (or a portion of it) in
% your project.
% ------------------------------------------------------- o = cell(, size(b, ));
for i = :size(b, )
x1 = max(a(:,), b(i,));
y1 = max(a(:,), b(i,));
x2 = min(a(:,), b(i,));
y2 = min(a(:,), b(i,)); w = x2-x1+;
h = y2-y1+;
inter = w.*h;
aarea = (a(:,)-a(:,)+) .* (a(:,)-a(:,)+);
barea = (b(i,)-b(i,)+) * (b(i,)-b(i,)+);
% intersection over union overlap
o{i} = inter ./ (aarea+barea-inter);
% set invalid entries to overlap
o{i}(w <= ) = ;
o{i}(h <= ) = ;
end o = cell2mat(o);

  晚上回去,我找了找 Faster RCNN Torch版本的代码:

 function Rect.union(a, b)
local minx = math.min(a.minX, b.minX)
local miny = math.min(a.minY, b.minY)
local maxx = math.max(a.maxX, b.maxX)
local maxy = math.max(a.maxY, b.maxY)
return Rect.new(minx, miny, maxx, maxy)
end function Rect.intersect(a, b)
local minx = math.max(a.minX, b.minX)
local miny = math.max(a.minY, b.minY)
local maxx = math.min(a.maxX, b.maxX)
local maxy = math.min(a.maxY, b.maxY)
if maxx >= minx and maxy >= miny then
return Rect.new(minx, miny, maxx, maxy)
else
return Rect.empty()
end
end function Rect.IoU(a, b)
local i = Rect.intersect(a, b):area()
return i / (a:area() + b:area() - i)
end

  是的,这就是关于求解 IoU 的代码了,至于,怎么调用 Opencv,我想说的是,等我安装好工具包先(此刻已泪崩 。。。)