Add *mat64.Dense to cv::Mat converter.

This commit is contained in:
Chih-Wei Chang 2015-02-17 00:14:10 +08:00
parent bdc350f850
commit 27818da249
5 changed files with 58 additions and 4 deletions

View file

@ -29,7 +29,7 @@ func NewGcvSize2f64(x, y float64) GcvSize2f64_ {
return NewGcvSize2f64_(float64(x), float64(y))
}
// Convert Mat, which defined by SWIG, to mat64.Dense.
// Convert Mat, which defined by SWIG, to *mat64.Dense.
// The reason is the latter is much easier to handle
// in Go.
// GcvMat is assumed to be 2-dimensional matrix.
@ -52,3 +52,18 @@ func MatToMat64(mat Mat) *mat64.Dense {
return mat64.NewDense(row, col, data)
}
// Convert *mat64.Dense to Mat
func ToMat(mat *mat64.Dense) Mat {
row, col := mat.Dims()
rawData := NewGcvFloat64Vector(int64(row * col))
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
rawData.Set(i*col+j, mat.At(i, j))
}
}
return ToMat_(row, col, rawData)
}

21
gocv/gocv_core.cpp Normal file
View file

@ -0,0 +1,21 @@
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <vector>
#include "gocv_core.hpp"
cv::Mat ToMat_(int row, int col, std::vector<double> data) {
assert(row * col == data.size());
cv::Mat mat = cv::Mat(row, col, CV_64F);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
mat.at<double>(i, j) = data[i*col + j];
}
}
return mat;
}

5
gocv/gocv_core.hpp Normal file
View file

@ -0,0 +1,5 @@
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
cv::Mat ToMat_(int row, int col, std::vector<double> data);

View file

@ -1,9 +1,11 @@
%include "std_vector.i"
%{
#include "opencv2/core/core.hpp"
#include "gocv_core.hpp"
%}
%include "std_vector.i"
%include "gocv_core.hpp"
/* Classes defined in core.hpp */
namespace cv {
@ -295,5 +297,6 @@ namespace std {
%template(GcvPoint2f32Vector) vector<cv::Point2f>;
%template(GcvIntVector) vector<int>;
%template(GcvFloatVector) vector<float>;
%template(GcvFloat32Vector) vector<float>;
%template(GcvFloat64Vector) vector<double>;
};

View file

@ -4,6 +4,7 @@ import (
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/gonum/matrix/mat64"
)
func TestNewGcvPoint3f32(t *testing.T) {
@ -26,3 +27,12 @@ func TestMat(t *testing.T) {
mat2 := NewMat(mat)
spew.Dump(mat2)
}
func TestToMat(t *testing.T) {
mat := mat64.NewDense(3, 2, []float64{
0, 1,
1.23, 4,
-12.3, -4,
})
spew.Dump(ToMat(mat))
}