opencv-ffi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +15 -0
- data/README.md +126 -0
- data/Rakefile +52 -0
- data/docs/DocsIndex.md +1 -0
- data/docs/examples/load_image.rb +25 -0
- data/ext/Rakefile +13 -0
- data/ext/aishack-sift/.gitignore +4 -0
- data/ext/aishack-sift/Descriptor.h +34 -0
- data/ext/aishack-sift/KeyPoint.h +38 -0
- data/ext/aishack-sift/README +20 -0
- data/ext/aishack-sift/SIFT.cpp +1036 -0
- data/ext/aishack-sift/SIFT.h +84 -0
- data/ext/aishack-sift/example/.gitignore +2 -0
- data/ext/aishack-sift/example/Makefile +24 -0
- data/ext/aishack-sift/example/MySIFT.cpp +29 -0
- data/ext/aishack-sift/mkrf_conf.rb +13 -0
- data/ext/aishack-sift/siftlib.cpp +85 -0
- data/ext/eigen/.gitignore +4 -0
- data/ext/eigen/eigen_polynomial.cpp +41 -0
- data/ext/eigen/eigen_svd.cpp +100 -0
- data/ext/eigen/mkrf_conf.rb +14 -0
- data/ext/mkrf-monkey.rb +85 -0
- data/ext/mkrf-rakehelper-monkey.rb +52 -0
- data/ext/mkrf_conf.rb +3 -0
- data/ext/opencv-ffi/.gitignore +4 -0
- data/ext/opencv-ffi/matcher_helper.cpp +56 -0
- data/ext/opencv-ffi/mkrf_conf.rb +12 -0
- data/ext/opencv-ffi/vector_math.cpp +39 -0
- data/ext/opensurf/.gitignore +4 -0
- data/ext/opensurf/README +38 -0
- data/ext/opensurf/fasthessian.cpp +376 -0
- data/ext/opensurf/fasthessian.h +108 -0
- data/ext/opensurf/integral.cpp +58 -0
- data/ext/opensurf/integral.h +55 -0
- data/ext/opensurf/ipoint.cpp +108 -0
- data/ext/opensurf/ipoint.h +76 -0
- data/ext/opensurf/kmeans.h +172 -0
- data/ext/opensurf/mkrf_conf.rb +10 -0
- data/ext/opensurf/responselayer.h +92 -0
- data/ext/opensurf/surf.cpp +317 -0
- data/ext/opensurf/surf.h +66 -0
- data/ext/opensurf/surflib.cpp +98 -0
- data/ext/opensurf/surflib.h +96 -0
- data/ext/opensurf/utils.cpp +357 -0
- data/ext/opensurf/utils.h +63 -0
- data/lib/.gitignore +1 -0
- data/lib/opencv-ffi-ext/eigen.rb +84 -0
- data/lib/opencv-ffi-ext/features2d.rb +4 -0
- data/lib/opencv-ffi-ext/matcher_helper.rb +24 -0
- data/lib/opencv-ffi-ext/opensurf.rb +217 -0
- data/lib/opencv-ffi-ext/sift.rb +118 -0
- data/lib/opencv-ffi-ext/vector_math.rb +115 -0
- data/lib/opencv-ffi-wrappers.rb +7 -0
- data/lib/opencv-ffi-wrappers/core.rb +24 -0
- data/lib/opencv-ffi-wrappers/core/iplimage.rb +50 -0
- data/lib/opencv-ffi-wrappers/core/mat.rb +268 -0
- data/lib/opencv-ffi-wrappers/core/misc_draw.rb +44 -0
- data/lib/opencv-ffi-wrappers/core/point.rb +286 -0
- data/lib/opencv-ffi-wrappers/core/rect.rb +40 -0
- data/lib/opencv-ffi-wrappers/core/scalar.rb +104 -0
- data/lib/opencv-ffi-wrappers/core/size.rb +88 -0
- data/lib/opencv-ffi-wrappers/enumerable.rb +10 -0
- data/lib/opencv-ffi-wrappers/features2d.rb +17 -0
- data/lib/opencv-ffi-wrappers/features2d/image_patch.rb +322 -0
- data/lib/opencv-ffi-wrappers/features2d/star.rb +111 -0
- data/lib/opencv-ffi-wrappers/features2d/surf.rb +115 -0
- data/lib/opencv-ffi-wrappers/highgui.rb +10 -0
- data/lib/opencv-ffi-wrappers/imgproc.rb +4 -0
- data/lib/opencv-ffi-wrappers/imgproc/features.rb +35 -0
- data/lib/opencv-ffi-wrappers/imgproc/geometric.rb +39 -0
- data/lib/opencv-ffi-wrappers/matcher.rb +297 -0
- data/lib/opencv-ffi-wrappers/matrix.rb +37 -0
- data/lib/opencv-ffi-wrappers/misc.rb +41 -0
- data/lib/opencv-ffi-wrappers/misc/params.rb +34 -0
- data/lib/opencv-ffi-wrappers/sequence.rb +37 -0
- data/lib/opencv-ffi-wrappers/vectors.rb +38 -0
- data/lib/opencv-ffi.rb +12 -0
- data/lib/opencv-ffi/calib3d.rb +26 -0
- data/lib/opencv-ffi/core.rb +15 -0
- data/lib/opencv-ffi/core/draw.rb +68 -0
- data/lib/opencv-ffi/core/dynamic.rb +13 -0
- data/lib/opencv-ffi/core/library.rb +5 -0
- data/lib/opencv-ffi/core/operations.rb +122 -0
- data/lib/opencv-ffi/core/point.rb +22 -0
- data/lib/opencv-ffi/core/types.rb +172 -0
- data/lib/opencv-ffi/cvffi.rb +8 -0
- data/lib/opencv-ffi/features2d.rb +7 -0
- data/lib/opencv-ffi/features2d/library.rb +6 -0
- data/lib/opencv-ffi/features2d/star.rb +30 -0
- data/lib/opencv-ffi/features2d/surf.rb +38 -0
- data/lib/opencv-ffi/highgui.rb +31 -0
- data/lib/opencv-ffi/imgproc.rb +9 -0
- data/lib/opencv-ffi/imgproc/features.rb +37 -0
- data/lib/opencv-ffi/imgproc/geometric.rb +42 -0
- data/lib/opencv-ffi/imgproc/library.rb +6 -0
- data/lib/opencv-ffi/imgproc/misc.rb +39 -0
- data/lib/opencv-ffi/version.rb +3 -0
- data/opencv-ffi.gemspec +26 -0
- data/test/core/test_draw.rb +46 -0
- data/test/core/test_operations.rb +135 -0
- data/test/core/test_size.rb +14 -0
- data/test/core/test_text.rb +52 -0
- data/test/ext/test_eigen.rb +105 -0
- data/test/ext/test_opensurf.rb +35 -0
- data/test/ext/test_sift.rb +26 -0
- data/test/ext/test_vector_math.rb +85 -0
- data/test/features2d/test_surf.rb +63 -0
- data/test/imgproc/test_goodfeatures.rb +18 -0
- data/test/setup.rb +65 -0
- data/test/test_calib3d.rb +38 -0
- data/test/test_core.rb +26 -0
- data/test/test_ext.rb +8 -0
- data/test/test_features2d.rb +9 -0
- data/test/test_files/images/IMG_7088.JPG +0 -0
- data/test/test_files/images/IMG_7088_small.JPG +0 -0
- data/test/test_files/images/IMG_7089.JPG +0 -0
- data/test/test_highgui.rb +26 -0
- data/test/test_imgproc.rb +35 -0
- data/test/test_wrappers.rb +8 -0
- data/test/wrappers/core/test_draw.rb +41 -0
- data/test/wrappers/core/test_mat.rb +40 -0
- data/test/wrappers/core/test_operations.rb +35 -0
- data/test/wrappers/core/test_types.rb +235 -0
- data/test/wrappers/features2d/test_image_patch.rb +108 -0
- data/test/wrappers/test_imgproc.rb +87 -0
- data/test/wrappers/test_matcher.rb +96 -0
- data/test/wrappers/test_star.rb +28 -0
- data/test/wrappers/test_surf.rb +36 -0
- metadata +234 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
/******************************************************
|
2
|
+
* Code by Utkarsh Sinha
|
3
|
+
* Based on JIFT by Jun Liu
|
4
|
+
* Visit http://aishack.in/ for more indepth articles and tutorials
|
5
|
+
* on artificial intelligence
|
6
|
+
* Use, reuse, modify, hack, kick. Do whatever you want with
|
7
|
+
* this code :)
|
8
|
+
******************************************************/
|
9
|
+
|
10
|
+
#include <opencv2/core/core.hpp>
|
11
|
+
#include <opencv2/highgui/highgui.hpp>
|
12
|
+
|
13
|
+
#include "KeyPoint.h"
|
14
|
+
#include "Descriptor.h"
|
15
|
+
|
16
|
+
#define SIGMA_ANTIALIAS 0.5
|
17
|
+
#define SIGMA_PREBLUR 1.0
|
18
|
+
#define CURVATURE_THRESHOLD 5.0
|
19
|
+
#define CONTRAST_THRESHOLD 0.03 // in terms of 255
|
20
|
+
#define NUM_BINS 36
|
21
|
+
#define MAX_KERNEL_SIZE 20
|
22
|
+
#define FEATURE_WINDOW_SIZE 16
|
23
|
+
#define DESC_NUM_BINS 8
|
24
|
+
#define FVSIZE 128
|
25
|
+
#define FV_THRESHOLD 0.2
|
26
|
+
|
27
|
+
|
28
|
+
class SIFT
|
29
|
+
{
|
30
|
+
public:
|
31
|
+
SIFT(IplImage* img, int octaves, int intervals);
|
32
|
+
SIFT(const char* filename, int octaves, int intervals);
|
33
|
+
~SIFT();
|
34
|
+
|
35
|
+
void DoSift();
|
36
|
+
void DetectKeypoints();
|
37
|
+
void DescribeKeypoints();
|
38
|
+
|
39
|
+
void ShowKeypoints();
|
40
|
+
void ShowAbsSigma();
|
41
|
+
|
42
|
+
const vector<Keypoint> keypoints( void ) { return m_keyPoints; }
|
43
|
+
const vector<Descriptor> descriptors( void ) { return m_keyDescs; }
|
44
|
+
|
45
|
+
private:
|
46
|
+
void GenerateLists();
|
47
|
+
void BuildScaleSpace();
|
48
|
+
void DetectExtrema();
|
49
|
+
void AssignOrientations();
|
50
|
+
void ExtractKeypointDescriptors();
|
51
|
+
|
52
|
+
unsigned int GetKernelSize(double sigma, double cut_off=0.001);
|
53
|
+
CvMat* BuildInterpolatedGaussianTable(unsigned int size, double sigma);
|
54
|
+
double gaussian2D(double x, double y, double sigma);
|
55
|
+
|
56
|
+
|
57
|
+
private:
|
58
|
+
IplImage* m_srcImage; // The image we're working on
|
59
|
+
unsigned int m_numOctaves; // The desired number of octaves
|
60
|
+
unsigned int m_numIntervals; // The desired number of intervals
|
61
|
+
unsigned int m_numKeypoints; // The number of keypoints detected
|
62
|
+
|
63
|
+
IplImage*** m_gList; // A 2D array to hold the different gaussian blurred images
|
64
|
+
IplImage*** m_dogList; // A 2D array to hold the different DoG images
|
65
|
+
IplImage*** m_extrema; // A 2D array to hold binary images. In the binary image, 1 = extrema, 0 = not extrema
|
66
|
+
double** m_absSigma; // A 2D array to hold the sigma used to blur a particular image
|
67
|
+
|
68
|
+
class Extrema {
|
69
|
+
public:
|
70
|
+
int octave, interval, xi, yi;
|
71
|
+
|
72
|
+
Extrema( int oct, int intv, int x, int y )
|
73
|
+
: octave( oct ), interval( intv ), xi( x ), yi( y )
|
74
|
+
{}
|
75
|
+
};
|
76
|
+
|
77
|
+
vector<Extrema> extrema;
|
78
|
+
|
79
|
+
vector<Keypoint> m_keyPoints; // Holds each keypoint's basic info
|
80
|
+
vector<Descriptor> m_keyDescs; // Holds each keypoint's descriptor
|
81
|
+
|
82
|
+
IplImage *magnitude_mat( int i, int j );
|
83
|
+
double orientation_at( int i, int j, int xi, int yi );
|
84
|
+
};
|
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
CXX = g++
|
3
|
+
BIN = MySIFT
|
4
|
+
OBJS = MySIFT.o ../SIFT.o
|
5
|
+
|
6
|
+
CFLAGS = -ggdb -I.. -I/localhome/amm299/usr/include
|
7
|
+
LFLAGS = -L/localhome/amm299/usr/lib
|
8
|
+
LIBS = -lopencv_core -lopencv_highgui -lopencv_imgproc
|
9
|
+
|
10
|
+
|
11
|
+
default: run
|
12
|
+
|
13
|
+
run: $(BIN)
|
14
|
+
LD_LIBRARY_PATH=~/usr/lib ./MySIFT
|
15
|
+
|
16
|
+
|
17
|
+
$(BIN): $(OBJS)
|
18
|
+
$(CXX) $(CFLAGS) -o $@ $^ $(LFLAGS) $(LIBS)
|
19
|
+
|
20
|
+
.cpp.o:
|
21
|
+
$(CXX) -c $(CFLAGS) -o $@ $^
|
22
|
+
|
23
|
+
clean:
|
24
|
+
rm -f $(BIN) *.o
|
@@ -0,0 +1,29 @@
|
|
1
|
+
/******************************************************
|
2
|
+
* Code by Utkarsh Sinha
|
3
|
+
* Based on JIFT by Jun Liu
|
4
|
+
* Visit http://aishack.in/ for more indepth articles and tutorials
|
5
|
+
* on artificial intelligence
|
6
|
+
* Use, reuse, modify, hack, kick. Do whatever you want with
|
7
|
+
* this code :)
|
8
|
+
******************************************************/
|
9
|
+
|
10
|
+
// MySIFT.cpp : Defines the entry point for the console application.
|
11
|
+
//
|
12
|
+
|
13
|
+
#include "SIFT.h"
|
14
|
+
|
15
|
+
#include <opencv2/core/core.hpp>
|
16
|
+
|
17
|
+
// The main function!
|
18
|
+
int main()
|
19
|
+
{
|
20
|
+
// Create an instance of SIFT
|
21
|
+
SIFT *sift = new SIFT("IMG_7088_small.JPG", 1, 5);
|
22
|
+
|
23
|
+
sift->DoSift(); // Find keypoints
|
24
|
+
|
25
|
+
// Cleanup and exit
|
26
|
+
delete sift;
|
27
|
+
return 0;
|
28
|
+
}
|
29
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require '../mkrf-monkey'
|
2
|
+
|
3
|
+
Mkrf::Generator.new('libcvffi_sift', [ "*.cpp" ], { :compiler=>"g++"}) { |g|
|
4
|
+
g.include_library 'stdc++'
|
5
|
+
|
6
|
+
raise "Can't find 'opencv_core'" unless g.include_library 'opencv_core', 'main', "#{ENV['HOME']}/usr/lib"
|
7
|
+
raise "Can't find 'opencv_imgproc'" unless g.include_library 'opencv_imgproc', 'main', "#{ENV['HOME']}/usr/lib"
|
8
|
+
raise "Can't find 'opencv_highgui'" unless g.include_library 'opencv_highgui', 'main', "#{ENV['HOME']}/usr/lib"
|
9
|
+
|
10
|
+
g.cflags += "-I#{ENV['HOME']}/usr/include "
|
11
|
+
|
12
|
+
}
|
13
|
+
|
@@ -0,0 +1,85 @@
|
|
1
|
+
|
2
|
+
#include "SIFT.h"
|
3
|
+
|
4
|
+
#include <stdio.h>
|
5
|
+
|
6
|
+
typedef struct {
|
7
|
+
int octaves;
|
8
|
+
int intervals;
|
9
|
+
} SiftParams_t;
|
10
|
+
|
11
|
+
typedef struct {
|
12
|
+
float xi;
|
13
|
+
float yi;
|
14
|
+
float mag[NUM_BINS];
|
15
|
+
float orien[NUM_BINS];
|
16
|
+
unsigned int num_bins;
|
17
|
+
|
18
|
+
unsigned int scale;
|
19
|
+
} SiftKeypoint_t;
|
20
|
+
|
21
|
+
typedef struct {
|
22
|
+
float xi, yi;
|
23
|
+
float fv[FVSIZE];
|
24
|
+
unsigned int fv_size;
|
25
|
+
} SiftDescriptor_t;
|
26
|
+
|
27
|
+
typedef struct {
|
28
|
+
SiftKeypoint_t *kps;
|
29
|
+
SiftDescriptor_t *descs;
|
30
|
+
unsigned int len;
|
31
|
+
} SiftResults_t;
|
32
|
+
|
33
|
+
|
34
|
+
SiftResults_t *buildSiftResults( const vector<Keypoint> kps, const vector<Descriptor> descs )
|
35
|
+
{
|
36
|
+
printf("Creating array for %d keypoints.\n", kps.size() );
|
37
|
+
SiftKeypoint_t *kp_array = new SiftKeypoint_t[ kps.size() ];
|
38
|
+
|
39
|
+
unsigned int idx = 0;
|
40
|
+
for( vector<Keypoint>::const_iterator itr = kps.begin(); itr != kps.end(); itr++ ) {
|
41
|
+
kp_array[idx].xi = (*itr).xi;
|
42
|
+
kp_array[idx].yi = (*itr).yi;
|
43
|
+
kp_array[idx].scale = (*itr).scale;
|
44
|
+
|
45
|
+
assert( (*itr).mag.size() == (*itr).orien.size() );
|
46
|
+
|
47
|
+
kp_array[idx].num_bins = (*itr).mag.size();
|
48
|
+
for( unsigned int j = 0; j < (*itr).mag.size(); j++ ) {
|
49
|
+
kp_array[idx].mag[j] = (*itr).mag[j];
|
50
|
+
kp_array[idx].orien[j] = (*itr).orien[j];
|
51
|
+
}
|
52
|
+
|
53
|
+
idx++;
|
54
|
+
}
|
55
|
+
|
56
|
+
printf("Creating array for %d descriptors.\n", descs.size() );
|
57
|
+
SiftDescriptor_t *desc_array = new SiftDescriptor_t[ descs.size() ];
|
58
|
+
idx = 0;
|
59
|
+
for( vector<Descriptor>::const_iterator itr = descs.begin(); itr != descs.end(); itr++ ) {
|
60
|
+
desc_array[idx].xi = (*itr).xi;
|
61
|
+
desc_array[idx].yi = (*itr).yi;
|
62
|
+
|
63
|
+
desc_array[idx].fv_size = FVSIZE;
|
64
|
+
for( unsigned int i = 0; i < FVSIZE; i++ ) {
|
65
|
+
desc_array[idx].fv[i] = (*itr).fv[i];
|
66
|
+
}
|
67
|
+
++idx;
|
68
|
+
}
|
69
|
+
|
70
|
+
SiftResults_t *results= new SiftResults_t;
|
71
|
+
results->kps = kp_array;
|
72
|
+
results->descs = desc_array;
|
73
|
+
results->len = kps.size();
|
74
|
+
|
75
|
+
return results;
|
76
|
+
}
|
77
|
+
|
78
|
+
extern "C"
|
79
|
+
SiftResults_t *siftDetectDescribe( IplImage *img, SiftParams_t params )
|
80
|
+
{
|
81
|
+
SIFT sift( img, params.octaves, params.intervals );
|
82
|
+
sift.DoSift();
|
83
|
+
|
84
|
+
return buildSiftResults( sift.keypoints(), sift.descriptors() );
|
85
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
#include <eigen3/Eigen/Core>
|
3
|
+
#include <eigen3/unsupported/Eigen/Polynomials>
|
4
|
+
#include <vector>
|
5
|
+
#include <iostream>
|
6
|
+
|
7
|
+
using Eigen::MatrixXf;
|
8
|
+
using namespace Eigen;
|
9
|
+
using namespace std;
|
10
|
+
|
11
|
+
|
12
|
+
typedef struct {
|
13
|
+
double a[7];
|
14
|
+
} Eigen7d_t;
|
15
|
+
|
16
|
+
typedef struct {
|
17
|
+
double a[6];
|
18
|
+
} Eigen6d_t;
|
19
|
+
|
20
|
+
extern "C"
|
21
|
+
Eigen6d_t eigenPoly6Solver( Eigen7d_t coeff )
|
22
|
+
{
|
23
|
+
Eigen::Matrix<double,7,1> polynomial;
|
24
|
+
for( unsigned int i = 0; i < 7; i++ ) polynomial[i] = coeff.a[i];
|
25
|
+
|
26
|
+
PolynomialSolver<double,6> psolve( polynomial );
|
27
|
+
|
28
|
+
std::vector<double> realRoots;
|
29
|
+
psolve.realRoots( realRoots );
|
30
|
+
|
31
|
+
Eigen6d_t result;
|
32
|
+
for( unsigned int i = 0; i < 6; i++ ) {
|
33
|
+
if( i < realRoots.size() )
|
34
|
+
result.a[i] = realRoots[i];
|
35
|
+
else
|
36
|
+
result.a[i] = 0.0;
|
37
|
+
}
|
38
|
+
|
39
|
+
return result;
|
40
|
+
}
|
41
|
+
|
@@ -0,0 +1,100 @@
|
|
1
|
+
|
2
|
+
#include <opencv2/core/core.hpp>
|
3
|
+
#include <opencv2/core/core_c.h>
|
4
|
+
#include <eigen3/Eigen/Core>
|
5
|
+
#include <eigen3/Eigen/SVD>
|
6
|
+
#include <eigen3/Eigen/Eigenvalues>
|
7
|
+
|
8
|
+
using Eigen::MatrixXf;
|
9
|
+
using namespace Eigen;
|
10
|
+
using namespace cv;
|
11
|
+
|
12
|
+
|
13
|
+
typedef struct {
|
14
|
+
CvMat *D;
|
15
|
+
CvMat *U, *V;
|
16
|
+
} EigenSVDResult_t;
|
17
|
+
|
18
|
+
MatrixXf cvMatToEigen( CvMat *A )
|
19
|
+
{
|
20
|
+
MatrixXf a( A->rows, A->cols );
|
21
|
+
for( int i = 0; i < A->rows; i++ ) {
|
22
|
+
for( int j = 0; j < A->cols; j++ ) {
|
23
|
+
a(i,j) = cvGetReal2D( A, i, j );
|
24
|
+
}
|
25
|
+
}
|
26
|
+
return a;
|
27
|
+
}
|
28
|
+
|
29
|
+
CvMat *eigenToCvMat( MatrixXf a )
|
30
|
+
{
|
31
|
+
CvMat *mat = cvCreateMat( a.rows(), a.cols(), CV_32F );
|
32
|
+
|
33
|
+
for( int i = 0; i < a.rows(); i++ ) {
|
34
|
+
for( int j = 0; j < a.cols(); j++ ) {
|
35
|
+
cvSetReal2D( mat, i, j, a(i,j) );
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
return mat;
|
40
|
+
}
|
41
|
+
|
42
|
+
extern "C"
|
43
|
+
void eigenSvdWithCvMat( CvMat* A, EigenSVDResult_t *result, unsigned char thin )
|
44
|
+
{
|
45
|
+
// Brute force pack/unpack I'm afraid
|
46
|
+
// assert( ( A->type == CV_32F ) || (A->type == CV_64F) );
|
47
|
+
|
48
|
+
MatrixXf m = cvMatToEigen( A );
|
49
|
+
unsigned int options = (thin == 0) ? (ComputeFullU | ComputeFullV) : (ComputeThinU | ComputeThinV);
|
50
|
+
JacobiSVD<MatrixXf> svd( m, options );
|
51
|
+
|
52
|
+
result->D = eigenToCvMat( svd.singularValues() );
|
53
|
+
result->U = eigenToCvMat( svd.matrixU() );
|
54
|
+
result->V = eigenToCvMat( svd.matrixV() );
|
55
|
+
}
|
56
|
+
|
57
|
+
typedef struct {
|
58
|
+
CvMat *D;
|
59
|
+
CvMat *V;
|
60
|
+
} EigenDecompResult_t;
|
61
|
+
|
62
|
+
CvMat *eigenvaluesToCvMat( EigenSolver<MatrixXf>::EigenvalueType a )
|
63
|
+
{
|
64
|
+
CvMat *mat = cvCreateMat( a.rows(), a.cols(), CV_32F );
|
65
|
+
|
66
|
+
for( int i = 0; i < a.rows(); i++ ) {
|
67
|
+
for( int j = 0; j < a.cols(); j++ ) {
|
68
|
+
cvSetReal2D( mat, i, j, a(i,j).real() );
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
return mat;
|
73
|
+
}
|
74
|
+
|
75
|
+
CvMat *eigenvectorsToCvMat( EigenSolver<MatrixXf>::EigenvectorsType a )
|
76
|
+
{
|
77
|
+
CvMat *mat = cvCreateMat( a.rows(), a.cols(), CV_32F );
|
78
|
+
|
79
|
+
for( int i = 0; i < a.rows(); i++ ) {
|
80
|
+
for( int j = 0; j < a.cols(); j++ ) {
|
81
|
+
cvSetReal2D( mat, i, j, a(i,j).real() );
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
return mat;
|
86
|
+
}
|
87
|
+
|
88
|
+
extern "C"
|
89
|
+
void eigenDecompWithCvMat( CvMat *A, EigenDecompResult_t *result )
|
90
|
+
{
|
91
|
+
// Brute force pack/unpack I'm afraid
|
92
|
+
// assert( ( A->type == CV_32F ) || (A->type == CV_64F) );
|
93
|
+
|
94
|
+
MatrixXf m = cvMatToEigen( A );
|
95
|
+
EigenSolver<MatrixXf> eig( m );
|
96
|
+
|
97
|
+
result->D = eigenvaluesToCvMat( eig.eigenvalues() );
|
98
|
+
result->V = eigenvectorsToCvMat( eig.eigenvectors() );
|
99
|
+
}
|
100
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require '../mkrf-monkey'
|
2
|
+
|
3
|
+
# The compiler for availability checking must be specified as 'g++'
|
4
|
+
# otherwise it will use gcc and choke on Eigen
|
5
|
+
#
|
6
|
+
Mkrf::Generator.new('libcvffi_eigen', [ "*.cpp" ], { :compiler=>"g++"}) { |g|
|
7
|
+
g.include_library 'stdc++'
|
8
|
+
raise "Can't find 'opencv_core'" unless g.include_library 'opencv_core', 'main', "#{ENV['HOME']}/usr/lib"
|
9
|
+
raise "Can't find #include<eigne3/Eigen/Core>" unless g.include_header 'eigen3/Eigen/Core', "#{ENV['HOME']}/usr/include"
|
10
|
+
|
11
|
+
# This is awkward. Eigen's unsupported has internal includes <Eigen/Core>
|
12
|
+
g.cflags += "-I#{ENV['HOME']}/usr/include -I/usr/local/include/eigen3 -I#{ENV['HOME']}/usr/include/eigen3"
|
13
|
+
}
|
14
|
+
|
data/ext/mkrf-monkey.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
|
2
|
+
require 'mkrf'
|
3
|
+
|
4
|
+
# I admit it, I'm monkey-patching mkrf to get the behavior I want.
|
5
|
+
module Mkrf
|
6
|
+
|
7
|
+
class Generator
|
8
|
+
|
9
|
+
def rakefile_contents # :nodoc:
|
10
|
+
objext = CONFIG['OBJEXT']
|
11
|
+
cc = CONFIG['CC'] || 'gcc'
|
12
|
+
cpp = CONFIG['CXX'] || 'g++'
|
13
|
+
extension_sym = File.basename( @extension_name, ".#{CONFIG['DLEXT']}" ).to_sym
|
14
|
+
|
15
|
+
|
16
|
+
<<-END_RAKEFILE
|
17
|
+
# Generated by mkrf, monkey patched for opencv-ffi
|
18
|
+
require 'rake/clean'
|
19
|
+
|
20
|
+
|
21
|
+
SRC = FileList[#{sources.join(',')}]
|
22
|
+
OBJ = SRC.ext('#{objext}')
|
23
|
+
CC = '#{cc}'
|
24
|
+
CPP = '#{cpp}'
|
25
|
+
|
26
|
+
CLEAN.include(OBJ)
|
27
|
+
CLOBBER.include('#{@extension_name}', 'mkrf.log', 'Rakefile')
|
28
|
+
|
29
|
+
ADDITIONAL_OBJECTS = '#{objects}'
|
30
|
+
|
31
|
+
LDSHARED = "#{@available.ldshared_string} #{ldshared}"
|
32
|
+
|
33
|
+
LIBPATH = "#{library_path(CONFIG['libdir'])} #{@available.library_paths_compile_string}"
|
34
|
+
|
35
|
+
INCLUDES = "#{@available.includes_compile_string}"
|
36
|
+
|
37
|
+
LIBS = "#{@available.library_compile_string}"
|
38
|
+
|
39
|
+
CFLAGS = "#{cflags} #{defines_compile_string}"
|
40
|
+
|
41
|
+
RUBYARCHDIR = "\#{ENV["RUBYARCHDIR"]}"
|
42
|
+
LIBRUBYARG_SHARED = "#{CONFIG['LIBRUBYARG_SHARED']}"
|
43
|
+
|
44
|
+
task :default => :build_library
|
45
|
+
|
46
|
+
# Add one layer of indirection so I can generically call "rake build_library"
|
47
|
+
# and have it work ... or not work if the wrong rakefile is being run
|
48
|
+
task :build_library => '#{@extension_name}'
|
49
|
+
|
50
|
+
rule '.#{objext}' => '.c' do |t|
|
51
|
+
sh "\#{CC} \#{CFLAGS} \#{INCLUDES} -o \#{t.name} -c \#{t.source}"
|
52
|
+
end
|
53
|
+
|
54
|
+
rule '.#{objext}' => '.cpp' do |t|
|
55
|
+
sh "\#{CPP} \#{CFLAGS} \#{INCLUDES} -o \#{t.name} -c \#{t.source}"
|
56
|
+
end
|
57
|
+
|
58
|
+
DEPS = OBJ.clone.add('Rakefile')
|
59
|
+
desc "Build this extension"
|
60
|
+
file '#{@extension_name}' => DEPS do
|
61
|
+
sh "\#{LDSHARED} \#{LIBPATH} #{@available.ld_outfile(@extension_name)} \#{OBJ} \#{ADDITIONAL_OBJECTS} \#{LIBS} \#{LIBRUBYARG_SHARED}"
|
62
|
+
end
|
63
|
+
|
64
|
+
desc "Rebuild rakefile"
|
65
|
+
file 'Rakefile' => 'mkrf_conf.rb' do |t|
|
66
|
+
ruby 'mkrf_conf.rb'
|
67
|
+
puts "Rebuilt Rakefile. Run \'rake #{extension_sym.to_s}\' again"
|
68
|
+
end
|
69
|
+
|
70
|
+
desc "Install this extension"
|
71
|
+
task :install => '#{@extension_name}' do
|
72
|
+
makedirs "\#{RUBYARCHDIR}"
|
73
|
+
install "#{@extension_name}", "\#{RUBYARCHDIR}"
|
74
|
+
end
|
75
|
+
|
76
|
+
#{additional_code}
|
77
|
+
END_RAKEFILE
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
|