Commit 954f3c1e authored by Andrey Kamaev's avatar Andrey Kamaev

Java: fixed Mats comparison; added resahpe mathod to core.Mat; fixed test resources extraction.

parent 3f508941
......@@ -915,7 +915,7 @@ Reprojects a disparity image to 3D space.
.. ocv:cfunction:: void cvReprojectImageTo3D( const CvArr* disparity, CvArr* _3dImage, const CvMat* Q, int handleMissingValues=0)
.. ocv:pyoldfunction:: cv.ReprojectImageTo3D(disparity, _3dImage, Q, handleMissingValues=0) -> None
:param disparity: Input single-channel 16-bit signed or 32-bit floating-point disparity image.
:param disparity: Input single-channel 8-bit unsigned, 16-bit signed, 32-bit signed or 32-bit floating-point disparity image.
:param _3dImage: Output 3-channel floating-point image of the same size as ``disparity`` . Each element of ``_3dImage(x,y)`` contains 3D coordinates of the point ``(x,y)`` computed from the disparity map.
......
......@@ -115,6 +115,44 @@ public class OpenCVTestCase extends TestCase {
v1.put(0, 0, 1.0, 3.0, 2.0);
v2 = new Mat(1, 3, CvType.CV_32F);
v2.put(0, 0, 2.0, 1.0, 3.0);
low.release();
high.release();
}
@Override
protected void tearDown() throws Exception {
gray0.release();
gray1.release();
gray2.release();
gray3.release();
gray9.release();
gray127.release();
gray128.release();
gray255.release();
gray_16u_256.release();
gray_16s_1024.release();
grayRnd.release();
gray0_32f.release();
gray1_32f.release();
gray3_32f.release();
gray9_32f.release();
gray255_32f.release();
grayE_32f.release();
grayE_32f.release();
grayRnd_32f.release();
gray0_32f_1d.release();
gray0_64f.release();
gray0_64f_1d.release();
rgba0.release();
rgba128.release();
rgbLena.release();
grayChess.release();
v1.release();
v2.release();
super.tearDown();
}
public static void assertMatEqual(Mat m1, Mat m2) {
......@@ -133,30 +171,31 @@ public class OpenCVTestCase extends TestCase {
compareMats(expected, actual, eps, false);
}
static private void compareMats(Mat m1, Mat m2, boolean isEqualityMeasured) {
// OpenCVTestRunner.Log(m1.toString());
// OpenCVTestRunner.Log(m2.toString());
if (m1.type() != m2.type() || m1.cols() != m2.cols()
|| m1.rows() != m2.rows()) {
static private void compareMats(Mat expected, Mat actual, boolean isEqualityMeasured) {
if (expected.type() != actual.type() || expected.cols() != actual.cols()
|| expected.rows() != actual.rows()) {
throw new UnsupportedOperationException();
} else if (m1.channels() == 1) {
if (isEqualityMeasured) {
assertTrue(CalcPercentageOfDifference(m1, m2) == 0.0);
} else {
assertTrue(CalcPercentageOfDifference(m1, m2) != 0.0);
}
} else {
for (int coi = 0; coi < m1.channels(); coi++) {
Mat m1c = getCOI(m1, coi);
Mat m2c = getCOI(m2, coi);
if (isEqualityMeasured) {
assertTrue(CalcPercentageOfDifference(m1c, m2c) == 0.0);
} else {
assertTrue(CalcPercentageOfDifference(m1c, m2c) != 0.0);
}
}
if (expected.depth() == CvType.CV_32F || expected.depth() == CvType.CV_64F){
if (isEqualityMeasured)
throw new UnsupportedOperationException("Floating-point Mats must not be checked for exact match. Use assertMatEqual(Mat expected, Mat actual, double eps) instead.");
else
throw new UnsupportedOperationException("Floating-point Mats must not be checked for exact match. Use assertMatNotEqual(Mat expected, Mat actual, double eps) instead.");
}
Mat diff = new Mat();
Core.absdiff(expected, actual, diff);
Mat reshaped = diff.reshape(1);
int mistakes = Core.countNonZero(reshaped);
reshaped.release();
diff.release();
if(isEqualityMeasured)
assertTrue("Mats are different in " + mistakes + " points", 0 == mistakes);
else
assertFalse("Mats are equal", 0 == mistakes);
}
static private void compareMats(Mat expected, Mat actual, double eps, boolean isEqualityMeasured) {
......@@ -167,34 +206,13 @@ public class OpenCVTestCase extends TestCase {
Mat diff = new Mat();
Core.absdiff(expected, actual, diff);
if(isEqualityMeasured)
assertTrue("Max difference between expected and actiual values is bigger than " + eps,
assertTrue("Max difference between expected and actiual Mats is bigger than " + eps,
Core.checkRange(diff, true, new Point(), 0.0, eps));
else
assertFalse("Max difference between expected and actiual values is less than " + eps,
assertFalse("Max difference between expected and actiual Mats is less than " + eps,
Core.checkRange(diff, true, new Point(), 0.0, eps));
}
static private Mat getCOI(Mat m, int coi) {
Mat ch = new Mat(m.rows(), m.cols(), m.depth());
for (int i = 0; i < m.rows(); i++)
for (int j = 0; j < m.cols(); j++) {
double pixel[] = m.get(i, j);
ch.put(i, j, pixel[coi]);
}
return ch;
}
static private double CalcPercentageOfDifference(Mat m1, Mat m2) {
Mat cmp = new Mat(0, 0, CvType.CV_8U);
Core.compare(m1, m2, cmp, Core.CMP_EQ);
double difference = 100.0 * (1.0 - Double.valueOf(Core
.countNonZero(cmp)) / Double.valueOf(cmp.rows() * cmp.cols()));
return difference;
}
public void test_1(String label) {
OpenCVTestRunner
.Log("================================================");
......
package org.opencv.test;
import java.io.FileOutputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.test.AndroidTestRunner;
import android.test.InstrumentationTestRunner;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* This only class is Android specific.
* The original idea about test order randomization is from marek.defecinski blog.
* This only class is Android specific. The original idea about test order
* randomization is from marek.defecinski blog.
*
* @see <a href="http://opencv.itseez.com">OpenCV</a>
*/
public class OpenCVTestRunner extends InstrumentationTestRunner {
public static String LENA_PATH = "/data/data/org.opencv.test/files/lena.jpg";
public static String CHESS_PATH = "/data/data/org.opencv.test/files/chessboard.jpg";
public static String LBPCASCADE_FRONTALFACE_PATH = "/mnt/sdcard/lbpcascade_frontalface.xml";
public static String LENA_PATH;
public static String CHESS_PATH;
public static String LBPCASCADE_FRONTALFACE_PATH;
private AndroidTestRunner androidTestRunner;
private static String TAG = "opencv_test_java";
......@@ -32,13 +32,12 @@ public class OpenCVTestRunner extends InstrumentationTestRunner {
@Override
public void onStart() {
ExportResourceImage("lena.jpg", R.drawable.lena);
ExportResourceImage("chessboard.jpg", R.drawable.chessboard);
LENA_PATH = ExportResource(R.drawable.lena);
CHESS_PATH = ExportResource(R.drawable.chessboard);
LBPCASCADE_FRONTALFACE_PATH = ExportResource(R.raw.lbpcascade_frontalface);
//FIXME: implement export of the cascade
//List<TestCase> testCases = androidTestRunner.getTestCases();
//Collections.shuffle(testCases); //shuffle the tests order
// List<TestCase> testCases = androidTestRunner.getTestCases();
// Collections.shuffle(testCases); //shuffle the tests order
super.onStart();
}
......@@ -49,16 +48,31 @@ public class OpenCVTestRunner extends InstrumentationTestRunner {
return androidTestRunner;
}
private void ExportResourceImage(String image, int rId) {
private String ExportResource(int resourceId) {
String fullname = getContext().getResources().getString(resourceId);
String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
try {
Bitmap mBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), rId);
FileOutputStream fos = this.getContext().openFileOutput(image, Context.MODE_WORLD_READABLE);
mBitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
InputStream is = getContext().getResources().openRawResource(
resourceId);
File resDir = getContext().getDir("testdata", Context.MODE_PRIVATE);
File resFile = new File(resDir, resName);
FileOutputStream os = new FileOutputStream(resFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
catch (Exception e) {
Log("Tried to write " + image + ", but: " + e.toString());
is.close();
os.close();
return resFile.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
Log("Failed to export resource " + resName + ". Exception thrown: "
+ e);
}
return null;
}
}
......@@ -52,8 +52,8 @@ public class calib3dTest extends OpenCVTestCase {
Calib3d.composeRT(rvec1, tvec1, rvec2, tvec2, rvec3, tvec3);
assertMatEqual(outRvec, rvec3);
assertMatEqual(outTvec, tvec3);
assertMatEqual(outRvec, rvec3, EPS);
assertMatEqual(outTvec, tvec3, EPS);
}
public void testComposeRTMatMatMatMatMatMatMat() {
......@@ -367,15 +367,122 @@ public class calib3dTest extends OpenCVTestCase {
}
public void testReprojectImageTo3DMatMatMat() {
fail("Not yet implemented");
Mat transformMatrix = new Mat(4,4,CvType.CV_64F);
transformMatrix.put(0, 0,
0, 1, 0, 0,
1, 0, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
Mat disparity = new Mat(matSize,matSize,CvType.CV_32F);
float[] disp = new float[matSize * matSize];
for (int i = 0; i < matSize; i++)
for(int j = 0; j < matSize; j++)
disp[i * matSize + j] = i - j;
disparity.put(0, 0, disp);
Mat _3dPoints = new Mat();
Calib3d.reprojectImageTo3D(disparity, _3dPoints, transformMatrix);
assertEquals(CvType.CV_32FC3, _3dPoints.type());
assertEquals(matSize, _3dPoints.rows());
assertEquals(matSize, _3dPoints.cols());
truth = new Mat(matSize,matSize,CvType.CV_32FC3);
float[] _truth = new float[matSize * matSize * 3];
for (int i = 0; i < matSize; i++)
for(int j = 0; j < matSize; j++)
{
_truth[(i * matSize + j) * 3 + 0] = i;
_truth[(i * matSize + j) * 3 + 1] = j;
_truth[(i * matSize + j) * 3 + 2] = i-j;
}
truth.put(0, 0, _truth);
assertMatEqual(truth, _3dPoints, EPS);
}
public void testReprojectImageTo3DMatMatMatBoolean() {
fail("Not yet implemented");
Mat transformMatrix = new Mat(4,4,CvType.CV_64F);
transformMatrix.put(0, 0,
0, 1, 0, 0,
1, 0, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
Mat disparity = new Mat(matSize,matSize,CvType.CV_32F);
float[] disp = new float[matSize * matSize];
for (int i = 0; i < matSize; i++)
for(int j = 0; j < matSize; j++)
disp[i * matSize + j] = i - j;
disp[0] = -Float.MAX_VALUE;
disparity.put(0, 0, disp);
Mat _3dPoints = new Mat();
Calib3d.reprojectImageTo3D(disparity, _3dPoints, transformMatrix, true);
assertEquals(CvType.CV_32FC3, _3dPoints.type());
assertEquals(matSize, _3dPoints.rows());
assertEquals(matSize, _3dPoints.cols());
truth = new Mat(matSize,matSize,CvType.CV_32FC3);
float[] _truth = new float[matSize * matSize * 3];
for (int i = 0; i < matSize; i++)
for(int j = 0; j < matSize; j++)
{
_truth[(i * matSize + j) * 3 + 0] = i;
_truth[(i * matSize + j) * 3 + 1] = j;
_truth[(i * matSize + j) * 3 + 2] = i-j;
}
_truth[2] = 10000;
truth.put(0, 0, _truth);
assertMatEqual(truth, _3dPoints, EPS);
}
public void testReprojectImageTo3DMatMatMatBooleanInt() {
fail("Not yet implemented");
Mat transformMatrix = new Mat(4,4,CvType.CV_64F);
transformMatrix.put(0, 0,
0, 1, 0, 0,
1, 0, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
Mat disparity = new Mat(matSize,matSize,CvType.CV_32F);
float[] disp = new float[matSize * matSize];
for (int i = 0; i < matSize; i++)
for(int j = 0; j < matSize; j++)
disp[i * matSize + j] = i - j;
disparity.put(0, 0, disp);
Mat _3dPoints = new Mat();
Calib3d.reprojectImageTo3D(disparity, _3dPoints, transformMatrix, false, CvType.CV_16S);
assertEquals(CvType.CV_16SC3, _3dPoints.type());
assertEquals(matSize, _3dPoints.rows());
assertEquals(matSize, _3dPoints.cols());
truth = new Mat(matSize,matSize,CvType.CV_16SC3);
short[] _truth = new short[matSize * matSize * 3];
for (short i = 0; i < matSize; i++)
for(short j = 0; j < matSize; j++)
{
_truth[(i * matSize + j) * 3 + 0] = i;
_truth[(i * matSize + j) * 3 + 1] = j;
_truth[(i * matSize + j) * 3 + 2] = (short) (i-j);
}
truth.put(0, 0, _truth);
assertMatEqual(truth, _3dPoints, EPS);
}
public void testRodriguesMatMat() {
......
......@@ -29,8 +29,8 @@ public class MatTest extends OpenCVTestCase {
}
public void testColRange() {
Mat cols = gray0.colRange(0, gray0.cols()/2);
assertEquals(gray0.cols()/2, cols.cols());
Mat cols = gray0.colRange(0, gray0.cols() / 2);
assertEquals(gray0.cols() / 2, cols.cols());
assertEquals(gray0.rows(), cols.rows());
}
......@@ -48,7 +48,7 @@ public class MatTest extends OpenCVTestCase {
answer.put(0, 0, 7.0, 1.0, -5.0);
Mat cross = v1.cross(v2);
assertMatEqual(answer, cross);
assertMatEqual(answer, cross, EPS);
}
public void testDataAddr() {
......@@ -61,11 +61,11 @@ public class MatTest extends OpenCVTestCase {
}
public void testRelease() {
assertTrue( gray0.empty() == false );
assertTrue( gray0.rows() > 0 );
assertTrue(gray0.empty() == false);
assertTrue(gray0.rows() > 0);
gray0.release();
assertTrue( gray0.empty() == true );
assertTrue( gray0.rows() == 0 );
assertTrue(gray0.empty() == true);
assertTrue(gray0.rows() == 0);
}
public void testDot() {
......@@ -90,7 +90,7 @@ public class MatTest extends OpenCVTestCase {
public void testEye() {
Mat eye = Mat.eye(3, 3, CvType.CV_32FC1);
assertMatEqual(eye, eye.inv());
assertMatEqual(eye, eye.inv(), EPS);
}
public void testGetIntInt() {
......@@ -129,19 +129,19 @@ public class MatTest extends OpenCVTestCase {
public void testInv() {
dst = grayE_32f.inv();
assertMatEqual(grayE_32f, dst);
assertMatEqual(grayE_32f, dst, EPS);
}
public void testIsContinuous() {
assertTrue(gray0.isContinuous());
Mat subMat = gray0.submat(0, 0, gray0.rows()/2, gray0.cols()/2);
Mat subMat = gray0.submat(0, 0, gray0.rows() / 2, gray0.cols() / 2);
assertFalse(subMat.isContinuous());
}
public void testIsSubmatrix() {
assertFalse(gray0.isSubmatrix());
Mat subMat = gray0.submat(0, 0, gray0.rows()/2, gray0.cols()/2);
Mat subMat = gray0.submat(0, 0, gray0.rows() / 2, gray0.cols() / 2);
assertTrue(subMat.isSubmatrix());
}
......@@ -160,11 +160,13 @@ public class MatTest extends OpenCVTestCase {
}
public void testMatIntIntCvTypeScalar() {
dst = new Mat(gray127.rows(), gray127.cols(), CvType.CV_8U, new Scalar(127));
dst = new Mat(gray127.rows(), gray127.cols(), CvType.CV_8U, new Scalar(
127));
assertFalse(dst.empty());
assertMatEqual(dst, gray127);
dst = new Mat(rgba128.rows(), rgba128.cols(), CvType.CV_8UC4, Scalar.all(128));
dst = new Mat(rgba128.rows(), rgba128.cols(), CvType.CV_8UC4,
Scalar.all(128));
assertFalse(dst.empty());
assertMatEqual(dst, rgba128);
}
......@@ -178,13 +180,15 @@ public class MatTest extends OpenCVTestCase {
}
public void testMatIntIntIntScalar() {
Mat m1 = new Mat(gray127.rows(), gray127.cols(), CvType.CV_8U, new Scalar(127));
Mat m1 = new Mat(gray127.rows(), gray127.cols(), CvType.CV_8U,
new Scalar(127));
assertFalse(m1.empty());
assertMatEqual(m1, gray127);
Mat m2 = new Mat(gray0_32f.rows(), gray0_32f.cols(), CvType.CV_32F, new Scalar(0));
Mat m2 = new Mat(gray0_32f.rows(), gray0_32f.cols(), CvType.CV_32F,
new Scalar(0));
assertFalse(m2.empty());
assertMatEqual(m2, gray0_32f);
assertMatEqual(m2, gray0_32f, EPS);
}
public void testPutIntIntByteArray() {
......@@ -214,8 +218,8 @@ public class MatTest extends OpenCVTestCase {
}
public void testRowRange() {
Mat rows = gray0.rowRange(0, gray0.rows()/2);
assertEquals(gray0.rows()/2, rows.rows());
Mat rows = gray0.rowRange(0, gray0.rows() / 2);
assertEquals(gray0.rows() / 2, rows.rows());
assertEquals(gray0.cols(), rows.cols());
}
......@@ -229,9 +233,9 @@ public class MatTest extends OpenCVTestCase {
}
public void testSubmat() {
Mat submat = gray0.submat(0, gray0.rows()/2, 0, gray0.cols()/2);
assertEquals(gray0.rows()/2, submat.rows());
assertEquals(gray0.cols()/2, submat.cols());
Mat submat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2);
assertEquals(gray0.rows() / 2, submat.rows());
assertEquals(gray0.cols() / 2, submat.cols());
}
public void testToString() {
......
......@@ -426,6 +426,13 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nInv
return (jlong) new cv::Mat(me->inv());
}
JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nReshape
(JNIEnv* env, jclass cls, jlong self, jint cn, jint rows)
{
cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
return (jlong) new cv::Mat(me->reshape(cn, rows));
}
JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nCreateMat__
(JNIEnv* env, jclass cls)
{
......
......@@ -276,6 +276,16 @@ public class Mat {
return new Mat( nInv(nativeObj) );
}
//javadoc:Mat::reshape(cn)
public Mat reshape(int cn) {
return new Mat( nReshape(nativeObj, cn, 0) );
}
//javadoc:Mat::reshape(cn, rows)
public Mat reshape(int cn, int rows) {
return new Mat( nReshape(nativeObj, cn, rows) );
}
//javadoc:Mat::getNativeObjAddr()
public long getNativeObjAddr() {
return nativeObj;
......@@ -315,6 +325,7 @@ public class Mat {
private static native double nDot(long self, long mat);
private static native long nCross(long self, long mat);
private static native long nInv(long self);
private static native long nReshape(long self, int cn, int rows);
private static native long nEye(int rows, int cols, int type);
private static native String nDump(long self);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment