Skip to content

Commit

Permalink
core: add PCABackProject() and PCAProject() functions
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 20, 2023
1 parent 8a535e4 commit 56dff7a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Your pull requests will be greatly appreciated!
- [ ] **Operations on arrays - WORK STARTED**. The following functions still need implementation:
- [X] [Mahalanobis](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga4493aee129179459cbfc6064f051aa7d)
- [ ] [mulTransposed](https://docs.opencv.org/master/d2/de8/group__core__array.html#gadc4e49f8f7a155044e3be1b9e3b270ab)
- [ ] [PCABackProject](https://docs.opencv.org/master/d2/de8/group__core__array.html#gab26049f30ee8e94f7d69d82c124faafc)
- [X] [PCABackProject](https://docs.opencv.org/master/d2/de8/group__core__array.html#gab26049f30ee8e94f7d69d82c124faafc)
- [X] [PCACompute](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga4e2073c7311f292a0648f04c37b73781)
- [ ] [PCAProject](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga6b9fbc7b3a99ebfd441bbec0a6bc4f88)
- [X] [PCAProject](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga6b9fbc7b3a99ebfd441bbec0a6bc4f88)
- [ ] [PSNR](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga07aaf34ae31d226b1b847d8bcff3698f)
- [X] [randn](https://docs.opencv.org/master/d2/de8/group__core__array.html#gaeff1f61e972d133a04ce3a5f81cf6808)
- [X] [randShuffle](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga6a789c8a5cb56c6dd62506179808f763)
Expand Down
8 changes: 8 additions & 0 deletions core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,18 @@ void Mat_EigenNonSymmetric(Mat src, Mat eigenvalues, Mat eigenvectors) {
cv::eigenNonSymmetric(*src, *eigenvalues, *eigenvectors);
}

void Mat_PCABackProject(Mat data, Mat mean, Mat eigenvectors, Mat result) {
cv::PCABackProject(*data, *mean, *eigenvectors, *result);
}

void Mat_PCACompute(Mat src, Mat mean, Mat eigenvectors, Mat eigenvalues, int maxComponents) {
cv::PCACompute(*src, *mean, *eigenvectors, *eigenvalues, maxComponents);
}

void Mat_PCAProject(Mat data, Mat mean, Mat eigenvectors, Mat result) {
cv::PCAProject(*data, *mean, *eigenvectors, *result);
}

void Mat_Exp(Mat src, Mat dst) {
cv::exp(*src, *dst);
}
Expand Down
16 changes: 16 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,14 @@ func EigenNonSymmetric(src Mat, eigenvalues *Mat, eigenvectors *Mat) {
C.Mat_EigenNonSymmetric(src.p, eigenvalues.p, eigenvectors.p)
}

// PCABackProject reconstructs vectors from their PC projections.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d2/de8/group__core__array.html#gab26049f30ee8e94f7d69d82c124faafc
func PCABackProject(data Mat, mean Mat, eigenvectors Mat, result *Mat) {
C.Mat_PCABackProject(data.p, mean.p, eigenvectors.p, result.p)
}

// PCACompute performs PCA.
//
// The computed eigenvalues are sorted from the largest to the smallest and the corresponding
Expand All @@ -1209,6 +1217,14 @@ func PCACompute(src Mat, mean *Mat, eigenvectors *Mat, eigenvalues *Mat, maxComp
C.Mat_PCACompute(src.p, mean.p, eigenvectors.p, eigenvalues.p, C.int(maxComponents))
}

// PCAProject projects vector(s) to the principal component subspace.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d2/de8/group__core__array.html#ga6b9fbc7b3a99ebfd441bbec0a6bc4f88
func PCAProject(data Mat, mean Mat, eigenvectors Mat, result *Mat) {
C.Mat_PCAProject(data.p, mean.p, eigenvectors.p, result.p)
}

// Exp calculates the exponent of every array element.
//
// For further details, please see:
Expand Down
2 changes: 2 additions & 0 deletions core.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ void Mat_DFT(Mat m, Mat dst, int flags);
void Mat_Divide(Mat src1, Mat src2, Mat dst);
bool Mat_Eigen(Mat src, Mat eigenvalues, Mat eigenvectors);
void Mat_EigenNonSymmetric(Mat src, Mat eigenvalues, Mat eigenvectors);
void Mat_PCABackProject(Mat data, Mat mean, Mat eigenvectors, Mat result);
void Mat_PCACompute(Mat src, Mat mean, Mat eigenvectors, Mat eigenvalues, int maxComponents);
void Mat_PCAProject(Mat data, Mat mean, Mat eigenvectors, Mat result);
void Mat_Exp(Mat src, Mat dst);
void Mat_ExtractChannel(Mat src, Mat dst, int coi);
void Mat_FindNonZero(Mat src, Mat idx);
Expand Down
69 changes: 69 additions & 0 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,36 @@ func TestMatEigenNonSymmetric(t *testing.T) {
eigenvalues.Close()
}

func TestPCABackProject(t *testing.T) {
data := NewMatWithSize(3, 1, MatTypeCV32F)
defer data.Close()
data.SetFloatAt(0, 0, float32(-5))
data.SetFloatAt(1, 0, float32(0))
data.SetFloatAt(2, 0, float32(-10))

mean := NewMatWithSize(1, 4, MatTypeCV32F)
defer mean.Close()
mean.SetFloatAt(0, 0, float32(2))
mean.SetFloatAt(0, 1, float32(4))
mean.SetFloatAt(0, 2, float32(4))
mean.SetFloatAt(0, 3, float32(8))

vectors := NewMatWithSizeFromScalar(NewScalar(0, 0, 0, 0), 1, 4, MatTypeCV32F)
defer vectors.Close()
vectors.SetFloatAt(0, 0, float32(0.2))
vectors.SetFloatAt(0, 1, float32(0.4))
vectors.SetFloatAt(0, 2, float32(0.4))
vectors.SetFloatAt(0, 3, float32(0.8))

result := NewMat()
defer result.Close()

PCABackProject(data, mean, vectors, &result)
if result.Empty() {
t.Error("PCABackProject should not have empty result.")
}
}

func TestPCACompute(t *testing.T) {
src := NewMatWithSize(10, 10, MatTypeCV32F)
// Set some source data so the PCA is done on a non-zero matrix.
Expand All @@ -2093,6 +2123,45 @@ func TestPCACompute(t *testing.T) {
eigenvalues.Close()
}

func TestPCAProject(t *testing.T) {
data := NewMatWithSize(3, 4, MatTypeCV32F)
defer data.Close()
data.SetFloatAt(0, 0, float32(1))
data.SetFloatAt(0, 1, float32(2))
data.SetFloatAt(0, 2, float32(2))
data.SetFloatAt(0, 3, float32(4))
data.SetFloatAt(1, 0, float32(2))
data.SetFloatAt(1, 1, float32(4))
data.SetFloatAt(1, 2, float32(4))
data.SetFloatAt(1, 3, float32(8))
data.SetFloatAt(2, 0, float32(0))
data.SetFloatAt(2, 1, float32(0))
data.SetFloatAt(2, 2, float32(0))
data.SetFloatAt(2, 3, float32(0))

mean := NewMatWithSize(1, 4, MatTypeCV32F)
defer mean.Close()
mean.SetFloatAt(0, 0, float32(2))
mean.SetFloatAt(0, 1, float32(4))
mean.SetFloatAt(0, 2, float32(4))
mean.SetFloatAt(0, 3, float32(8))

vectors := NewMatWithSizeFromScalar(NewScalar(0, 0, 0, 0), 1, 4, MatTypeCV32F)
defer vectors.Close()
vectors.SetFloatAt(0, 0, float32(0.2))
vectors.SetFloatAt(0, 1, float32(0.4))
vectors.SetFloatAt(0, 2, float32(0.4))
vectors.SetFloatAt(0, 3, float32(0.8))

result := NewMat()
defer result.Close()

PCAProject(data, mean, vectors, &result)
if result.Empty() {
t.Error("PCABackProject should not have empty result.")
}
}

func TestMatExp(t *testing.T) {
src := NewMatWithSize(10, 10, MatTypeCV32F)
dst := NewMat()
Expand Down

0 comments on commit 56dff7a

Please sign in to comment.