Lua 調用 Opencv 的方法


 

Lua 調用 Opencv 的方法

  

  最近想用 Lua 調用 Opencv 進行相關像素級操作,如:bitwise_and 或者 bitwise_or,從而完成圖像 IoU 的計算。

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

  查了 Torch 的官方文檔,發現只有這么幾個可以調用的包:

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

 

    

  然后,你點擊一個進去,發現有這么一個方法,可以安裝對應的 Opencv 包:

  

    然后,你就在終端里輸入: luarocks install cv ,發現半天沒反應 。。。

  過了一會,有反應了,見下圖:  

 

    然后,就是等待了,這個”龜速“ 真的不能忍!!!

  其實,這里並沒有那么直接,因為,你發現,如果你沒有下載好 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.

 

  具體的是:

  

 

  

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

 

     后來找到一個關於求解 IoU 的帖子,來自於 Faster RCNN :  

 1 function o = boxoverlap(a, b)
 2 % Compute the symmetric intersection over union overlap between a set of
 3 % bounding boxes in a and a single bounding box in b.
 4 %
 5 % a  a matrix where each row specifies a bounding box
 6 % b  a matrix where each row specifies a bounding box
 7 
 8 % AUTORIGHTS
 9 % -------------------------------------------------------
10 % Copyright (C) 2011-2012 Ross Girshick
11 % Copyright (C) 2008, 2009, 2010 Pedro Felzenszwalb, Ross Girshick
12 % 
13 % This file is part of the voc-releaseX code
14 % (http://people.cs.uchicago.edu/~rbg/latent/)
15 % and is available under the terms of an MIT-like license
16 % provided in COPYING. Please retain this notice and
17 % COPYING if you use this file (or a portion of it) in
18 % your project.
19 % -------------------------------------------------------
20 
21 o = cell(1, size(b, 1));
22 for i = 1:size(b, 1)
23     x1 = max(a(:,1), b(i,1));
24     y1 = max(a(:,2), b(i,2));
25     x2 = min(a(:,3), b(i,3));
26     y2 = min(a(:,4), b(i,4));
27 
28     w = x2-x1+1;
29     h = y2-y1+1;
30     inter = w.*h;
31     aarea = (a(:,3)-a(:,1)+1) .* (a(:,4)-a(:,2)+1);
32     barea = (b(i,3)-b(i,1)+1) * (b(i,4)-b(i,2)+1);
33     % intersection over union overlap
34     o{i} = inter ./ (aarea+barea-inter);
35     % set invalid entries to 0 overlap
36     o{i}(w <= 0) = 0;
37     o{i}(h <= 0) = 0;
38 end
39 
40 o = cell2mat(o);

 

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

 1 function Rect.union(a, b)
 2   local minx = math.min(a.minX, b.minX)
 3   local miny = math.min(a.minY, b.minY)
 4   local maxx = math.max(a.maxX, b.maxX)
 5   local maxy = math.max(a.maxY, b.maxY)
 6   return Rect.new(minx, miny, maxx, maxy)
 7 end
 8 
 9 function Rect.intersect(a, b)
10   local minx = math.max(a.minX, b.minX)
11   local miny = math.max(a.minY, b.minY)
12   local maxx = math.min(a.maxX, b.maxX)
13   local maxy = math.min(a.maxY, b.maxY)
14   if maxx >= minx and maxy >= miny then
15     return Rect.new(minx, miny, maxx, maxy)
16   else
17     return Rect.empty()
18   end
19 end
20 
21 function Rect.IoU(a, b)
22   local i = Rect.intersect(a, b):area() 
23   return i / (a:area() + b:area() - i)
24 end

 

 

 

   是的,這就是關於求解 IoU 的代碼了,至於,怎么調用 Opencv,我想說的是,等我安裝好工具包先(此刻已淚崩 。。。)

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM