Skip to content

Commit

Permalink
API change: Mat.fromScalar
Browse files Browse the repository at this point in the history
  • Loading branch information
rainyl committed Apr 12, 2024
1 parent a0fd236 commit 0e7732f
Show file tree
Hide file tree
Showing 37 changed files with 286 additions and 153 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space # "space""tab"
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

max_line_length = 100
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ OpenCV Bindings for Dart Language.
<p align="center">
<a href="https://codecov.io/github/rainyl/opencv_dart" ><img src="https://codecov.io/github/rainyl/opencv_dart/graph/badge.svg?token=2H0WWT39SN"/></a>
<a href="https://github.com/rainyl/opencv_dart"><img src="https://img.shields.io/github/stars/rainyl/opencv_dart.svg?style=flat&logo=github&label=stars" alt="Star on Github"></a>
<a href="https://pub.dev/packages/opencv_dart"><img src="https://img.shields.io/pub/v/opencv_dart.svg" alt="https://pub.dev/packages/opencv_dart"></a>
<a href="https://opensource.org/license/apache-2-0"><img src="https://img.shields.io/github/license/rainyl/opencv_dart" alt="License: Apache-2.0"></a>
</p>

Expand Down
2 changes: 2 additions & 0 deletions bin/setup_commands.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: avoid_print

import 'dart:convert';
import 'dart:io';

Expand Down
10 changes: 6 additions & 4 deletions lib/src/calib3d/calib3d.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class Fisheye {
R ??= Mat.empty();
P ??= Mat.empty();
undistorted ??= Mat.empty();
cvRun(() => CFFI.Fisheye_UndistortPoints(distorted.ref, undistorted!.ref, K.ref, D.ref, R!.ref, P!.ref));
cvRun(() => CFFI.Fisheye_UndistortPoints(
distorted.ref, undistorted!.ref, K.ref, D.ref, R!.ref, P!.ref));
return undistorted;
}

Expand Down Expand Up @@ -211,7 +212,8 @@ Mat undistort(
}) {
dst ??= Mat.empty();
newCameraMatrix ??= Mat.empty();
cvRun(() => CFFI.Undistort(src.ref, dst!.ref, cameraMatrix.ref, distCoeffs.ref, newCameraMatrix!.ref));
cvRun(() =>
CFFI.Undistort(src.ref, dst!.ref, cameraMatrix.ref, distCoeffs.ref, newCameraMatrix!.ref));
return dst;
}

