forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add functions (singular value decomposition, multiply between matrice…
…s, transpose matrix) (hybridgroup#559) * core: add the following: cv::SVD::compute() Mat.MultiplyMatrix(): Multiply between matrices Mat.T(): transpose matrix
- Loading branch information
1 parent
93d8d46
commit 453df7c
Showing
8 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "svd.h" | ||
|
||
void SVD_Compute(Mat src, Mat w, Mat u, Mat vt) { | ||
cv::SVD::compute(*src, *w, *u, *vt, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package gocv | ||
|
||
/* | ||
#include <stdlib.h> | ||
#include "svd.h" | ||
*/ | ||
import "C" | ||
|
||
// SVDCompute decomposes matrix and stores the results to user-provided matrices | ||
// | ||
// https://docs.opencv.org/4.1.2/df/df7/classcv_1_1SVD.html#a76f0b2044df458160292045a3d3714c6 | ||
func SVDCompute(src Mat, w, u, vt *Mat) { | ||
C.SVD_Compute(src.Ptr(), w.Ptr(), u.Ptr(), vt.Ptr()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef _OPENCV3_SVD_H_ | ||
#define _OPENCV3_SVD_H_ | ||
|
||
#ifdef __cplusplus | ||
#include <opencv2/opencv.hpp> | ||
|
||
extern "C" { | ||
#endif | ||
|
||
#include "core.h" | ||
|
||
void SVD_Compute(Mat src, Mat w, Mat u, Mat vt); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif //_OPENCV3_SVD_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package gocv | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSVDCompute(t *testing.T) { | ||
var resultW = []float32{6.167493, 3.8214223} | ||
var resultU = []float32{-0.1346676, -0.99089086, 0.9908908, -0.1346676} | ||
var resultVt = []float32{0.01964448, 0.999807, -0.999807, 0.01964448} | ||
|
||
checkFunc := func(a []float32, b []float32) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
|
||
for i := range a { | ||
if a[i] != b[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
src := NewMatWithSize(2, 2, MatTypeCV32F) | ||
src.SetFloatAt(0, 0, 3.76956568) | ||
src.SetFloatAt(0, 1, -0.90478725) | ||
src.SetFloatAt(1, 0, 0.634576) | ||
src.SetFloatAt(1, 1, 6.10002347) | ||
defer src.Close() | ||
|
||
w := NewMat() | ||
defer w.Close() | ||
|
||
u := NewMat() | ||
defer u.Close() | ||
|
||
vt := NewMat() | ||
defer vt.Close() | ||
|
||
SVDCompute(src, &w, &u, &vt) | ||
|
||
dataW, err := w.DataPtrFloat32() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if !checkFunc(resultW, dataW) { | ||
t.Error("w value is incorrect") | ||
} | ||
|
||
dataU, err := u.DataPtrFloat32() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if !checkFunc(resultU, dataU) { | ||
t.Error("u value is incorrect") | ||
} | ||
|
||
dataVt, err := vt.DataPtrFloat32() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if !checkFunc(resultVt, dataVt) { | ||
t.Error("vt value is incorrect") | ||
} | ||
} |