Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: add missing array functions #1119

Merged
merged 5 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ Your pull requests will be greatly appreciated!

- [ ] **core. Core functionality - WORK STARTED**
- [X] **Basic structures**
- [ ] **Operations on arrays - WORK STARTED**. The following functions still need implementation:
- [ ] [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] **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)
- [X] [mulTransposed](https://docs.opencv.org/master/d2/de8/group__core__array.html#gadc4e49f8f7a155044e3be1b9e3b270ab)
- [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)
- [ ] [PSNR](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga07aaf34ae31d226b1b847d8bcff3698f)
- [ ] [randn](https://docs.opencv.org/master/d2/de8/group__core__array.html#gaeff1f61e972d133a04ce3a5f81cf6808)
- [ ] [randShuffle](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga6a789c8a5cb56c6dd62506179808f763)
- [ ] [randu](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga1ba1026dca0807b27057ba6a49d258c0)
- [ ] [setRNGSeed](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga757e657c037410d9e19e819569e7de0f)
- [ ] [SVBackSubst](https://docs.opencv.org/master/d2/de8/group__core__array.html#gab4e620e6fc6c8a27bb2be3d50a840c0b)
- [ ] [SVDecomp](https://docs.opencv.org/master/d2/de8/group__core__array.html#gab477b5b7b39b370bb03e75b19d2d5109)
- [ ] [theRNG](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga75843061d150ad6564b5447e38e57722)
- [X] [PCAProject](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga6b9fbc7b3a99ebfd441bbec0a6bc4f88)
- [X] [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)
- [X] [randu](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga1ba1026dca0807b27057ba6a49d258c0)
- [X] [setRNGSeed](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga757e657c037410d9e19e819569e7de0f)
- [X] [SVBackSubst](https://docs.opencv.org/master/d2/de8/group__core__array.html#gab4e620e6fc6c8a27bb2be3d50a840c0b)
- [X] [SVDecomp](https://docs.opencv.org/master/d2/de8/group__core__array.html#gab477b5b7b39b370bb03e75b19d2d5109)
- [X] [theRNG](https://docs.opencv.org/master/d2/de8/group__core__array.html#ga75843061d150ad6564b5447e38e57722)

- [ ] XML/YAML Persistence
- [ ] [FileStorage](https://docs.opencv.org/master/da/d56/classcv_1_1FileStorage.html)
Expand Down
40 changes: 40 additions & 0 deletions core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ bool Mat_IsContinuous(Mat m) {
return m->isContinuous();
}

void Mat_Inv(Mat m) {
m->inv();
}

Mat Mat_Col(Mat m, int c) {
return new cv::Mat(m->col(c));
}

Mat Mat_Row(Mat m, int r) {
return new cv::Mat(m->row(r));
}

// Mat_Clone returns a clone of this Mat
Mat Mat_Clone(Mat m) {
return new cv::Mat(m->clone());
Expand Down Expand Up @@ -504,10 +516,30 @@ 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);
}

double PSNR(Mat src1, Mat src2) {
return cv::PSNR(*src1, *src2);
}

void SVBackSubst(Mat w, Mat u, Mat vt, Mat rhs, Mat dst) {
cv::SVBackSubst(*w, *u, *vt, *rhs, *dst);
}

void SVDecomp(Mat src, Mat w, Mat u, Mat vt) {
cv::SVDecomp(*src, *w, *u, *vt);
}

void Mat_Exp(Mat src, Mat dst) {
cv::exp(*src, *dst);
}
Expand Down Expand Up @@ -591,6 +623,14 @@ void Mat_Magnitude(Mat x, Mat y, Mat magnitude) {
cv::magnitude(*x, *y, *magnitude);
}

double Mat_Mahalanobis(Mat v1, Mat v2, Mat icovar) {
return cv::Mahalanobis(*v1, *v2, *icovar);
}

void MulTransposed(Mat src, Mat dest, bool ata) {
cv::mulTransposed(*src, *dest, ata);
}

void Mat_Max(Mat src1, Mat src2, Mat dst) {
cv::max(*src1, *src2, *dst);
}
Expand Down
76 changes: 76 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,26 @@ func (m *Mat) IsContinuous() bool {
return bool(C.Mat_IsContinuous(m.p))
}

// Inv inverses a matrix.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html#a039eb3c6740a850696a12519a4b8bfc6
func (m *Mat) Inv() {
C.Mat_Inv(m.p)
}

// Col creates a matrix header for the specified matrix column.
// The underlying data of the new matrix is shared with the original matrix.
func (m *Mat) Col(col int) Mat {
return newMat(C.Mat_Col(m.p, C.int(col)))
}

// Row creates a matrix header for the specified matrix row.
// The underlying data of the new matrix is shared with the original matrix.
func (m *Mat) Row(row int) Mat {
return newMat(C.Mat_Row(m.p, C.int(row)))
}

// Clone returns a cloned full copy of the Mat.
func (m *Mat) Clone() Mat {
return newMat(C.Mat_Clone(m.p))
Expand Down Expand Up @@ -1176,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 @@ -1189,6 +1217,38 @@ 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)
}

// PSNR computes the Peak Signal-to-Noise Ratio (PSNR) image quality metric.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d2/de8/group__core__array.html#ga3119e3ea73010a6f810bb05aa36ac8d6
func PSNR(src1 Mat, src2 Mat) float64 {
return float64(C.PSNR(src1.p, src2.p))
}

// SVBackSubst performs a singular value back substitution.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d2/de8/group__core__array.html#gab4e620e6fc6c8a27bb2be3d50a840c0b
func SVBackSubst(w Mat, u Mat, vt Mat, rhs Mat, dst *Mat) {
C.SVBackSubst(w.p, u.p, vt.p, rhs.p, dst.p)
}

// SVDecomp decomposes matrix and stores the results to user-provided matrices.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d2/de8/group__core__array.html#gab477b5b7b39b370bb03e75b19d2d5109
func SVDecomp(src Mat, w *Mat, u *Mat, vt *Mat) {
C.SVDecomp(src.p, w.p, u.p, vt.p)
}

// Exp calculates the exponent of every array element.
//
// For further details, please see:
Expand Down Expand Up @@ -1391,6 +1451,22 @@ func Magnitude(x, y Mat, magnitude *Mat) {
C.Mat_Magnitude(x.p, y.p, magnitude.p)
}

// Mahalanobis calculates the Mahalanobis distance between two vectors.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d2/de8/group__core__array.html#ga4493aee129179459cbfc6064f051aa7d
func Mahalanobis(v1, v2, icovar Mat) float64 {
return float64(C.Mat_Mahalanobis(v1.p, v2.p, icovar.p))
}

// MulTransposed calculates the product of a matrix and its transposition.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d2/de8/group__core__array.html#gadc4e49f8f7a155044e3be1b9e3b270ab
func MulTransposed(src Mat, dest *Mat, ata bool) {
C.MulTransposed(src.p, dest.p, C.bool(ata))
}

// Max calculates per-element maximum of two arrays or an array and a scalar.
//
// For further details, please see:
Expand Down
10 changes: 10 additions & 0 deletions core.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ Mat Mat_FromPtr(Mat m, int rows, int cols, int type, int prows, int pcols);
void Mat_Close(Mat m);
int Mat_Empty(Mat m);
bool Mat_IsContinuous(Mat m);
void Mat_Inv(Mat m);
Mat Mat_Col(Mat m, int c);
Mat Mat_Row(Mat m, int r);
Mat Mat_Clone(Mat m);
void Mat_CopyTo(Mat m, Mat dst);
int Mat_Total(Mat m);
Expand Down Expand Up @@ -379,7 +382,12 @@ 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);
double PSNR(Mat src1, Mat src2);
void SVBackSubst(Mat w, Mat u, Mat vt, Mat rhs, Mat dst);
void SVDecomp(Mat src, Mat w, Mat u, Mat vt);
void Mat_Exp(Mat src, Mat dst);
void Mat_ExtractChannel(Mat src, Mat dst, int coi);
void Mat_FindNonZero(Mat src, Mat idx);
Expand All @@ -399,6 +407,8 @@ double KMeans(Mat data, int k, Mat bestLabels, TermCriteria criteria, int attemp
double KMeansPoints(PointVector pts, int k, Mat bestLabels, TermCriteria criteria, int attempts, int flags, Mat centers);
void Mat_Log(Mat src, Mat dst);
void Mat_Magnitude(Mat x, Mat y, Mat magnitude);
double Mat_Mahalanobis(Mat v1, Mat v2, Mat icovar);
void MulTransposed(Mat src, Mat dest, bool ata);
void Mat_Max(Mat src1, Mat src2, Mat dst);
void Mat_MeanStdDev(Mat src, Mat dstMean, Mat dstStdDev);
void Mat_Merge(struct Mats mats, Mat dst);
Expand Down
Loading
Loading