Commit ea735a04 authored by Andrey Pavlenko's avatar Andrey Pavlenko

Java API: fixing Mat::put() for non-continuous Mat-s, adding/improving tests

parent 87888984
...@@ -275,7 +275,7 @@ public class ImgprocTest extends OpenCVTestCase { ...@@ -275,7 +275,7 @@ public class ImgprocTest extends OpenCVTestCase {
assertMatEqual(truth, hist, EPS); assertMatEqual(truth, hist, EPS);
} }
public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat2d() { public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat2D() {
List<Mat> images = Arrays.asList(gray255, gray128); List<Mat> images = Arrays.asList(gray255, gray128);
MatOfInt channels = new MatOfInt(0, 1); MatOfInt channels = new MatOfInt(0, 1);
MatOfInt histSize = new MatOfInt(10, 10); MatOfInt histSize = new MatOfInt(10, 10);
...@@ -292,6 +292,43 @@ public class ImgprocTest extends OpenCVTestCase { ...@@ -292,6 +292,43 @@ public class ImgprocTest extends OpenCVTestCase {
assertMatEqual(truth, hist, EPS); assertMatEqual(truth, hist, EPS);
} }
public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloat3D() {
List<Mat> images = Arrays.asList(rgbLena);
Mat hist3D = new Mat();
List<Mat> histList = Arrays.asList( new Mat[] {new Mat(), new Mat(), new Mat()} );
MatOfInt histSize = new MatOfInt(10);
MatOfFloat ranges = new MatOfFloat(0f, 256f);
for(int i=0; i<rgbLena.channels(); i++)
{
Imgproc.calcHist(images, new MatOfInt(i), new Mat(), histList.get(i), histSize, ranges);
assertEquals(10, histList.get(i).checkVector(1));
}
Core.merge(histList, hist3D);
assertEquals(CvType.CV_32FC3, hist3D.type());
Mat truth = new Mat(10, 1, CvType.CV_32FC3);
truth.put(0, 0,
0, 24870, 0,
1863, 31926, 1,
56682, 37677, 2260,
77278, 44751, 32436,
69397, 41343, 18526,
27180, 40407, 18658,
21101, 15993, 32042,
8343, 18585, 47786,
300, 6567, 80988,
0, 25, 29447
);
assertMatEqual(truth, hist3D, EPS);
}
public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloatBoolean() { public void testCalcHistListOfMatListOfIntegerMatMatListOfIntegerListOfFloatBoolean() {
List<Mat> images = Arrays.asList(gray255, gray128); List<Mat> images = Arrays.asList(gray255, gray128);
MatOfInt channels = new MatOfInt(0, 1); MatOfInt channels = new MatOfInt(0, 1);
......
...@@ -2,11 +2,12 @@ ...@@ -2,11 +2,12 @@
#include "converters.h" #include "converters.h"
#ifdef DEBUG
#include <android/log.h> #include <android/log.h>
#define MODULE_LOG_TAG "OpenCV.core.Mat" #define LOG_TAG "org.opencv.core.Mat"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__)) #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#else //DEBUG #ifdef DEBUG
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#else //!DEBUG
#define LOGD(...) #define LOGD(...)
#endif //DEBUG #endif //DEBUG
...@@ -1979,7 +1980,7 @@ template<typename T> static int mat_put(cv::Mat* m, int row, int col, int count, ...@@ -1979,7 +1980,7 @@ template<typename T> static int mat_put(cv::Mat* m, int row, int col, int count,
if(! buff) return 0; if(! buff) return 0;
count *= sizeof(T); count *= sizeof(T);
int rest = ((m->rows - row) * m->cols - col) * m->channels() * sizeof(T); int rest = ((m->rows - row) * m->cols - col) * m->elemSize();
if(count>rest) count = rest; if(count>rest) count = rest;
int res = count; int res = count;
...@@ -1988,14 +1989,14 @@ template<typename T> static int mat_put(cv::Mat* m, int row, int col, int count, ...@@ -1988,14 +1989,14 @@ template<typename T> static int mat_put(cv::Mat* m, int row, int col, int count,
memcpy(m->ptr(row, col), buff, count); memcpy(m->ptr(row, col), buff, count);
} else { } else {
// row by row // row by row
int num = (m->cols - col - 1) * m->channels() * sizeof(T); // 1st partial row int num = (m->cols - col) * m->elemSize(); // 1st partial row
if(count<num) num = count; if(count<num) num = count;
uchar* data = m->ptr(row++, col); uchar* data = m->ptr(row++, col);
while(count>0){ while(count>0){
memcpy(data, buff, num); memcpy(data, buff, num);
count -= num; count -= num;
buff += num; buff += num;
num = m->cols * m->channels() * sizeof(T); num = m->cols * m->elemSize();
if(count<num) num = count; if(count<num) num = count;
data = m->ptr(row++, 0); data = m->ptr(row++, 0);
} }
...@@ -2197,8 +2198,23 @@ JNIEXPORT jstring JNICALL Java_org_opencv_core_Mat_nDump ...@@ -2197,8 +2198,23 @@ JNIEXPORT jstring JNICALL Java_org_opencv_core_Mat_nDump
{ {
cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
std::stringstream s; std::stringstream s;
s << *me; try {
return env->NewStringUTF(s.str().c_str()); LOGD("Mat::nDump()");
s << *me;
return env->NewStringUTF(s.str().c_str());
} catch(cv::Exception e) {
LOGE("Mat::nDump() catched cv::Exception: %s", e.what());
jclass je = env->FindClass("org/opencv/core/CvException");
if(!je) je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, e.what());
return env->NewStringUTF("ERROR");
} catch (...) {
LOGE("Mat::nDump() catched unknown exception (...)");
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "Unknown exception in JNI code {Mat::nDump()}");
return env->NewStringUTF("ERROR");
}
} }
......
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