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;
/**
* This only class is Android specific.
* The original idea about test order randomization is from marek.defecinski blog.
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.
*
* @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";
static public void Log(String message) {
Log.e(TAG, message);
}
private AndroidTestRunner androidTestRunner;
private static String TAG = "opencv_test_java";
static public void Log(String message) {
Log.e(TAG, message);
}
@Override
@Override
public void onStart() {
ExportResourceImage("lena.jpg", R.drawable.lena);
ExportResourceImage("chessboard.jpg", R.drawable.chessboard);
//FIXME: implement export of the cascade
//List<TestCase> testCases = androidTestRunner.getTestCases();
//Collections.shuffle(testCases); //shuffle the tests order
LENA_PATH = ExportResource(R.drawable.lena);
CHESS_PATH = ExportResource(R.drawable.chessboard);
LBPCASCADE_FRONTALFACE_PATH = ExportResource(R.raw.lbpcascade_frontalface);
// List<TestCase> testCases = androidTestRunner.getTestCases();
// Collections.shuffle(testCases); //shuffle the tests order
super.onStart();
}
@Override
protected AndroidTestRunner getAndroidTestRunner() {
androidTestRunner = super.getAndroidTestRunner();
return androidTestRunner;
@Override
protected AndroidTestRunner getAndroidTestRunner() {
androidTestRunner = super.getAndroidTestRunner();
return androidTestRunner;
}
private String ExportResource(int resourceId) {
String fullname = getContext().getResources().getString(resourceId);
String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
try {
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);
}
is.close();
os.close();
return resFile.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
Log("Failed to export resource " + resName + ". Exception thrown: "
+ e);
}
return null;
}
private void ExportResourceImage(String image, int rId) {
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();
}
catch (Exception e) {
Log("Tried to write " + image + ", but: " + e.toString());
}
}
}
......@@ -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() {
......
This diff is collapsed.
......@@ -275,6 +275,16 @@ public class Mat {
public Mat inv() {
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() {
......@@ -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