Merge pull request #20731 from komakai:matrix_mult_android_ios

pull/20817/head
Alexander Alekhin 3 years ago
commit 13c6eb42e9
  1. 22
      modules/core/misc/java/src/java/core+Mat.java
  2. 3
      modules/core/misc/java/src/java/core+MatMatMul.kt
  3. 10
      modules/core/misc/java/test/MatTest.java
  4. 10
      modules/core/misc/objc/common/Mat.h
  5. 5
      modules/core/misc/objc/common/Mat.mm
  6. 6
      modules/core/misc/objc/common/MatExt.swift
  7. 10
      modules/core/misc/objc/test/MatTest.swift
  8. 26
      modules/java/generator/src/cpp/Mat.cpp

@ -466,16 +466,32 @@ public class Mat {
// C++: Mat Mat::mul(Mat m, double scale = 1) // C++: Mat Mat::mul(Mat m, double scale = 1)
// //
// javadoc: Mat::mul(m, scale) /**
* Element-wise multiplication with scale factor
* @param m operand with with which to perform element-wise multiplication
* @param scale scale factor
*/
public Mat mul(Mat m, double scale) { public Mat mul(Mat m, double scale) {
return new Mat(n_mul(nativeObj, m.nativeObj, scale)); return new Mat(n_mul(nativeObj, m.nativeObj, scale));
} }
// javadoc: Mat::mul(m) /**
* Element-wise multiplication
* @param m operand with with which to perform element-wise multiplication
*/
public Mat mul(Mat m) { public Mat mul(Mat m) {
return new Mat(n_mul(nativeObj, m.nativeObj)); return new Mat(n_mul(nativeObj, m.nativeObj));
} }
/**
* Matrix multiplication
* @param m operand with with which to perform matrix multiplication
* @see Core#gemm(Mat, Mat, double, Mat, double, Mat, int)
*/
public Mat matMul(Mat m) {
return new Mat(n_matMul(nativeObj, m.nativeObj));
}
// //
// C++: static Mat Mat::ones(int rows, int cols, int type) // C++: static Mat Mat::ones(int rows, int cols, int type)
// //
@ -1732,6 +1748,8 @@ public class Mat {
private static native long n_mul(long nativeObj, long m_nativeObj); private static native long n_mul(long nativeObj, long m_nativeObj);
private static native long n_matMul(long nativeObj, long m_nativeObj);
// C++: static Mat Mat::ones(int rows, int cols, int type) // C++: static Mat Mat::ones(int rows, int cols, int type)
private static native long n_ones(int rows, int cols, int type); private static native long n_ones(int rows, int cols, int type);

@ -0,0 +1,3 @@
package org.opencv.core
operator fun Mat.times(other: Mat): Mat = this.matMul(other)

@ -686,6 +686,16 @@ public class MatTest extends OpenCVTestCase {
assertMatEqual(truth, dst, EPS); assertMatEqual(truth, dst, EPS);
} }
public void testMatMulMat() {
Mat m1 = new Mat(2, 2, CvType.CV_32F, new Scalar(2));
Mat m2 = new Mat(2, 2, CvType.CV_32F, new Scalar(3));
dst = m1.matMul(m2);
truth = new Mat(2, 2, CvType.CV_32F, new Scalar(12));
assertMatEqual(truth, dst, EPS);
}
public void testOnesIntIntInt() { public void testOnesIntIntInt() {
dst = Mat.ones(matSize, matSize, CvType.CV_32F); dst = Mat.ones(matSize, matSize, CvType.CV_32F);

@ -114,7 +114,17 @@ CV_EXPORTS @interface Mat : NSObject
- (BOOL)isSubmatrix; - (BOOL)isSubmatrix;
- (void)locateROI:(Size2i*)wholeSize ofs:(Point2i*)offset NS_SWIFT_NAME(locateROI(wholeSize:offset:)); - (void)locateROI:(Size2i*)wholeSize ofs:(Point2i*)offset NS_SWIFT_NAME(locateROI(wholeSize:offset:));
- (Mat*)mul:(Mat*)mat scale:(double)scale; - (Mat*)mul:(Mat*)mat scale:(double)scale;
/**
Performs element-wise multiplication
@param mat operand with with which to perform element-wise multiplication
*/
- (Mat*)mul:(Mat*)mat; - (Mat*)mul:(Mat*)mat;
/**
Performs matrix multiplication
@param mat operand with with which to perform matrix multiplication
@see `Core.gemm(...)`
*/
- (Mat*)matMul:(Mat*)mat;
+ (Mat*)ones:(int)rows cols:(int)cols type:(int)type NS_SWIFT_NAME(ones(rows:cols:type:)); + (Mat*)ones:(int)rows cols:(int)cols type:(int)type NS_SWIFT_NAME(ones(rows:cols:type:));
+ (Mat*)ones:(Size2i*)size type:(int)type NS_SWIFT_NAME(ones(size:type:)); + (Mat*)ones:(Size2i*)size type:(int)type NS_SWIFT_NAME(ones(size:type:));
+ (Mat*)onesEx:(NSArray<NSNumber*>*)sizes type:(int)type NS_SWIFT_NAME(ones(sizes:type:)); + (Mat*)onesEx:(NSArray<NSNumber*>*)sizes type:(int)type NS_SWIFT_NAME(ones(sizes:type:));

@ -372,6 +372,11 @@ static bool updateIdx(cv::Mat* mat, std::vector<int>& indices, size_t inc) {
return [[Mat alloc] initWithNativeMat:new cv::Mat(_nativePtr->mul(*(cv::Mat*)mat.nativePtr))]; return [[Mat alloc] initWithNativeMat:new cv::Mat(_nativePtr->mul(*(cv::Mat*)mat.nativePtr))];
} }
- (Mat*)matMul:(Mat*)mat {
cv::Mat temp = self.nativeRef * mat.nativeRef;
return [Mat fromNative:temp];
}
+ (Mat*)ones:(int)rows cols:(int)cols type:(int)type { + (Mat*)ones:(int)rows cols:(int)cols type:(int)type {
return [[Mat alloc] initWithNativeMat:new cv::Mat(cv::Mat::ones(rows, cols, type))]; return [[Mat alloc] initWithNativeMat:new cv::Mat(cv::Mat::ones(rows, cols, type))];
} }

@ -715,3 +715,9 @@ public extension Mat {
return MatAt(mat: self, indices: indices) return MatAt(mat: self, indices: indices)
} }
} }
public extension Mat {
static func *(lhs:Mat, rhs: Mat) -> Mat {
return lhs.matMul(rhs)
}
}

