mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
Merge branch 4.x
This commit is contained in:
@@ -1128,6 +1128,458 @@ public class Mat {
|
||||
return cols();
|
||||
}
|
||||
|
||||
// javadoc:Mat::at(clazz, row, col)
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Atable<T> at(Class<T> clazz, int row, int col) {
|
||||
if (clazz == Byte.class || clazz == byte.class) {
|
||||
return (Atable<T>)new AtableByte(this, row, col);
|
||||
} else if (clazz == Double.class || clazz == double.class) {
|
||||
return (Atable<T>)new AtableDouble(this, row, col);
|
||||
} else if (clazz == Float.class || clazz == float.class) {
|
||||
return (Atable<T>)new AtableFloat(this, row, col);
|
||||
} else if (clazz == Integer.class || clazz == int.class) {
|
||||
return (Atable<T>)new AtableInteger(this, row, col);
|
||||
} else if (clazz == Short.class || clazz == short.class) {
|
||||
return (Atable<T>)new AtableShort(this, row, col);
|
||||
} else {
|
||||
throw new RuntimeException("Unsupported class type");
|
||||
}
|
||||
}
|
||||
|
||||
// javadoc:Mat::at(clazz, idx)
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Atable<T> at(Class<T> clazz, int[] idx) {
|
||||
if (clazz == Byte.class || clazz == byte.class) {
|
||||
return (Atable<T>)new AtableByte(this, idx);
|
||||
} else if (clazz == Double.class || clazz == double.class) {
|
||||
return (Atable<T>)new AtableDouble(this, idx);
|
||||
} else if (clazz == Float.class || clazz == float.class) {
|
||||
return (Atable<T>)new AtableFloat(this, idx);
|
||||
} else if (clazz == Integer.class || clazz == int.class) {
|
||||
return (Atable<T>)new AtableInteger(this, idx);
|
||||
} else if (clazz == Short.class || clazz == short.class) {
|
||||
return (Atable<T>)new AtableShort(this, idx);
|
||||
} else {
|
||||
throw new RuntimeException("Unsupported class parameter");
|
||||
}
|
||||
}
|
||||
|
||||
public static class Tuple2<T> {
|
||||
public Tuple2(T _0, T _1) {
|
||||
this._0 = _0;
|
||||
this._1 = _1;
|
||||
}
|
||||
|
||||
public T get_0() {
|
||||
return _0;
|
||||
}
|
||||
|
||||
public T get_1() {
|
||||
return _1;
|
||||
}
|
||||
|
||||
private final T _0;
|
||||
private final T _1;
|
||||
}
|
||||
|
||||
public static class Tuple3<T> {
|
||||
public Tuple3(T _0, T _1, T _2) {
|
||||
this._0 = _0;
|
||||
this._1 = _1;
|
||||
this._2 = _2;
|
||||
}
|
||||
|
||||
public T get_0() {
|
||||
return _0;
|
||||
}
|
||||
|
||||
public T get_1() {
|
||||
return _1;
|
||||
}
|
||||
|
||||
public T get_2() {
|
||||
return _2;
|
||||
}
|
||||
|
||||
private final T _0;
|
||||
private final T _1;
|
||||
private final T _2;
|
||||
}
|
||||
|
||||
public static class Tuple4<T> {
|
||||
public Tuple4(T _0, T _1, T _2, T _3) {
|
||||
this._0 = _0;
|
||||
this._1 = _1;
|
||||
this._2 = _2;
|
||||
this._3 = _3;
|
||||
}
|
||||
|
||||
public T get_0() {
|
||||
return _0;
|
||||
}
|
||||
|
||||
public T get_1() {
|
||||
return _1;
|
||||
}
|
||||
|
||||
public T get_2() {
|
||||
return _2;
|
||||
}
|
||||
|
||||
public T get_3() {
|
||||
return _3;
|
||||
}
|
||||
|
||||
private final T _0;
|
||||
private final T _1;
|
||||
private final T _2;
|
||||
private final T _3;
|
||||
}
|
||||
|
||||
public interface Atable<T> {
|
||||
T getV();
|
||||
void setV(T v);
|
||||
Tuple2<T> getV2c();
|
||||
void setV2c(Tuple2<T> v);
|
||||
Tuple3<T> getV3c();
|
||||
void setV3c(Tuple3<T> v);
|
||||
Tuple4<T> getV4c();
|
||||
void setV4c(Tuple4<T> v);
|
||||
}
|
||||
|
||||
private static class AtableBase {
|
||||
|
||||
protected AtableBase(Mat mat, int row, int col) {
|
||||
this.mat = mat;
|
||||
indices = new int[2];
|
||||
indices[0] = row;
|
||||
indices[1] = col;
|
||||
}
|
||||
|
||||
protected AtableBase(Mat mat, int[] indices) {
|
||||
this.mat = mat;
|
||||
this.indices = indices;
|
||||
}
|
||||
|
||||
protected final Mat mat;
|
||||
protected final int[] indices;
|
||||
}
|
||||
|
||||
private static class AtableByte extends AtableBase implements Atable<Byte> {
|
||||
|
||||
public AtableByte(Mat mat, int row, int col) {
|
||||
super(mat, row, col);
|
||||
}
|
||||
|
||||
public AtableByte(Mat mat, int[] indices) {
|
||||
super(mat, indices);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte getV() {
|
||||
byte[] data = new byte[1];
|
||||
mat.get(indices, data);
|
||||
return data[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV(Byte v) {
|
||||
byte[] data = new byte[] { v };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple2<Byte> getV2c() {
|
||||
byte[] data = new byte[2];
|
||||
mat.get(indices, data);
|
||||
return new Tuple2<Byte>(data[0], data[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV2c(Tuple2<Byte> v) {
|
||||
byte[] data = new byte[] { v._0, v._1 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple3<Byte> getV3c() {
|
||||
byte[] data = new byte[3];
|
||||
mat.get(indices, data);
|
||||
return new Tuple3<Byte>(data[0], data[1], data[2]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV3c(Tuple3<Byte> v) {
|
||||
byte[] data = new byte[] { v._0, v._1, v._2 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple4<Byte> getV4c() {
|
||||
byte[] data = new byte[4];
|
||||
mat.get(indices, data);
|
||||
return new Tuple4<Byte>(data[0], data[1], data[2], data[3]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV4c(Tuple4<Byte> v) {
|
||||
byte[] data = new byte[] { v._0, v._1, v._2, v._3 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
}
|
||||
|
||||
private static class AtableDouble extends AtableBase implements Atable<Double> {
|
||||
|
||||
public AtableDouble(Mat mat, int row, int col) {
|
||||
super(mat, row, col);
|
||||
}
|
||||
|
||||
public AtableDouble(Mat mat, int[] indices) {
|
||||
super(mat, indices);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getV() {
|
||||
double[] data = new double[1];
|
||||
mat.get(indices, data);
|
||||
return data[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV(Double v) {
|
||||
double[] data = new double[] { v };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple2<Double> getV2c() {
|
||||
double[] data = new double[2];
|
||||
mat.get(indices, data);
|
||||
return new Tuple2<Double>(data[0], data[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV2c(Tuple2<Double> v) {
|
||||
double[] data = new double[] { v._0, v._1 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple3<Double> getV3c() {
|
||||
double[] data = new double[3];
|
||||
mat.get(indices, data);
|
||||
return new Tuple3<Double>(data[0], data[1], data[2]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV3c(Tuple3<Double> v) {
|
||||
double[] data = new double[] { v._0, v._1, v._2 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple4<Double> getV4c() {
|
||||
double[] data = new double[4];
|
||||
mat.get(indices, data);
|
||||
return new Tuple4<Double>(data[0], data[1], data[2], data[3]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV4c(Tuple4<Double> v) {
|
||||
double[] data = new double[] { v._0, v._1, v._2, v._3 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
}
|
||||
|
||||
private static class AtableFloat extends AtableBase implements Atable<Float> {
|
||||
|
||||
public AtableFloat(Mat mat, int row, int col) {
|
||||
super(mat, row, col);
|
||||
}
|
||||
|
||||
public AtableFloat(Mat mat, int[] indices) {
|
||||
super(mat, indices);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getV() {
|
||||
float[] data = new float[1];
|
||||
mat.get(indices, data);
|
||||
return data[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV(Float v) {
|
||||
float[] data = new float[] { v };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple2<Float> getV2c() {
|
||||
float[] data = new float[2];
|
||||
mat.get(indices, data);
|
||||
return new Tuple2<Float>(data[0], data[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV2c(Tuple2<Float> v) {
|
||||
float[] data = new float[] { v._0, v._1 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple3<Float> getV3c() {
|
||||
float[] data = new float[3];
|
||||
mat.get(indices, data);
|
||||
return new Tuple3<Float>(data[0], data[1], data[2]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV3c(Tuple3<Float> v) {
|
||||
float[] data = new float[] { v._0, v._1, v._2 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple4<Float> getV4c() {
|
||||
float[] data = new float[4];
|
||||
mat.get(indices, data);
|
||||
return new Tuple4<Float>(data[0], data[1], data[2], data[3]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV4c(Tuple4<Float> v) {
|
||||
double[] data = new double[] { v._0, v._1, v._2, v._3 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
}
|
||||
|
||||
private static class AtableInteger extends AtableBase implements Atable<Integer> {
|
||||
|
||||
public AtableInteger(Mat mat, int row, int col) {
|
||||
super(mat, row, col);
|
||||
}
|
||||
|
||||
public AtableInteger(Mat mat, int[] indices) {
|
||||
super(mat, indices);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getV() {
|
||||
int[] data = new int[1];
|
||||
mat.get(indices, data);
|
||||
return data[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV(Integer v) {
|
||||
int[] data = new int[] { v };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple2<Integer> getV2c() {
|
||||
int[] data = new int[2];
|
||||
mat.get(indices, data);
|
||||
return new Tuple2<Integer>(data[0], data[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV2c(Tuple2<Integer> v) {
|
||||
int[] data = new int[] { v._0, v._1 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple3<Integer> getV3c() {
|
||||
int[] data = new int[3];
|
||||
mat.get(indices, data);
|
||||
return new Tuple3<Integer>(data[0], data[1], data[2]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV3c(Tuple3<Integer> v) {
|
||||
int[] data = new int[] { v._0, v._1, v._2 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple4<Integer> getV4c() {
|
||||
int[] data = new int[4];
|
||||
mat.get(indices, data);
|
||||
return new Tuple4<Integer>(data[0], data[1], data[2], data[3]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV4c(Tuple4<Integer> v) {
|
||||
int[] data = new int[] { v._0, v._1, v._2, v._3 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
}
|
||||
|
||||
private static class AtableShort extends AtableBase implements Atable<Short> {
|
||||
|
||||
public AtableShort(Mat mat, int row, int col) {
|
||||
super(mat, row, col);
|
||||
}
|
||||
|
||||
public AtableShort(Mat mat, int[] indices) {
|
||||
super(mat, indices);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short getV() {
|
||||
short[] data = new short[1];
|
||||
mat.get(indices, data);
|
||||
return data[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV(Short v) {
|
||||
short[] data = new short[] { v };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple2<Short> getV2c() {
|
||||
short[] data = new short[2];
|
||||
mat.get(indices, data);
|
||||
return new Tuple2<Short>(data[0], data[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV2c(Tuple2<Short> v) {
|
||||
short[] data = new short[] { v._0, v._1 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple3<Short> getV3c() {
|
||||
short[] data = new short[3];
|
||||
mat.get(indices, data);
|
||||
return new Tuple3<Short>(data[0], data[1], data[2]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV3c(Tuple3<Short> v) {
|
||||
short[] data = new short[] { v._0, v._1, v._2 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tuple4<Short> getV4c() {
|
||||
short[] data = new short[4];
|
||||
mat.get(indices, data);
|
||||
return new Tuple4<Short>(data[0], data[1], data[2], data[3]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setV4c(Tuple4<Short> v) {
|
||||
short[] data = new short[] { v._0, v._1, v._2, v._3 };
|
||||
mat.put(indices, data);
|
||||
}
|
||||
}
|
||||
|
||||
// javadoc:Mat::getNativeObjAddr()
|
||||
public long getNativeObjAddr() {
|
||||
return nativeObj;
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package org.opencv.core
|
||||
|
||||
import org.opencv.core.Mat.*
|
||||
import java.lang.RuntimeException
|
||||
|
||||
/***
|
||||
* Example use:
|
||||
*
|
||||
* val (b, g, r) = mat.at<UByte>(50, 50).v3c
|
||||
* mat.at<UByte>(50, 50).val = T3(245u, 113u, 34u)
|
||||
*
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <reified T> Mat.at(row: Int, col: Int) : Atable<T> =
|
||||
when (T::class) {
|
||||
Byte::class, Double::class, Float::class, Int::class, Short::class -> this.at(
|
||||
T::class.java,
|
||||
row,
|
||||
col
|
||||
)
|
||||
UByte::class -> AtableUByte(this, row, col) as Atable<T>
|
||||
else -> throw RuntimeException("Unsupported class type")
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <reified T> Mat.at(idx: IntArray) : Atable<T> =
|
||||
when (T::class) {
|
||||
Byte::class, Double::class, Float::class, Int::class, Short::class -> this.at(
|
||||
T::class.java,
|
||||
idx
|
||||
)
|
||||
UByte::class -> AtableUByte(this, idx) as Atable<T>
|
||||
else -> throw RuntimeException("Unsupported class type")
|
||||
}
|
||||
|
||||
class AtableUByte(val mat: Mat, val indices: IntArray): Atable<UByte> {
|
||||
|
||||
constructor(mat: Mat, row: Int, col: Int) : this(mat, intArrayOf(row, col))
|
||||
|
||||
override fun getV(): UByte {
|
||||
val data = ByteArray(1)
|
||||
mat[indices, data]
|
||||
return data[0].toUByte()
|
||||
}
|
||||
|
||||
override fun setV(v: UByte) {
|
||||
val data = byteArrayOf(v.toByte())
|
||||
mat.put(indices, data)
|
||||
}
|
||||
|
||||
override fun getV2c(): Tuple2<UByte> {
|
||||
val data = ByteArray(2)
|
||||
mat[indices, data]
|
||||
return Tuple2(data[0].toUByte(), data[1].toUByte())
|
||||
}
|
||||
|
||||
override fun setV2c(v: Tuple2<UByte>) {
|
||||
val data = byteArrayOf(v._0.toByte(), v._1.toByte())
|
||||
mat.put(indices, data)
|
||||
}
|
||||
|
||||
override fun getV3c(): Tuple3<UByte> {
|
||||
val data = ByteArray(3)
|
||||
mat[indices, data]
|
||||
return Tuple3(data[0].toUByte(), data[1].toUByte(), data[2].toUByte())
|
||||
}
|
||||
|
||||
override fun setV3c(v: Tuple3<UByte>) {
|
||||
val data = byteArrayOf(v._0.toByte(), v._1.toByte(), v._2.toByte())
|
||||
mat.put(indices, data)
|
||||
}
|
||||
|
||||
override fun getV4c(): Tuple4<UByte> {
|
||||
val data = ByteArray(4)
|
||||
mat[indices, data]
|
||||
return Tuple4(data[0].toUByte(), data[1].toUByte(), data[2].toUByte(), data[3].toUByte())
|
||||
}
|
||||
|
||||
override fun setV4c(v: Tuple4<UByte>) {
|
||||
val data = byteArrayOf(v._0.toByte(), v._1.toByte(), v._2.toByte(), v._3.toByte())
|
||||
mat.put(indices, data)
|
||||
}
|
||||
}
|
||||
|
||||
operator fun <T> Tuple2<T>.component1(): T = this._0
|
||||
operator fun <T> Tuple2<T>.component2(): T = this._1
|
||||
|
||||
operator fun <T> Tuple3<T>.component1(): T = this._0
|
||||
operator fun <T> Tuple3<T>.component2(): T = this._1
|
||||
operator fun <T> Tuple3<T>.component3(): T = this._2
|
||||
|
||||
operator fun <T> Tuple4<T>.component1(): T = this._0
|
||||
operator fun <T> Tuple4<T>.component2(): T = this._1
|
||||
operator fun <T> Tuple4<T>.component3(): T = this._2
|
||||
operator fun <T> Tuple4<T>.component4(): T = this._3
|
||||
|
||||
fun <T> T2(_0: T, _1: T) : Tuple2<T> = Tuple2(_0, _1)
|
||||
fun <T> T3(_0: T, _1: T, _2: T) : Tuple3<T> = Tuple3(_0, _1, _2)
|
||||
fun <T> T4(_0: T, _1: T, _2: T, _3: T) : Tuple4<T> = Tuple4(_0, _1, _2, _3)
|
||||
@@ -1285,4 +1285,31 @@ public class MatTest extends OpenCVTestCase {
|
||||
assertEquals(5, bbuf.get(63*80 + 63));
|
||||
}
|
||||
|
||||
public void testMatAt() {
|
||||
Mat uc1 = new Mat(2, 3, CvType.CV_8S) {
|
||||
{
|
||||
put(0, 0, 1, 2, 3);
|
||||
put(1, 0, 4, 5, 6);
|
||||
}
|
||||
};
|
||||
assertEquals((byte)1, uc1.at(Byte.class, 0, 0).getV().byteValue());
|
||||
assertEquals((byte)2, uc1.at(Byte.class, 0, 1).getV().byteValue());
|
||||
assertEquals((byte)3, uc1.at(Byte.class, 0, 2).getV().byteValue());
|
||||
assertEquals((byte)4, uc1.at(Byte.class, 1, 0).getV().byteValue());
|
||||
assertEquals((byte)5, uc1.at(Byte.class, 1, 1).getV().byteValue());
|
||||
assertEquals((byte)6, uc1.at(Byte.class, 1, 2).getV().byteValue());
|
||||
uc1.at(Byte.class, 0, 0).setV((byte)7);
|
||||
uc1.at(Byte.class, 0, 1).setV((byte)8);
|
||||
uc1.at(Byte.class, 0, 2).setV((byte)9);
|
||||
uc1.at(Byte.class, 1, 0).setV((byte)10);
|
||||
uc1.at(Byte.class, 1, 1).setV((byte)11);
|
||||
uc1.at(Byte.class, 1, 2).setV((byte)12);
|
||||
byte[] data = new byte[6];
|
||||
uc1.get(0, 0, data);
|
||||
assertArrayEquals(data, new byte[] {7, 8, 9, 10, 11, 12});
|
||||
Mat.Tuple3<Byte> bgr = rgbLena.at(Byte.class, 0, 0).getV3c();
|
||||
assertEquals(bgr.get_0().byteValue(), (byte)128);
|
||||
assertEquals(bgr.get_1().byteValue(), (byte)138);
|
||||
assertEquals(bgr.get_2().byteValue(), (byte)225);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ func throwIncompatibleBufferSize(count: Int, channels: Int32) throws {
|
||||
)
|
||||
}
|
||||
|
||||
public typealias T2<T> = (T, T)
|
||||
public typealias T3<T> = (T, T, T)
|
||||
public typealias T4<T> = (T, T, T, T)
|
||||
|
||||
public extension Mat {
|
||||
|
||||
convenience init(rows:Int32, cols:Int32, type:Int32, data:[Int8]) {
|
||||
@@ -242,3 +246,350 @@ public extension Mat {
|
||||
return __get(indices as [NSNumber]) as! [Double]
|
||||
}
|
||||
}
|
||||
|
||||
public protocol Atable {
|
||||
static func getAt(m: Mat, indices:[Int32]) -> Self
|
||||
static func putAt(m: Mat, indices:[Int32], v: Self)
|
||||
static func getAt2c(m: Mat, indices:[Int32]) -> (Self, Self)
|
||||
static func putAt2c(m: Mat, indices:[Int32], v: (Self, Self))
|
||||
static func getAt3c(m: Mat, indices:[Int32]) -> (Self, Self, Self)
|
||||
static func putAt3c(m: Mat, indices:[Int32], v: (Self, Self, Self))
|
||||
static func getAt4c(m: Mat, indices:[Int32]) -> (Self, Self, Self, Self)
|
||||
static func putAt4c(m: Mat, indices:[Int32], v: (Self, Self, Self, Self))
|
||||
}
|
||||
|
||||
public class MatAt<N: Atable> {
|
||||
|
||||
init(mat: Mat, indices: [Int32]) {
|
||||
self.mat = mat
|
||||
self.indices = indices
|
||||
}
|
||||
|
||||
private let mat: Mat
|
||||
private let indices: [Int32]
|
||||
public var v: N {
|
||||
get {
|
||||
return N.getAt(m: mat, indices: indices)
|
||||
}
|
||||
set(value) {
|
||||
N.putAt(m: mat, indices: indices, v: value)
|
||||
}
|
||||
}
|
||||
public var v2c: (N, N) {
|
||||
get {
|
||||
return N.getAt2c(m: mat, indices: indices)
|
||||
}
|
||||
set(value) {
|
||||
N.putAt2c(m: mat, indices: indices, v: value)
|
||||
}
|
||||
}
|
||||
public var v3c: (N, N, N) {
|
||||
get {
|
||||
return N.getAt3c(m: mat, indices: indices)
|
||||
}
|
||||
set(value) {
|
||||
N.putAt3c(m: mat, indices: indices, v: value)
|
||||
}
|
||||
}
|
||||
public var v4c: (N, N, N, N) {
|
||||
get {
|
||||
return N.getAt4c(m: mat, indices: indices)
|
||||
}
|
||||
set(value) {
|
||||
N.putAt4c(m: mat, indices: indices, v: value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UInt8: Atable {
|
||||
public static func getAt(m: Mat, indices:[Int32]) -> UInt8 {
|
||||
var tmp = [Int8](repeating: 0, count: 1)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return UInt8(bitPattern: tmp[0])
|
||||
}
|
||||
|
||||
public static func putAt(m: Mat, indices: [Int32], v: UInt8) {
|
||||
let tmp = [Int8(bitPattern: v)]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt2c(m: Mat, indices:[Int32]) -> (UInt8, UInt8) {
|
||||
var tmp = [Int8](repeating: 0, count: 2)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (UInt8(bitPattern: tmp[0]), UInt8(bitPattern: tmp[1]))
|
||||
}
|
||||
|
||||
public static func putAt2c(m: Mat, indices: [Int32], v: (UInt8, UInt8)) {
|
||||
let tmp = [Int8(bitPattern: v.0), Int8(bitPattern: v.1)]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt3c(m: Mat, indices:[Int32]) -> (UInt8, UInt8, UInt8) {
|
||||
var tmp = [Int8](repeating: 0, count: 3)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (UInt8(bitPattern: tmp[0]), UInt8(bitPattern: tmp[1]), UInt8(bitPattern: tmp[2]))
|
||||
}
|
||||
|
||||
public static func putAt3c(m: Mat, indices: [Int32], v: (UInt8, UInt8, UInt8)) {
|
||||
let tmp = [Int8(bitPattern: v.0), Int8(bitPattern: v.1), Int8(bitPattern: v.2)]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt4c(m: Mat, indices:[Int32]) -> (UInt8, UInt8, UInt8, UInt8) {
|
||||
var tmp = [Int8](repeating: 0, count: 4)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (UInt8(bitPattern: tmp[0]), UInt8(bitPattern: tmp[1]), UInt8(bitPattern: tmp[2]), UInt8(bitPattern: tmp[3]))
|
||||
}
|
||||
|
||||
public static func putAt4c(m: Mat, indices: [Int32], v: (UInt8, UInt8, UInt8, UInt8)) {
|
||||
let tmp = [Int8(bitPattern: v.0), Int8(bitPattern: v.1), Int8(bitPattern: v.2), Int8(bitPattern: v.3)]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
}
|
||||
|
||||
extension Int8: Atable {
|
||||
public static func getAt(m: Mat, indices:[Int32]) -> Int8 {
|
||||
var tmp = [Int8](repeating: 0, count: 1)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return tmp[0]
|
||||
}
|
||||
|
||||
public static func putAt(m: Mat, indices: [Int32], v: Int8) {
|
||||
let tmp = [v]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt2c(m: Mat, indices:[Int32]) -> (Int8, Int8) {
|
||||
var tmp = [Int8](repeating: 0, count: 2)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1])
|
||||
}
|
||||
|
||||
public static func putAt2c(m: Mat, indices: [Int32], v: (Int8, Int8)) {
|
||||
let tmp = [v.0, v.1]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt3c(m: Mat, indices:[Int32]) -> (Int8, Int8, Int8) {
|
||||
var tmp = [Int8](repeating: 0, count: 3)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2])
|
||||
}
|
||||
|
||||
public static func putAt3c(m: Mat, indices: [Int32], v: (Int8, Int8, Int8)) {
|
||||
let tmp = [v.0, v.1, v.2]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt4c(m: Mat, indices:[Int32]) -> (Int8, Int8, Int8, Int8) {
|
||||
var tmp = [Int8](repeating: 0, count: 4)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2], tmp[3])
|
||||
}
|
||||
|
||||
public static func putAt4c(m: Mat, indices: [Int32], v: (Int8, Int8, Int8, Int8)) {
|
||||
let tmp = [v.0, v.1, v.2, v.3]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
}
|
||||
|
||||
extension Double: Atable {
|
||||
public static func getAt(m: Mat, indices:[Int32]) -> Double {
|
||||
var tmp = [Double](repeating: 0, count: 1)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return tmp[0]
|
||||
}
|
||||
|
||||
public static func putAt(m: Mat, indices: [Int32], v: Double) {
|
||||
let tmp = [v]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt2c(m: Mat, indices:[Int32]) -> (Double, Double) {
|
||||
var tmp = [Double](repeating: 0, count: 2)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1])
|
||||
}
|
||||
|
||||
public static func putAt2c(m: Mat, indices: [Int32], v: (Double, Double)) {
|
||||
let tmp = [v.0, v.1]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt3c(m: Mat, indices:[Int32]) -> (Double, Double, Double) {
|
||||
var tmp = [Double](repeating: 0, count: 3)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2])
|
||||
}
|
||||
|
||||
public static func putAt3c(m: Mat, indices: [Int32], v: (Double, Double, Double)) {
|
||||
let tmp = [v.0, v.1, v.2]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt4c(m: Mat, indices:[Int32]) -> (Double, Double, Double, Double) {
|
||||
var tmp = [Double](repeating: 0, count: 4)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2], tmp[3])
|
||||
}
|
||||
|
||||
public static func putAt4c(m: Mat, indices: [Int32], v: (Double, Double, Double, Double)) {
|
||||
let tmp = [v.0, v.1, v.2, v.3]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
}
|
||||
|
||||
extension Float: Atable {
|
||||
public static func getAt(m: Mat, indices:[Int32]) -> Float {
|
||||
var tmp = [Float](repeating: 0, count: 1)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return tmp[0]
|
||||
}
|
||||
|
||||
public static func putAt(m: Mat, indices: [Int32], v: Float) {
|
||||
let tmp = [v]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt2c(m: Mat, indices:[Int32]) -> (Float, Float) {
|
||||
var tmp = [Float](repeating: 0, count: 2)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1])
|
||||
}
|
||||
|
||||
public static func putAt2c(m: Mat, indices: [Int32], v: (Float, Float)) {
|
||||
let tmp = [v.0, v.1]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt3c(m: Mat, indices:[Int32]) -> (Float, Float, Float) {
|
||||
var tmp = [Float](repeating: 0, count: 3)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2])
|
||||
}
|
||||
|
||||
public static func putAt3c(m: Mat, indices: [Int32], v: (Float, Float, Float)) {
|
||||
let tmp = [v.0, v.1, v.2]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt4c(m: Mat, indices:[Int32]) -> (Float, Float, Float, Float) {
|
||||
var tmp = [Float](repeating: 0, count: 4)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2], tmp[3])
|
||||
}
|
||||
|
||||
public static func putAt4c(m: Mat, indices: [Int32], v: (Float, Float, Float, Float)) {
|
||||
let tmp = [v.0, v.1, v.2, v.3]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
}
|
||||
|
||||
extension Int32: Atable {
|
||||
public static func getAt(m: Mat, indices:[Int32]) -> Int32 {
|
||||
var tmp = [Int32](repeating: 0, count: 1)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return tmp[0]
|
||||
}
|
||||
|
||||
public static func putAt(m: Mat, indices: [Int32], v: Int32) {
|
||||
let tmp = [v]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt2c(m: Mat, indices:[Int32]) -> (Int32, Int32) {
|
||||
var tmp = [Int32](repeating: 0, count: 2)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1])
|
||||
}
|
||||
|
||||
public static func putAt2c(m: Mat, indices: [Int32], v: (Int32, Int32)) {
|
||||
let tmp = [v.0, v.1]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt3c(m: Mat, indices:[Int32]) -> (Int32, Int32, Int32) {
|
||||
var tmp = [Int32](repeating: 0, count: 3)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2])
|
||||
}
|
||||
|
||||
public static func putAt3c(m: Mat, indices: [Int32], v: (Int32, Int32, Int32)) {
|
||||
let tmp = [v.0, v.1, v.2]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt4c(m: Mat, indices:[Int32]) -> (Int32, Int32, Int32, Int32) {
|
||||
var tmp = [Int32](repeating: 0, count: 4)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2], tmp[3])
|
||||
}
|
||||
|
||||
public static func putAt4c(m: Mat, indices: [Int32], v: (Int32, Int32, Int32, Int32)) {
|
||||
let tmp = [v.0, v.1, v.2, v.3]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
}
|
||||
|
||||
extension Int16: Atable {
|
||||
public static func getAt(m: Mat, indices:[Int32]) -> Int16 {
|
||||
var tmp = [Int16](repeating: 0, count: 1)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return tmp[0]
|
||||
}
|
||||
|
||||
public static func putAt(m: Mat, indices: [Int32], v: Int16) {
|
||||
let tmp = [v]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt2c(m: Mat, indices:[Int32]) -> (Int16, Int16) {
|
||||
var tmp = [Int16](repeating: 0, count: 2)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1])
|
||||
}
|
||||
|
||||
public static func putAt2c(m: Mat, indices: [Int32], v: (Int16, Int16)) {
|
||||
let tmp = [v.0, v.1]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt3c(m: Mat, indices:[Int32]) -> (Int16, Int16, Int16) {
|
||||
var tmp = [Int16](repeating: 0, count: 3)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2])
|
||||
}
|
||||
|
||||
public static func putAt3c(m: Mat, indices: [Int32], v: (Int16, Int16, Int16)) {
|
||||
let tmp = [v.0, v.1, v.2]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
|
||||
public static func getAt4c(m: Mat, indices:[Int32]) -> (Int16, Int16, Int16, Int16) {
|
||||
var tmp = [Int16](repeating: 0, count: 4)
|
||||
try! m.get(indices: indices, data: &tmp)
|
||||
return (tmp[0], tmp[1], tmp[2], tmp[3])
|
||||
}
|
||||
|
||||
public static func putAt4c(m: Mat, indices: [Int32], v: (Int16, Int16, Int16, Int16)) {
|
||||
let tmp = [v.0, v.1, v.2, v.3]
|
||||
try! m.put(indices: indices, data: tmp)
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* Example use:
|
||||
*
|
||||
* let elemantVal: UInt8 = mat.at(row: 50, col: 50).v
|
||||
* mat.at(row: 50, col: 50).v = 245
|
||||
*
|
||||
*/
|
||||
public extension Mat {
|
||||
func at<N: Atable>(row: Int32, col: Int32) -> MatAt<N> {
|
||||
return MatAt(mat: self, indices: [row, col])
|
||||
}
|
||||
|
||||
func at<N: Atable>(indices:[Int32]) -> MatAt<N> {
|
||||
return MatAt(mat: self, indices: indices)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1143,4 +1143,28 @@ class MatTests: OpenCVTestCase {
|
||||
XCTAssertEqual(5, bufferOut[63*80 + 63])
|
||||
}
|
||||
|
||||
func testMatAt() {
|
||||
let uc1 = Mat(rows: 2, cols: 3, type: CvType.CV_8U)
|
||||
try! uc1.put(row: 0, col: 0, data: [1, 2, 3, 4, 5, 6] as [Int8])
|
||||
XCTAssertEqual(UInt8(1), uc1.at(row: 0, col: 0).v)
|
||||
XCTAssertEqual(UInt8(2), uc1.at(row: 0, col: 1).v)
|
||||
XCTAssertEqual(UInt8(3), uc1.at(row: 0, col: 2).v)
|
||||
XCTAssertEqual(UInt8(4), uc1.at(row: 1, col: 0).v)
|
||||
XCTAssertEqual(UInt8(5), uc1.at(row: 1, col: 1).v)
|
||||
XCTAssertEqual(UInt8(6), uc1.at(row: 1, col: 2).v)
|
||||
uc1.at(row: 0, col: 0).v = UInt8(7)
|
||||
uc1.at(row: 0, col: 1).v = UInt8(8)
|
||||
uc1.at(row: 0, col: 2).v = UInt8(9)
|
||||
uc1.at(row: 1, col: 0).v = UInt8(10)
|
||||
uc1.at(row: 1, col: 1).v = UInt8(11)
|
||||
uc1.at(row: 1, col: 2).v = UInt8(12)
|
||||
var data = [Int8](repeating: 0, count: 6)
|
||||
try! uc1.get(row: 0, col: 0, data: &data)
|
||||
XCTAssertEqual(data, [7, 8, 9, 10, 11, 12] as [Int8])
|
||||
let (b, g, r): T3<UInt8> = rgbLena.at(row: 0, col: 0).v3c
|
||||
XCTAssertEqual(b, UInt8(128))
|
||||
XCTAssertEqual(g, UInt8(138))
|
||||
XCTAssertEqual(r, UInt8(225))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user