Java API: fixing Mat::get() for non-continuous Mat-s.
This commit is contained in:
parent
9213bba48a
commit
3c0e9d12f7
@ -1,5 +1,7 @@
|
|||||||
package org.opencv.test.core;
|
package org.opencv.test.core;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.opencv.core.Core;
|
import org.opencv.core.Core;
|
||||||
import org.opencv.core.CvException;
|
import org.opencv.core.CvException;
|
||||||
import org.opencv.core.CvType;
|
import org.opencv.core.CvType;
|
||||||
@ -11,8 +13,6 @@ import org.opencv.core.Scalar;
|
|||||||
import org.opencv.core.Size;
|
import org.opencv.core.Size;
|
||||||
import org.opencv.test.OpenCVTestCase;
|
import org.opencv.test.OpenCVTestCase;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class MatTest extends OpenCVTestCase {
|
public class MatTest extends OpenCVTestCase {
|
||||||
|
|
||||||
public void testAdjustROI() {
|
public void testAdjustROI() {
|
||||||
@ -274,19 +274,21 @@ public class MatTest extends OpenCVTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testGetIntInt() {
|
public void testGetIntInt() {
|
||||||
Mat src = new Mat(3, 3, CvType.CV_8U, new Scalar(2));
|
Mat src = new Mat(3, 3, CvType.CV_8UC2, new Scalar(2, 5));
|
||||||
double[] actualArray = src.get(1, 1);
|
double[] actualArray = src.get(1, 1);
|
||||||
|
|
||||||
assertTrue(Arrays.equals(new double[] { 2 }, actualArray));
|
assertTrue(Arrays.equals(new double[] { 2, 5 }, actualArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetIntIntByteArray() {
|
public void testGetIntIntByteArray() {
|
||||||
Mat m = new Mat(5, 5, CvType.CV_8UC3, new Scalar(1, 2, 3));
|
Mat m = new Mat(5, 5, CvType.CV_8UC3, new Scalar(1, 2, 3));
|
||||||
byte[] goodData = new byte[9];
|
byte[] goodData = new byte[9];
|
||||||
byte[] badData = new byte[7];
|
byte[] badData = new byte[7];
|
||||||
m.get(1, 1, goodData);
|
|
||||||
|
int bytesNum = m.get(1, 1, goodData);
|
||||||
|
|
||||||
assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 }, goodData));
|
assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 }, goodData));
|
||||||
|
assertEquals(9, bytesNum);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
m.get(2, 2, badData);
|
m.get(2, 2, badData);
|
||||||
@ -321,11 +323,11 @@ public class MatTest extends OpenCVTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testGetIntIntShortArray() {
|
public void testGetIntIntShortArray() {
|
||||||
Mat src = new Mat(2, 2, CvType.CV_16U);
|
Mat src = new Mat(2, 3, CvType.CV_16U, new Scalar(11));
|
||||||
short[] data = { 3, 1, 4, 7 };
|
short[] data = { 3, 1, 4, 7 };
|
||||||
|
|
||||||
int numOfBytes = src.get(1, 1, data);
|
int numOfBytes = src.get(1, 0, data);
|
||||||
assertEquals(2, numOfBytes);
|
assertEquals(6, numOfBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetNativeObjAddr() {
|
public void testGetNativeObjAddr() {
|
||||||
@ -528,10 +530,17 @@ public class MatTest extends OpenCVTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testPutIntIntByteArray() {
|
public void testPutIntIntByteArray() {
|
||||||
Mat m = new Mat(5, 5, CvType.CV_8UC3);
|
Mat m = new Mat(5, 5, CvType.CV_8UC3, new Scalar(1, 2, 3));
|
||||||
byte[] bytes = new byte[] { 10, 20, 30, 40, 50, 60 };
|
byte[] bytes = new byte[] { 10, 20, 30, 40, 50, 60 };
|
||||||
m.put(1, 1, bytes);
|
|
||||||
|
int bytesNum = m.put(1, 2, bytes);
|
||||||
|
|
||||||
|
assertEquals(6, bytesNum);
|
||||||
|
byte buff[] = new byte[3];
|
||||||
|
Mat m1 = m.row(1);
|
||||||
|
bytesNum = m1.get(0, 3, buff);
|
||||||
|
assertEquals(3, bytesNum);
|
||||||
|
assertTrue(Arrays.equals(new byte[]{40, 50, 60}, buff));
|
||||||
try {
|
try {
|
||||||
byte[] bytes2 = new byte[] { 10, 20, 30, 40, 50 };
|
byte[] bytes2 = new byte[] { 10, 20, 30, 40, 50 };
|
||||||
m.put(2, 2, bytes2);
|
m.put(2, 2, bytes2);
|
||||||
@ -542,9 +551,18 @@ public class MatTest extends OpenCVTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testPutIntIntDoubleArray() {
|
public void testPutIntIntDoubleArray() {
|
||||||
Mat m = new Mat(5, 5, CvType.CV_8UC3);
|
Mat m = new Mat(5, 5, CvType.CV_8UC3, new Scalar(1, 2, 3));
|
||||||
m.put(1, 1, 10, 20, 30, 40, 50, 60);
|
|
||||||
|
int bytesNum = m.put(2, 1, 10, 20, 30, 40, 50, 60);
|
||||||
|
|
||||||
|
assertEquals(6, bytesNum);
|
||||||
|
Mat m1 = m.row(2);
|
||||||
|
byte buff[] = new byte[3];
|
||||||
|
bytesNum = m1.get(0, 2, buff);
|
||||||
|
assertEquals(3, bytesNum);
|
||||||
|
assertTrue(Arrays.equals(new byte[]{40, 50, 60}, buff));
|
||||||
|
assertArrayEquals(new double[]{10, 20, 30}, m.get(2, 1), EPS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
m.put(2, 2, 11, 22, 33, 44, 55);
|
m.put(2, 2, 11, 22, 33, 44, 55);
|
||||||
fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
|
fail("Expected UnsupportedOperationException (data.length % CvType.channels(t) != 0)");
|
||||||
@ -554,9 +572,18 @@ public class MatTest extends OpenCVTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testPutIntIntFloatArray() {
|
public void testPutIntIntFloatArray() {
|
||||||
Mat m = new Mat(5, 5, CvType.CV_32FC3);
|
Mat m = new Mat(5, 5, CvType.CV_32FC3, new Scalar(1, 2, 3));
|
||||||
float[] elements = new float[] { 10, 20, 30, 40, 50, 60 };
|
float[] elements = new float[] { 10, 20, 30, 40, 50, 60 };
|
||||||
m.put(1, 1, elements);
|
|
||||||
|
int bytesNum = m.put(4, 3, elements);
|
||||||
|
|
||||||
|
assertEquals(elements.length * 4, bytesNum);
|
||||||
|
Mat m1 = m.row(4);
|
||||||
|
float buff[] = new float[3];
|
||||||
|
bytesNum = m1.get(0, 4, buff);
|
||||||
|
assertEquals(buff.length * 4, bytesNum);
|
||||||
|
assertTrue(Arrays.equals(new float[]{40, 50, 60}, buff));
|
||||||
|
assertArrayEquals(new double[]{10, 20, 30}, m.get(4, 3), EPS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
float[] elements2 = new float[] { 10, 20, 30, 40, 50 };
|
float[] elements2 = new float[] { 10, 20, 30, 40, 50 };
|
||||||
@ -568,9 +595,18 @@ public class MatTest extends OpenCVTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testPutIntIntIntArray() {
|
public void testPutIntIntIntArray() {
|
||||||
Mat m = new Mat(5, 5, CvType.CV_32SC3);
|
Mat m = new Mat(5, 5, CvType.CV_32SC3, new Scalar(-1, -2, -3));
|
||||||
int[] elements = new int[] { 10, 20, 30, 40, 50, 60 };
|
int[] elements = new int[] { 10, 20, 30, 40, 50, 60 };
|
||||||
m.put(1, 1, elements);
|
|
||||||
|
int bytesNum = m.put(0, 4, elements);
|
||||||
|
|
||||||
|
assertEquals(elements.length * 4, bytesNum);
|
||||||
|
Mat m1 = m.col(4);
|
||||||
|
int buff[] = new int[3];
|
||||||
|
bytesNum = m1.get(0, 0, buff);
|
||||||
|
assertEquals(buff.length * 4, bytesNum);
|
||||||
|
assertTrue(Arrays.equals(new int[]{10, 20, 30}, buff));
|
||||||
|
assertArrayEquals(new double[]{40, 50, 60}, m.get(1, 0), EPS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int[] elements2 = new int[] { 10, 20, 30, 40, 50 };
|
int[] elements2 = new int[] { 10, 20, 30, 40, 50 };
|
||||||
@ -582,9 +618,17 @@ public class MatTest extends OpenCVTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testPutIntIntShortArray() {
|
public void testPutIntIntShortArray() {
|
||||||
Mat m = new Mat(5, 5, CvType.CV_16SC3);
|
Mat m = new Mat(5, 5, CvType.CV_16SC3, new Scalar(-1, -2, -3));
|
||||||
short[] elements = new short[] { 10, 20, 30, 40, 50, 60 };
|
short[] elements = new short[] { 10, 20, 30, 40, 50, 60 };
|
||||||
m.put(1, 1, elements);
|
|
||||||
|
int bytesNum = m.put(2, 3, elements);
|
||||||
|
|
||||||
|
assertEquals(elements.length * 2, bytesNum);
|
||||||
|
Mat m1 = m.col(3);
|
||||||
|
short buff[] = new short[3];
|
||||||
|
bytesNum = m1.get(2, 0, buff);
|
||||||
|
assertTrue(Arrays.equals(new short[]{10, 20, 30}, buff));
|
||||||
|
assertArrayEquals(new double[]{40, 50, 60}, m.get(2, 4), EPS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
short[] elements2 = new short[] { 10, 20, 30, 40, 50 };
|
short[] elements2 = new short[] { 10, 20, 30, 40, 50 };
|
||||||
|
@ -2070,26 +2070,26 @@ template<typename T> int mat_get(cv::Mat* m, int row, int col, int count, char*
|
|||||||
if(! m) return 0;
|
if(! m) return 0;
|
||||||
if(! buff) return 0;
|
if(! buff) return 0;
|
||||||
|
|
||||||
count *= sizeof(T);
|
int bytesToCopy = count * sizeof(T);
|
||||||
int rest = ((m->rows - row) * m->cols - col) * m->channels() * sizeof(T);
|
int bytesRestInMat = ((m->rows - row) * m->cols - col) * m->elemSize();
|
||||||
if(count>rest) count = rest;
|
if(bytesToCopy > bytesRestInMat) bytesToCopy = bytesRestInMat;
|
||||||
int res = count;
|
int res = bytesToCopy;
|
||||||
|
|
||||||
if( m->isContinuous() )
|
if( m->isContinuous() )
|
||||||
{
|
{
|
||||||
memcpy(buff, m->ptr(row, col), count);
|
memcpy(buff, m->ptr(row, col), bytesToCopy);
|
||||||
} else {
|
} else {
|
||||||
// row by row
|
// row by row
|
||||||
int num = (m->cols - col - 1) * m->channels() * sizeof(T); // 1st partial row
|
int bytesInRow = (m->cols - col) * m->elemSize(); // 1st partial row
|
||||||
if(count<num) num = count;
|
while(bytesToCopy > 0)
|
||||||
uchar* data = m->ptr(row++, col);
|
{
|
||||||
while(count>0){//TODO: recheck this cycle for the case col!=0
|
int len = std::min(bytesToCopy, bytesInRow);
|
||||||
memcpy(buff, data, num);
|
memcpy(buff, m->ptr(row, col), len);
|
||||||
count -= num;
|
bytesToCopy -= len;
|
||||||
buff += num;
|
buff += len;
|
||||||
num = m->cols * m->channels() * sizeof(T);
|
row++;
|
||||||
if(count<num) num = count;
|
col = 0;
|
||||||
data = m->ptr(row++, 0);
|
bytesInRow = m->cols * m->elemSize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user