@ -683,6 +683,16 @@ class MatTests: OpenCVTestCase {
try assertMatEqual(truth!, dst, OpenCVTestCase.EPS) try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
} }
func testMatMulMat() throws {
let m1 = Mat(rows: 2, cols: 2, type: CvType.CV_32F, scalar: Scalar(2))
let m2 = Mat(rows: 2, cols: 2, type: CvType.CV_32F, scalar: Scalar(3))
dst = m1.matMul(m2)
truth = Mat(rows: 2, cols: 2, type: CvType.CV_32F, scalar: Scalar(12))
try assertMatEqual(truth!, dst, OpenCVTestCase.EPS)
}
func testOnesIntIntInt() throws { func testOnesIntIntInt() throws {
dst = Mat.ones(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32F) dst = Mat.ones(rows: OpenCVTestCase.matSize, cols: OpenCVTestCase.matSize, type: CvType.CV_32F)

@ -1372,6 +1372,32 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1mul__JJ
} }
//
// Mat Mat Mat::matMul(Mat m)
//
JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1matMul__JJ
(JNIEnv* env, jclass, jlong self, jlong m_nativeObj);
JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1matMul__JJ
(JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
{
static const char method_name[] = "Mat::n_1matMul__JJ()";
try {
LOGD("%s", method_name);
Mat* me = (Mat*) self; //TODO: check for NULL
Mat& m = *((Mat*)m_nativeObj);
Mat _retval_ = (*me) * m;
return (jlong) new Mat(_retval_);
} catch(const std::exception &e) {
throwJavaException(env, &e, method_name);
} catch (...) {
throwJavaException(env, 0, method_name);
}
return 0;
}
// //
// static Mat Mat::ones(int rows, int cols, int type) // static Mat Mat::ones(int rows, int cols, int type)

Loading…
Cancel
Save