Expand Down Expand Up @@ -337,8 +339,8 @@ Mat drawChessboardCorners(
bool patternWasFound,
) {
return cvRunArena<Mat>((arena) {
cvRun(() =>
CFFI.DrawChessboardCorners(image.ref, patternSize.toSize(arena).ref, corners.ref, patternWasFound));
cvRun(() => CFFI.DrawChessboardCorners(
image.ref, patternSize.toSize(arena).ref, corners.ref, patternWasFound));
return image;
});
}
Expand Down
36 changes: 24 additions & 12 deletions lib/src/contrib/aruco.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class ArucoDetector extends CvStruct<cvg.ArucoDetector> {
///
/// For further details, please see:
/// https://docs.opencv.org/master/d9/d6a/group__aruco.html#ga3bc50d61fe4db7bce8d26d56b5a6428a
(VecVecPoint2f corners, VecInt ids, VecVecPoint2f rejectedImgPoints) detectMarkers(InputArray image) {
(VecVecPoint2f corners, VecInt ids, VecVecPoint2f rejectedImgPoints) detectMarkers(
InputArray image) {
return using<(VecVecPoint2f, VecInt, VecVecPoint2f)>((arena) {
final pCorners = calloc<cvg.VecVecPoint2f>();
final pRejected = calloc<cvg.VecVecPoint2f>();
Expand All @@ -55,8 +56,10 @@ class ArucoDetector extends CvStruct<cvg.ArucoDetector> {
List<int> get props => [ptr.address];
}

void arucoDrawDetectedMarkers(Mat img, VecVecPoint2f markerCorners, VecInt markerIds, Scalar borderColor) {
cvRun(() => CFFI.ArucoDrawDetectedMarkers(img.ref, markerCorners.ref, markerIds.ref, borderColor.ref));
void arucoDrawDetectedMarkers(
Mat img, VecVecPoint2f markerCorners, VecInt markerIds, Scalar borderColor) {
cvRun(() =>
CFFI.ArucoDrawDetectedMarkers(img.ref, markerCorners.ref, markerIds.ref, borderColor.ref));
}

void arucoGenerateImageMarker(
Expand All @@ -66,7 +69,8 @@ void arucoGenerateImageMarker(
Mat img,
int borderBits,
) {
cvRun(() => CFFI.ArucoGenerateImageMarker(dictionaryId.value, id, sidePixels, img.ref, borderBits));
cvRun(
() => CFFI.ArucoGenerateImageMarker(dictionaryId.value, id, sidePixels, img.ref, borderBits));
}

class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
Expand All @@ -82,7 +86,8 @@ class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {

@override
cvg.ArucoDetectorParameters get ref => ptr.ref;
static final finalizer = OcvFinalizer<cvg.ArucoDetectorParametersPtr>(CFFI.addresses.ArucoDetectorParameters_Close);
static final finalizer =
OcvFinalizer<cvg.ArucoDetectorParametersPtr>(CFFI.addresses.ArucoDetectorParameters_Close);

int get adaptiveThreshWinSizeMin {
return cvRunArena<int>((arena) {
Expand Down Expand Up @@ -180,7 +185,8 @@ class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
});
}

set minDistanceToBorder(int value) => CFFI.ArucoDetectorParameters_SetMinDistanceToBorder(ref, value);
set minDistanceToBorder(int value) =>
CFFI.ArucoDetectorParameters_SetMinDistanceToBorder(ref, value);

double get minMarkerDistanceRate {
return cvRunArena<double>((arena) {
Expand All @@ -201,7 +207,8 @@ class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
});
}

set cornerRefinementMethod(int value) => CFFI.ArucoDetectorParameters_SetCornerRefinementMethod(ref, value);
set cornerRefinementMethod(int value) =>
CFFI.ArucoDetectorParameters_SetCornerRefinementMethod(ref, value);

int get cornerRefinementWinSize {
return cvRunArena<int>((arena) {
Expand Down Expand Up @@ -297,7 +304,8 @@ class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
});
}

set errorCorrectionRate(double value) => CFFI.ArucoDetectorParameters_SetErrorCorrectionRate(ref, value);
set errorCorrectionRate(double value) =>
CFFI.ArucoDetectorParameters_SetErrorCorrectionRate(ref, value);

double get aprilTagQuadDecimate {
return cvRunArena<double>((arena) {
Expand All @@ -307,7 +315,8 @@ class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
});
}

set aprilTagQuadDecimate(double value) => CFFI.ArucoDetectorParameters_SetAprilTagQuadDecimate(ref, value);
set aprilTagQuadDecimate(double value) =>
CFFI.ArucoDetectorParameters_SetAprilTagQuadDecimate(ref, value);

double get aprilTagQuadSigma {
return cvRunArena<double>((arena) {
Expand All @@ -317,7 +326,8 @@ class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
});
}

set aprilTagQuadSigma(double value) => CFFI.ArucoDetectorParameters_SetAprilTagQuadSigma(ref, value);
set aprilTagQuadSigma(double value) =>
CFFI.ArucoDetectorParameters_SetAprilTagQuadSigma(ref, value);

int get aprilTagMinClusterPixels {
return cvRunArena<int>((arena) {
Expand All @@ -338,7 +348,8 @@ class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
});
}

set aprilTagMaxNmaxima(int value) => CFFI.ArucoDetectorParameters_SetAprilTagMaxNmaxima(ref, value);
set aprilTagMaxNmaxima(int value) =>
CFFI.ArucoDetectorParameters_SetAprilTagMaxNmaxima(ref, value);

double get aprilTagCriticalRad {
return cvRunArena<double>((arena) {
Expand All @@ -348,7 +359,8 @@ class ArucoDetectorParameters extends CvStruct<cvg.ArucoDetectorParameters> {
});
}

set aprilTagCriticalRad(double value) => CFFI.ArucoDetectorParameters_SetAprilTagCriticalRad(ref, value);
set aprilTagCriticalRad(double value) =>
CFFI.ArucoDetectorParameters_SetAprilTagCriticalRad(ref, value);

double get aprilTagMaxLineFitMse {
return cvRunArena<double>((arena) {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/contrib/aruco_dict.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class ArucoDictionary extends CvStruct<cvg.ArucoDictionary> {

@override
cvg.ArucoDictionary get ref => ptr.ref;
static final finalizer = OcvFinalizer<cvg.ArucoDictionaryPtr>(CFFI.addresses.ArucoDictionary_Close);
static final finalizer =
OcvFinalizer<cvg.ArucoDictionaryPtr>(CFFI.addresses.ArucoDictionary_Close);

@override
List<int> get props => [ptr.address];
Expand Down
9 changes: 6 additions & 3 deletions lib/src/contrib/img_hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ const int BLOCK_MEAN_HASH_MODE_1 = 1;

/// BlockMeanHash is implementation of the BlockMeanHash algorithm.
class BlockMeanHash extends CvStruct<cvg.BlockMeanHash> implements ImgHashBase {
BlockMeanHash._(cvg.BlockMeanHashPtr ptr, [this._mode = BLOCK_MEAN_HASH_MODE_0]) : super.fromPointer(ptr) {
BlockMeanHash._(cvg.BlockMeanHashPtr ptr, [this._mode = BLOCK_MEAN_HASH_MODE_0])
: super.fromPointer(ptr) {
finalizer.attach(this, ptr.cast());
}
static final finalizer = OcvFinalizer<ffi.Pointer<cvg.BlockMeanHash>>(CFFI.addresses.BlockMeanHash_Close);
static final finalizer =
OcvFinalizer<ffi.Pointer<cvg.BlockMeanHash>>(CFFI.addresses.BlockMeanHash_Close);

factory BlockMeanHash({int mode = BLOCK_MEAN_HASH_MODE_0}) {
final p = calloc<cvg.BlockMeanHash>();
Expand Down Expand Up @@ -223,7 +225,8 @@ class NewRadialVarianceHash implements ImgHashBase {
double compare(InputArray hashOne, InputArray hashTwo) {
return using<double>((arena) {
final p = arena<ffi.Double>();
cvRun(() => CFFI.radialVarianceHashCompare(hashOne.ref, hashTwo.ref, sigma, numOfAngleLine, p));
cvRun(
() => CFFI.radialVarianceHashCompare(hashOne.ref, hashTwo.ref, sigma, numOfAngleLine, p));
return p.value;
});
}
Expand Down
18 changes: 12 additions & 6 deletions lib/src/core/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ Mat insertChannel(InputArray src, InputOutputArray dst, int coi) {
final rval = cvRunArena<double>((arena) {
final p = arena<ffi.Double>();
cvRun(
() => CFFI.KMeans(
data.ref, K, bestLabels.ref, criteria.toTermCriteria(arena).ref, attempts, flags, centers!.ref, p),
() => CFFI.KMeans(data.ref, K, bestLabels.ref, criteria.toTermCriteria(arena).ref, attempts,
flags, centers!.ref, p),
);
return p.value;
});
Expand All @@ -697,8 +697,8 @@ Mat insertChannel(InputArray src, InputOutputArray dst, int coi) {
final rval = cvRunArena<double>((arena) {
final p = arena<ffi.Double>();
cvRun(
() => CFFI.KMeansPoints(
pts.ref, K, bestLabels.ref, criteria.toTermCriteria(arena).ref, attempts, flags, centers!.ref, p),
() => CFFI.KMeansPoints(pts.ref, K, bestLabels.ref, criteria.toTermCriteria(arena).ref,
attempts, flags, centers!.ref, p),
);
return p.value;
});
Expand Down Expand Up @@ -793,14 +793,20 @@ Mat min(InputArray src1, InputArray src2, {OutputArray? dst}) {
///
/// For further details, please see:
/// https://docs.opencv.org/trunk/d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707
(double minVal, double maxVal, Point minLoc, Point maxLoc) minMaxLoc(InputArray src, {InputArray? mask}) {
(double minVal, double maxVal, Point minLoc, Point maxLoc) minMaxLoc(InputArray src,
{InputArray? mask}) {
return using<(double, double, Point, Point)>((arena) {
final minValP = arena<ffi.Double>();
final maxValP = arena<ffi.Double>();
final minLocP = arena<cvg.Point>();
final maxLocP = arena<cvg.Point>();
cvRun(() => CFFI.Mat_MinMaxLoc(src.ref, minValP, maxValP, minLocP, maxLocP));
return (minValP.value, maxValP.value, Point.fromNative(minLocP.ref), Point.fromNative(maxLocP.ref));
return (
minValP.value,
maxValP.value,
Point.fromNative(minLocP.ref),
Point.fromNative(maxLocP.ref)
);
});
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/core/exception.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: avoid_print

import 'dart:ffi' as ffi;
import 'dart:io';

Expand Down
3 changes: 2 additions & 1 deletion lib/src/core/keypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class KeyPoint extends CvStruct<cvg.KeyPoint> {
KeyPoint._(ffi.Pointer<cvg.KeyPoint> ptr) : super.fromPointer(ptr) {
finalizer.attach(this, ptr.cast());
}
factory KeyPoint(double x, double y, double size, double angle, double response, int octave, int classID) {
factory KeyPoint(
double x, double y, double size, double angle, double response, int octave, int classID) {
final ptr = calloc<cvg.KeyPoint>()
..ref.x = x
..ref.y = y
Expand Down
40 changes: 23 additions & 17 deletions lib/src/core/mat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Mat extends CvStruct<cvg.Mat> {
return mat;
}

factory Mat.fromScalar(Scalar s, MatType type, {int rows = 1, int cols = 1}) {
factory Mat.fromScalar(int rows, int cols, MatType type, Scalar s) {
final p = calloc<cvg.Mat>();
cvRun(() => CFFI.Mat_NewWithSizeFromScalar(s.ref, rows, cols, type.toInt32(), p));
final mat = Mat._(p);
Expand Down Expand Up @@ -617,7 +617,8 @@ class Mat extends CvStruct<cvg.Mat> {
}

Mat addU8(int val, {bool inplace = false}) {
assert(type.depth == MatType.CV_8U && val >= CV_U8_MIN && val <= CV_U8_MAX, "addU8() only for CV_8U");
assert(type.depth == MatType.CV_8U && val >= CV_U8_MIN && val <= CV_U8_MAX,
"addU8() only for CV_8U");
if (inplace) {
cvRun(() => CFFI.Mat_AddUChar(ref, val));
return this;
Expand All @@ -629,7 +630,8 @@ class Mat extends CvStruct<cvg.Mat> {
}

Mat addI8(int val, {bool inplace = false}) {
assert(type.depth == MatType.CV_8S && val >= CV_I8_MIN && val <= CV_I8_MAX, "addI8() only for CV_8S");
assert(type.depth == MatType.CV_8S && val >= CV_I8_MIN && val <= CV_I8_MAX,
"addI8() only for CV_8S");
if (inplace) {
cvRun(() => CFFI.Mat_AddSChar(ref, val));
return this;
Expand All @@ -641,8 +643,8 @@ class Mat extends CvStruct<cvg.Mat> {
}

Mat addI32(int val, {bool inplace = false}) {
assert(
type.depth == MatType.CV_32S && val >= CV_I32_MIN && val <= CV_I32_MAX, "addI32() only for CV_32S");
assert(type.depth == MatType.CV_32S && val >= CV_I32_MIN && val <= CV_I32_MAX,
"addI32() only for CV_32S");
if (inplace) {
cvRun(() => CFFI.Mat_AddI32(ref, val));
return this;
Expand Down Expand Up @@ -719,8 +721,8 @@ class Mat extends CvStruct<cvg.Mat> {
}

Mat subtractU8(int val, {bool inplace = false}) {
assert(
type.depth == MatType.CV_8U && val >= CV_U8_MIN && val <= CV_U8_MAX, "subtractU8() only for CV_8U");
assert(type.depth == MatType.CV_8U && val >= CV_U8_MIN && val <= CV_U8_MAX,
"subtractU8() only for CV_8U");
if (inplace) {
cvRun(() => CFFI.Mat_SubtractUChar(ref, val));
return this;
Expand All @@ -732,8 +734,8 @@ class Mat extends CvStruct<cvg.Mat> {
}

Mat subtractI8(int val, {bool inplace = false}) {
assert(
type.depth == MatType.CV_8S && val >= CV_I8_MIN && val <= CV_I8_MAX, "subtractI8() only for CV_8S");
assert(type.depth == MatType.CV_8S && val >= CV_I8_MIN && val <= CV_I8_MAX,
"subtractI8() only for CV_8S");
if (inplace) {
cvRun(() => CFFI.Mat_SubtractSChar(ref, val));
return this;
Expand Down Expand Up @@ -823,8 +825,8 @@ class Mat extends CvStruct<cvg.Mat> {
}

Mat multiplyU8(int val, {bool inplace = false}) {
assert(
type.depth == MatType.CV_8U && val >= CV_U8_MIN && val <= CV_U8_MAX, "multiplyU8() only for CV_8U");
assert(type.depth == MatType.CV_8U && val >= CV_U8_MIN && val <= CV_U8_MAX,
"multiplyU8() only for CV_8U");
if (inplace) {
cvRun(() => CFFI.Mat_MultiplyUChar(ref, val));
return this;
Expand All @@ -836,8 +838,8 @@ class Mat extends CvStruct<cvg.Mat> {
}

Mat multiplyI8(int val, {bool inplace = false}) {
assert(
type.depth == MatType.CV_8S && val >= CV_I8_MIN && val <= CV_I8_MAX, "multiplyI8() only for CV_8S");
assert(type.depth == MatType.CV_8S && val >= CV_I8_MIN && val <= CV_I8_MAX,
"multiplyI8() only for CV_8S");
if (inplace) {
cvRun(() => CFFI.Mat_MultiplySChar(ref, val));
return this;
Expand Down Expand Up @@ -927,7 +929,8 @@ class Mat extends CvStruct<cvg.Mat> {
}

Mat divideU8(int val, {bool inplace = false}) {
assert(type.depth == MatType.CV_8U && val >= CV_U8_MIN && val <= CV_U8_MAX, "divideU8() only for CV_8U");
assert(type.depth == MatType.CV_8U && val >= CV_U8_MIN && val <= CV_U8_MAX,
"divideU8() only for CV_8U");
if (inplace) {
cvRun(() => CFFI.Mat_DivideUChar(ref, val));
return this;
Expand All @@ -939,7 +942,8 @@ class Mat extends CvStruct<cvg.Mat> {
}

Mat divideI8(int val, {bool inplace = false}) {
assert(type.depth == MatType.CV_8S && val >= CV_I8_MIN && val <= CV_I8_MAX, "divideI8() only for CV_8S");
assert(type.depth == MatType.CV_8S && val >= CV_I8_MIN && val <= CV_I8_MAX,
"divideI8() only for CV_8S");
if (inplace) {
cvRun(() => CFFI.Mat_DivideSChar(ref, val));
return this;
Expand Down Expand Up @@ -1075,7 +1079,8 @@ class Mat extends CvStruct<cvg.Mat> {

/// Calculates a square root of array elements.
Mat sqrt() {
assert(type.depth == MatType.CV_32F || type.depth == MatType.CV_64F, "sqrt() only for CV_32F or CV_64F");
assert(type.depth == MatType.CV_32F || type.depth == MatType.CV_64F,
"sqrt() only for CV_32F or CV_64F");
final p = calloc<cvg.Mat>();
cvRun(() => CFFI.Mat_Sqrt(ref, p));
final dst = Mat._(p);
Expand Down Expand Up @@ -1132,7 +1137,8 @@ class Mat extends CvStruct<cvg.Mat> {
/// ```
List<List<List<P>>> toList3D<T extends CvVec, P extends num>() {
assert(channels >= 2, "toList3D() only for channels >= 2, but this.channels=$channels");
return List.generate(rows, (row) => List.generate(cols, (col) => at<T>(row, col).val as List<P>));
return List.generate(
rows, (row) => List.generate(cols, (col) => at<T>(row, col).val as List<P>));
}

/// Get the data pointer of the Mat, this getter will reture a view of native
Expand Down
3 changes: 2 additions & 1 deletion lib/src/core/point.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class Point3f extends CvStruct<cvg.Point3f> {
@override
cvg.Point3f get ref => ptr.ref;
@override
String toString() => 'Point3f(${x.toStringAsFixed(3)}, ${y.toStringAsFixed(3)}, ${z.toStringAsFixed(3)})';
String toString() =>
'Point3f(${x.toStringAsFixed(3)}, ${y.toStringAsFixed(3)}, ${z.toStringAsFixed(3)})';
@override
List<double> get props => [x, y, z];
}
Expand Down
Loading

0 comments on commit 0e7732f

Please sign in to comment.