Added functions required for Java API wrappers generating converting Mat-s to/from vectors.

This commit is contained in:
Leonid Beynenson 2011-07-16 11:25:33 +00:00
parent e76ad5509c
commit bf40a3a5b8
3 changed files with 90 additions and 15 deletions

View File

@ -21,6 +21,7 @@ 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";
private static String TAG = "opencv_test_java";
private AndroidTestRunner androidTestRunner;

View File

@ -1,8 +1,30 @@
package org.opencv.test.objdetect;
import org.opencv.test.OpenCVTestCase;
import java.util.ArrayList;
import org.opencv.Mat;
import org.opencv.objdetect;
import org.opencv.imgproc;
import org.opencv.highgui;
import org.opencv.core;
import org.opencv.test.OpenCVTestCase;
import org.opencv.test.OpenCVTestRunner;
import org.opencv.Rect;
import org.opencv.Size;
public class objdetectTest extends OpenCVTestCase {
public void testCascadeClassifierFaceDetector() {
//objdetect.CascadeClassifier cc=new objdetect.CascadeClassifier("/mnt/sdcard/lbpcascade_frontalface.xml");
objdetect.CascadeClassifier cc=new objdetect.CascadeClassifier("/mnt/sdcard/haarcascade_frontalface_alt2.xml");
ArrayList<Rect> faces=new ArrayList<Rect>();
Mat shot002=highgui.imread("/mnt/sdcard/shot0002.png");
OpenCVTestRunner.Log("after imread shot002");
cc.detectMultiScale(shot002, faces, 1.1, 2, 2 /*TODO: CV_HAAR_SCALE_IMAGE*/, new Size(10,10));
OpenCVTestRunner.Log("faces.size="+faces.size());
}
}

View File

@ -1,4 +1,8 @@
#include "utils.h"
#include <android/log.h>
#define MODULE_LOG_TAG "OpenCV.utils.cpp"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__))
using namespace cv;
@ -6,12 +10,21 @@ using namespace cv;
void Mat_to_vector_int(Mat& mat, vector<int>& v_int)
{
v_int.clear();
if(mat.type()!= CV_32SC1 || mat.rows!=1)
return;
for(int i=0; i<mat.cols; i++)
v_int.push_back( mat.at< int >(0, i) );
}
void vector_int_to_Mat(vector<int>& v_int, Mat& mat)
{
return;
mat.create(1, v_int.size(), CV_32SC1);
for(size_t i=0; i<v_int.size(); i++)
mat.at< int >(0, i) = v_int[i];
}
@ -19,12 +32,21 @@ void vector_int_to_Mat(vector<int>& v_int, Mat& mat)
void Mat_to_vector_double(Mat& mat, vector<double>& v_double)
{
v_double.clear();
if(mat.type()!= CV_64FC1 || mat.rows!=1)
return;
for(int i=0; i<mat.cols; i++)
v_double.push_back( mat.at< double >(0, i) );
}
void vector_double_to_Mat(vector<double>& v_double, Mat& mat)
{
return;
mat.create(1, v_double.size(), CV_64FC1);
for(size_t i=0; i<v_double.size(); i++)
mat.at< double >(0, i) = v_double[i];
}
@ -32,12 +54,21 @@ void vector_double_to_Mat(vector<double>& v_double, Mat& mat)
void Mat_to_vector_float(Mat& mat, vector<float>& v_float)
{
v_float.clear();
if(mat.type()!= CV_32FC1 || mat.rows!=1)
return;
for(int i=0; i<mat.cols; i++)
v_float.push_back( mat.at< float >(0, i) );
}
void vector_float_to_Mat(vector<float>& v_float, Mat& mat)
{
return;
mat.create(1, v_float.size(), CV_32FC1);
for(size_t i=0; i<v_float.size(); i++)
mat.at< float >(0, i) = v_float[i];
}
@ -45,7 +76,14 @@ void vector_float_to_Mat(vector<float>& v_float, Mat& mat)
void Mat_to_vector_uchar(cv::Mat& mat, std::vector<uchar>& v_uchar)
{
v_uchar.clear();
if(mat.type()!= CV_8UC1 || mat.rows!=1)
return;
for(int i=0; i<mat.cols; i++)
v_uchar.push_back( mat.at< uchar >(0, i) );
}
@ -53,12 +91,29 @@ void Mat_to_vector_uchar(cv::Mat& mat, std::vector<uchar>& v_uchar)
void Mat_to_vector_Rect(Mat& mat, vector<Rect>& v_rect)
{
LOGD("Mat_to_vector_Rect start, mat.cols=%d", mat.cols);
v_rect.clear();
if(mat.type()!= CV_32SC4 || mat.rows!=1) {
LOGD("ERROR mat.type()!= CV_32SC4 || mat.rows!=1");
return;
}
for(int i=0; i<mat.cols; i++) {
Vec<int, 4> v=mat.at< Vec<int, 4> >(0, i);
v_rect.push_back( Rect(v[0], v[1], v[2], v[3]) );
}
LOGD("Mat_to_vector_Rect end, vec.size=%d", (int)v_rect.size());
}
void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)
{
return;
LOGD("vector_Rect_to_Mat start, vec.size=%d", (int)v_rect.size());
mat.create(1, v_rect.size(), CV_32SC4);
for(size_t i=0; i<v_rect.size(); i++) {
mat.at< Vec<int, 4> >(0, i) = Vec<int, 4>(v_rect[i].x, v_rect[i].y, v_rect[i].width, v_rect[i].height);
}
LOGD("vector_Rect_to_Mat end, mat.cols=%d", mat.cols);
}
@ -72,17 +127,14 @@ void Mat_to_vector_Point(Mat& mat, vector<Point>& v_point)
for(int i=0; i<mat.cols; i++)
v_point.push_back( Point( mat.at< Vec<int, 2> >(0, i) ) );
return;
}
void vector_Point_to_Mat(vector<Point>& v_point, Mat& mat)
{
mat.create(1, v_point.size(), CV_32SC2);
for(int i=0; i<v_point.size(); i++)
for(size_t i=0; i<v_point.size(); i++)
mat.at< Vec<int, 2> >(0, i) = Vec<int, 2>(v_point[i].x, v_point[i].y);
return;
}