diff --git a/android/android-jni/AndroidManifest.xml b/android/android-jni/AndroidManifest.xml
index 64b428a1fd..f0a9639b6c 100644
--- a/android/android-jni/AndroidManifest.xml
+++ b/android/android-jni/AndroidManifest.xml
@@ -11,6 +11,10 @@
regular Android project.
-->
+
+
+
+
diff --git a/android/android-jni/Makefile b/android/android-jni/Makefile
index db1c2a2eb6..e208cca67c 100644
--- a/android/android-jni/Makefile
+++ b/android/android-jni/Makefile
@@ -12,6 +12,10 @@ $(info gedit $(LOCAL_ENV_MK))
$(error Please setup the $(LOCAL_ENV_MK) - the default was just created')
endif
+ifndef ARM_TARGETS
+ARM_TARGETS=armeabi armeabi-v7a
+endif
+
ANDROID_NDK_BASE = $(ANDROID_NDK_ROOT)
$(info OPENCV_CONFIG = $(OPENCV_CONFIG))
@@ -44,7 +48,7 @@ all: $(LIB) nogdb
#calls the ndk-build script, passing it OPENCV_ROOT and OPENCV_LIBS_DIR
$(LIB): $(SWIG_C_OUT) $(SOURCES) $(HEADERS) $(ANDROID_MKS)
$(ANDROID_NDK_BASE)/ndk-build OPENCV_CONFIG=$(OPENCV_CONFIG) \
- PROJECT_PATH=$(PROJECT_PATH) V=$(V) $(NDK_FLAGS)
+ PROJECT_PATH=$(PROJECT_PATH) ARM_TARGETS=$(ARM_TARGETS) V=$(V) $(NDK_FLAGS)
#this creates the swig wrappers
@@ -70,5 +74,5 @@ clean-swig:
#does clean-swig and then uses the ndk-build clean
clean: clean-swig
$(ANDROID_NDK_BASE)/ndk-build OPENCV_CONFIG=$(OPENCV_CONFIG) \
- PROJECT_PATH=$(PROJECT_PATH) clean V=$(V) $(NDK_FLAGS)
+ PROJECT_PATH=$(PROJECT_PATH) clean ARM_TARGETS=$(ARM_TARGETS) V=$(V) $(NDK_FLAGS)
diff --git a/android/android-jni/jni/Application.mk b/android/android-jni/jni/Application.mk
index 5d44df5975..f23b245dca 100644
--- a/android/android-jni/jni/Application.mk
+++ b/android/android-jni/jni/Application.mk
@@ -1,2 +1,2 @@
-APP_ABI := armeabi armeabi-v7a
+APP_ABI := $(ARM_TARGETS)
APP_MODULES := android-opencv
diff --git a/android/android-jni/jni/Calibration.cpp b/android/android-jni/jni/Calibration.cpp
index c3e95fecc1..2f62acc905 100644
--- a/android/android-jni/jni/Calibration.cpp
+++ b/android/android-jni/jni/Calibration.cpp
@@ -7,255 +7,240 @@
#include "Calibration.h"
-
#include
using namespace cv;
-Calibration::Calibration():patternsize(6,8)
+Calibration::Calibration() :
+ patternsize(6, 8)
{
}
-Calibration::~Calibration() {
+Calibration::~Calibration()
+{
}
-
namespace
{
-double computeReprojectionErrors(
- const vector >& objectPoints, const vector >& imagePoints, const vector& rvecs,
- const vector& tvecs, const Mat& cameraMatrix,
- const Mat& distCoeffs, vector& perViewErrors) {
- vector imagePoints2;
- int i, totalPoints = 0;
- double totalErr = 0, err;
- perViewErrors.resize(objectPoints.size());
-
- for (i = 0; i < (int) objectPoints.size(); i++) {
- projectPoints(Mat(objectPoints[i]), rvecs[i], tvecs[i], cameraMatrix,
- distCoeffs, imagePoints2);
- err = norm(Mat(imagePoints[i]), Mat(imagePoints2), CV_L1 );
- int n = (int) objectPoints[i].size();
- perViewErrors[i] = err / n;
- totalErr += err;
- totalPoints += n;
- }
-
- return totalErr / totalPoints;
+double computeReprojectionErrors(const vector >& objectPoints,
+ const vector >& imagePoints, const vector& rvecs, const vector<
+ Mat>& tvecs, const Mat& cameraMatrix, const Mat& distCoeffs,
+ vector& perViewErrors)
+{
+ vector imagePoints2;
+ int i, totalPoints = 0;
+ double totalErr = 0, err;
+ perViewErrors.resize(objectPoints.size());
+
+ for (i = 0; i < (int)objectPoints.size(); i++)
+ {
+ projectPoints(Mat(objectPoints[i]), rvecs[i], tvecs[i], cameraMatrix, distCoeffs, imagePoints2);
+ err = norm(Mat(imagePoints[i]), Mat(imagePoints2), CV_L1);
+ int n = (int)objectPoints[i].size();
+ perViewErrors[i] = err / n;
+ totalErr += err;
+ totalPoints += n;
+ }
+
+ return totalErr / totalPoints;
}
+void calcChessboardCorners(Size boardSize, float squareSize, vector& corners)
+{
+ corners.resize(0);
-void calcChessboardCorners(Size boardSize, float squareSize, vector<
- Point3f>& corners) {
- corners.resize(0);
-
- for (int i = 0; i < boardSize.height; i++)
- for (int j = 0; j < boardSize.width; j++)
- corners.push_back(Point3f(float(j * squareSize), float(i
- * squareSize), 0));
+ for (int i = 0; i < boardSize.height; i++)
+ for (int j = 0; j < boardSize.width; j++)
+ corners.push_back(Point3f(float(j * squareSize), float(i * squareSize), 0));
}
/**from opencv/samples/cpp/calibration.cpp
*
*/
-bool runCalibration(vector > imagePoints,
- Size imageSize, Size boardSize, float squareSize, float aspectRatio,
- int flags, Mat& cameraMatrix, Mat& distCoeffs, vector& rvecs,
- vector& tvecs, vector& reprojErrs, double& totalAvgErr) {
- cameraMatrix = Mat::eye(3, 3, CV_64F);
- if (flags & CV_CALIB_FIX_ASPECT_RATIO)
- cameraMatrix.at (0, 0) = aspectRatio;
+bool runCalibration(vector > imagePoints, Size imageSize, Size boardSize, float squareSize,
+ float aspectRatio, int flags, Mat& cameraMatrix, Mat& distCoeffs, vector& rvecs,
+ vector& tvecs, vector& reprojErrs, double& totalAvgErr)
+{
+ cameraMatrix = Mat::eye(3, 3, CV_64F);
+ if (flags & CV_CALIB_FIX_ASPECT_RATIO)
+ cameraMatrix.at (0, 0) = aspectRatio;
- distCoeffs = Mat::zeros(4, 1, CV_64F);
+ distCoeffs = Mat::zeros(4, 1, CV_64F);
- vector > objectPoints(1);
- calcChessboardCorners(boardSize, squareSize, objectPoints[0]);
- for (size_t i = 1; i < imagePoints.size(); i++)
- objectPoints.push_back(objectPoints[0]);
+ vector > objectPoints(1);
+ calcChessboardCorners(boardSize, squareSize, objectPoints[0]);
+ for (size_t i = 1; i < imagePoints.size(); i++)
+ objectPoints.push_back(objectPoints[0]);
- calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
- distCoeffs, rvecs, tvecs, flags);
+ calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, flags);
- bool ok = checkRange(cameraMatrix, CV_CHECK_QUIET ) && checkRange(
- distCoeffs, CV_CHECK_QUIET );
+ bool ok = checkRange(cameraMatrix, CV_CHECK_QUIET) && checkRange(distCoeffs, CV_CHECK_QUIET);
- totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints, rvecs,
- tvecs, cameraMatrix, distCoeffs, reprojErrs);
+ totalAvgErr
+ = computeReprojectionErrors(objectPoints, imagePoints, rvecs, tvecs, cameraMatrix, distCoeffs, reprojErrs);
- return ok;
+ return ok;
}
-void saveCameraParams(const string& filename, Size imageSize, Size boardSize,
- float squareSize, float aspectRatio, int flags,
- const Mat& cameraMatrix, const Mat& distCoeffs,
- const vector& rvecs, const vector& tvecs,
- const vector& reprojErrs,
- const vector >& imagePoints, double totalAvgErr) {
- FileStorage fs(filename, FileStorage::WRITE);
-
- time_t t;
- time(&t);
- struct tm *t2 = localtime(&t);
- char buf[1024];
- strftime(buf, sizeof(buf) - 1, "%c", t2);
-
- fs << "calibration_time" << buf;
-
- if (!rvecs.empty() || !reprojErrs.empty())
- fs << "nframes" << (int) std::max(rvecs.size(), reprojErrs.size());
- fs << "image_width" << imageSize.width;
- fs << "image_height" << imageSize.height;
- fs << "board_width" << boardSize.width;
- fs << "board_height" << boardSize.height;
- fs << "squareSize" << squareSize;
-
- if (flags & CV_CALIB_FIX_ASPECT_RATIO)
- fs << "aspectRatio" << aspectRatio;
-
- if (flags != 0) {
- sprintf(buf, "flags: %s%s%s%s",
- flags & CV_CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess"
- : "",
- flags & CV_CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
- flags & CV_CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point"
- : "",
- flags & CV_CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "");
- cvWriteComment(*fs, buf, 0);
- }
-
- fs << "flags" << flags;
-
- fs << "camera_matrix" << cameraMatrix;
- fs << "distortion_coefficients" << distCoeffs;
-
- fs << "avg_reprojection_error" << totalAvgErr;
- if (!reprojErrs.empty())
- fs << "per_view_reprojection_errors" << Mat(reprojErrs);
-
- if (!rvecs.empty() && !tvecs.empty()) {
- Mat bigmat(rvecs.size(), 6, CV_32F);
- for (size_t i = 0; i < rvecs.size(); i++) {
- Mat r = bigmat(Range(i, i + 1), Range(0, 3));
- Mat t = bigmat(Range(i, i + 1), Range(3, 6));
- rvecs[i].copyTo(r);
- tvecs[i].copyTo(t);
- }
- cvWriteComment(
- *fs,
- "a set of 6-tuples (rotation vector + translation vector) for each view",
- 0);
- fs << "extrinsic_parameters" << bigmat;
- }
-
- if (!imagePoints.empty()) {
- Mat imagePtMat(imagePoints.size(), imagePoints[0].size(), CV_32FC2);
- for (size_t i = 0; i < imagePoints.size(); i++) {
- Mat r = imagePtMat.row(i).reshape(2, imagePtMat.cols);
- Mat(imagePoints[i]).copyTo(r);
- }
- fs << "image_points" << imagePtMat;
- }
+void saveCameraParams(const string& filename, Size imageSize, Size boardSize, float squareSize, float aspectRatio,
+ int flags, const Mat& cameraMatrix, const Mat& distCoeffs, const vector& rvecs,
+ const vector& tvecs, const vector& reprojErrs,
+ const vector >& imagePoints, double totalAvgErr)
+{
+ FileStorage fs(filename, FileStorage::WRITE);
+
+ time_t t;
+ time(&t);
+ struct tm *t2 = localtime(&t);
+ char buf[1024];
+ strftime(buf, sizeof(buf) - 1, "%c", t2);
+
+ fs << "calibration_time" << buf;
+
+ if (!rvecs.empty() || !reprojErrs.empty())
+ fs << "nframes" << (int)std::max(rvecs.size(), reprojErrs.size());
+ fs << "image_width" << imageSize.width;
+ fs << "image_height" << imageSize.height;
+ fs << "board_width" << boardSize.width;
+ fs << "board_height" << boardSize.height;
+ fs << "squareSize" << squareSize;
+
+ if (flags & CV_CALIB_FIX_ASPECT_RATIO)
+ fs << "aspectRatio" << aspectRatio;
+
+ if (flags != 0)
+ {
+ sprintf(buf, "flags: %s%s%s%s", flags & CV_CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "", flags
+ & CV_CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "", flags & CV_CALIB_FIX_PRINCIPAL_POINT
+ ? "+fix_principal_point" : "", flags & CV_CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "");
+ cvWriteComment(*fs, buf, 0);
+ }
+
+ fs << "flags" << flags;
+
+ fs << "camera_matrix" << cameraMatrix;
+ fs << "distortion_coefficients" << distCoeffs;
+
+ fs << "avg_reprojection_error" << totalAvgErr;
+ if (!reprojErrs.empty())
+ fs << "per_view_reprojection_errors" << Mat(reprojErrs);
+
+ if (!rvecs.empty() && !tvecs.empty())
+ {
+ Mat bigmat(rvecs.size(), 6, CV_32F);
+ for (size_t i = 0; i < rvecs.size(); i++)
+ {
+ Mat r = bigmat(Range(i, i + 1), Range(0, 3));
+ Mat t = bigmat(Range(i, i + 1), Range(3, 6));
+ rvecs[i].copyTo(r);
+ tvecs[i].copyTo(t);
+ }
+ cvWriteComment(*fs, "a set of 6-tuples (rotation vector + translation vector) for each view", 0);
+ fs << "extrinsic_parameters" << bigmat;
+ }
+
+ if (!imagePoints.empty())
+ {
+ Mat imagePtMat(imagePoints.size(), imagePoints[0].size(), CV_32FC2);
+ for (size_t i = 0; i < imagePoints.size(); i++)
+ {
+ Mat r = imagePtMat.row(i).reshape(2, imagePtMat.cols);
+ Mat(imagePoints[i]).copyTo(r);
+ }
+ fs << "image_points" << imagePtMat;
+ }
}
}//anon namespace
-bool Calibration::detectAndDrawChessboard(int idx,image_pool* pool) {
-
- Mat grey;
- pool->getGrey(idx, grey);
- if (grey.empty())
- return false;
- vector corners;
+bool Calibration::detectAndDrawChessboard(int idx, image_pool* pool)
+{
+ Mat grey = pool->getGrey(idx);
+ if (grey.empty())
+ return false;
+ vector corners;
- IplImage iplgrey = grey;
- if (!cvCheckChessboard(&iplgrey, patternsize))
- return false;
- bool patternfound = findChessboardCorners(grey, patternsize, corners);
+ IplImage iplgrey = grey;
+ if (!cvCheckChessboard(&iplgrey, patternsize))
+ return false;
+ bool patternfound = findChessboardCorners(grey, patternsize, corners);
- Mat * img = pool->getImage(idx);
+ Mat img = pool->getImage(idx);
- if (corners.size() < 1)
- return false;
+ if (corners.size() < 1)
+ return false;
- cornerSubPix(grey, corners, Size(11, 11), Size(-1, -1), TermCriteria(
- CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
+ cornerSubPix(grey, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
- if(patternfound)
- imagepoints.push_back(corners);
+ if (patternfound)
+ imagepoints.push_back(corners);
- drawChessboardCorners(*img, patternsize, Mat(corners), patternfound);
+ drawChessboardCorners(img, patternsize, Mat(corners), patternfound);
- imgsize = grey.size();
+ imgsize = grey.size();
- return patternfound;
+ return patternfound;
}
-void Calibration::drawText(int i, image_pool* pool, const char* ctext){
- // Use "y" to show that the baseLine is about
- string text = ctext;
- int fontFace = FONT_HERSHEY_COMPLEX_SMALL;
- double fontScale = .8;
- int thickness = .5;
-
- Mat img = *pool->getImage(i);
-
- int baseline=0;
- Size textSize = getTextSize(text, fontFace,
- fontScale, thickness, &baseline);
- baseline += thickness;
-
- // center the text
- Point textOrg((img.cols - textSize.width)/2,
- (img.rows - textSize.height *2));
-
- // draw the box
- rectangle(img, textOrg + Point(0, baseline),
- textOrg + Point(textSize.width, -textSize.height),
- Scalar(0,0,255),CV_FILLED);
- // ... and the baseline first
- line(img, textOrg + Point(0, thickness),
- textOrg + Point(textSize.width, thickness),
- Scalar(0, 0, 255));
-
- // then put the text itself
- putText(img, text, textOrg, fontFace, fontScale,
- Scalar::all(255), thickness, 8);
-}
+void Calibration::drawText(int i, image_pool* pool, const char* ctext)
+{
+ // Use "y" to show that the baseLine is about
+ string text = ctext;
+ int fontFace = FONT_HERSHEY_COMPLEX_SMALL;
+ double fontScale = .8;
+ int thickness = .5;
+
+ Mat img = pool->getImage(i);
+
+ int baseline = 0;
+ Size textSize = getTextSize(text, fontFace, fontScale, thickness, &baseline);
+ baseline += thickness;
+
+ // center the text
+ Point textOrg((img.cols - textSize.width) / 2, (img.rows - textSize.height * 2));
-void Calibration::resetChess() {
+ // draw the box
+ rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0, 0, 255),
+ CV_FILLED);
+ // ... and the baseline first
+ line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));
- imagepoints.clear();
+ // then put the text itself
+ putText(img, text, textOrg, fontFace, fontScale, Scalar::all(255), thickness, 8);
}
-void Calibration::calibrate(const char* filename) {
+void Calibration::resetChess()
+{
- vector rvecs, tvecs;
- vector reprojErrs;
- double totalAvgErr = 0;
- int flags = 0;
- flags |= CV_CALIB_FIX_PRINCIPAL_POINT | CV_CALIB_FIX_ASPECT_RATIO;
- bool writeExtrinsics = true;
- bool writePoints = true;
+ imagepoints.clear();
+}
- bool ok = runCalibration(imagepoints, imgsize, patternsize, 1.f, 1.f,
- flags, K, distortion, rvecs, tvecs, reprojErrs, totalAvgErr);
+void Calibration::calibrate(const char* filename)
+{
+ vector rvecs, tvecs;
+ vector reprojErrs;
+ double totalAvgErr = 0;
+ int flags = 0;
+ flags |= CV_CALIB_FIX_PRINCIPAL_POINT | CV_CALIB_FIX_ASPECT_RATIO;
+ bool writeExtrinsics = true;
+ bool writePoints = true;
+ bool ok = runCalibration(imagepoints, imgsize, patternsize, 1.f, 1.f, flags, K, distortion, rvecs, tvecs, reprojErrs,
+ totalAvgErr);
- if (ok){
+ if (ok)
+ {
- saveCameraParams(filename, imgsize, patternsize, 1.f,
- 1.f, flags, K, distortion, writeExtrinsics ? rvecs
- : vector (), writeExtrinsics ? tvecs
- : vector (), writeExtrinsics ? reprojErrs
- : vector (), writePoints ? imagepoints : vector<
- vector > (), totalAvgErr);
- }
+ saveCameraParams(filename, imgsize, patternsize, 1.f, 1.f, flags, K, distortion, writeExtrinsics ? rvecs : vector<
+ Mat> (), writeExtrinsics ? tvecs : vector (), writeExtrinsics ? reprojErrs : vector (), writePoints
+ ? imagepoints : vector > (), totalAvgErr);
+ }
}
-int Calibration::getNumberDetectedChessboards() {
- return imagepoints.size();
+int Calibration::getNumberDetectedChessboards()
+{
+ return imagepoints.size();
}
diff --git a/android/android-jni/jni/Calibration.h b/android/android-jni/jni/Calibration.h
index f3a9453ea9..6e0eef3bc4 100644
--- a/android/android-jni/jni/Calibration.h
+++ b/android/android-jni/jni/Calibration.h
@@ -14,8 +14,6 @@
#include
#include
-
-
#include
#include "image_pool.h"
@@ -24,36 +22,33 @@
#define DETECT_STAR 1
#define DETECT_SURF 2
+class Calibration
+{
+public:
-class Calibration {
- std::vector keypoints;
-
- vector > imagepoints;
-
- cv::Mat K;
- cv::Mat distortion;
- cv::Size imgsize;
+ Calibration();
+ virtual ~Calibration();
+ bool detectAndDrawChessboard(int idx, image_pool* pool);
+ void resetChess();
-public:
+ int getNumberDetectedChessboards();
- cv::Size patternsize;
-
- Calibration();
- virtual ~Calibration();
+ void calibrate(const char* filename);
- bool detectAndDrawChessboard(int idx, image_pool* pool);
+ void drawText(int idx, image_pool* pool, const char* text);
- void resetChess();
+ cv::Size patternsize;
+private:
+ std::vector keypoints;
- int getNumberDetectedChessboards();
+ std::vector > imagepoints;
- void calibrate(const char* filename);
+ cv::Mat K;
+ cv::Mat distortion;
+ cv::Size imgsize;
- void drawText(int idx, image_pool* pool, const char* text);
};
-
-
#endif /* PROCESSOR_H_ */
diff --git a/android/android-jni/jni/gl_code.cpp b/android/android-jni/jni/gl_code.cpp
index c13374f5d1..4512b9dcd3 100644
--- a/android/android-jni/jni/gl_code.cpp
+++ b/android/android-jni/jni/gl_code.cpp
@@ -37,273 +37,286 @@ using namespace cv;
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
-static void printGLString(const char *name, GLenum s) {
- const char *v = (const char *) glGetString(s);
- LOGI("GL %s = %s\n", name, v);
+static void printGLString(const char *name, GLenum s)
+{
+ const char *v = (const char *)glGetString(s);
+ LOGI("GL %s = %s\n", name, v);
}
-static void checkGlError(const char* op) {
- for (GLint error = glGetError(); error; error = glGetError()) {
- LOGI("after %s() glError (0x%x)\n", op, error);
- }
+static void checkGlError(const char* op)
+{
+ for (GLint error = glGetError(); error; error = glGetError())
+ {
+ LOGI("after %s() glError (0x%x)\n", op, error);
+ }
}
static const char gVertexShader[] = "attribute vec4 a_position; \n"
- "attribute vec2 a_texCoord; \n"
- "varying vec2 v_texCoord; \n"
- "void main() \n"
- "{ \n"
- " gl_Position = a_position; \n"
- " v_texCoord = a_texCoord; \n"
- "} \n";
-
-static const char gFragmentShader[] =
- "precision mediump float; \n"
- "varying vec2 v_texCoord; \n"
- "uniform sampler2D s_texture; \n"
- "void main() \n"
- "{ \n"
- " gl_FragColor = texture2D( s_texture, v_texCoord );\n"
- "} \n";
-
-const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f };
-GLubyte testpixels[4 * 3] = { 255, 0, 0, // Red
- 0, 255, 0, // Green
- 0, 0, 255, // Blue
- 255, 255, 0 // Yellow
- };
-
-GLuint glcamera::createSimpleTexture2D(GLuint _textureid, GLubyte* pixels,
- int width, int height, int channels) {
-
- // Bind the texture
- glActiveTexture(GL_TEXTURE0);
- checkGlError("glActiveTexture");
- // Bind the texture object
- glBindTexture(GL_TEXTURE_2D, _textureid);
- checkGlError("glBindTexture");
-
- GLenum format;
- switch (channels) {
- case 3:
- format = GL_RGB;
- break;
- case 1:
- format = GL_LUMINANCE;
- break;
- case 4:
- format = GL_RGBA;
- break;
- }
- // Load the texture
- glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
- GL_UNSIGNED_BYTE, pixels);
-
- checkGlError("glTexImage2D");
- // Set the filtering mode
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
-
- return _textureid;
+ "attribute vec2 a_texCoord; \n"
+ "varying vec2 v_texCoord; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_Position = a_position; \n"
+ " v_texCoord = a_texCoord; \n"
+ "} \n";
+
+static const char gFragmentShader[] = "precision mediump float; \n"
+ "varying vec2 v_texCoord; \n"
+ "uniform sampler2D s_texture; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_FragColor = texture2D( s_texture, v_texCoord );\n"
+ "} \n";
+
+const GLfloat gTriangleVertices[] = {0.0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f};
+GLubyte testpixels[4 * 3] = {255, 0, 0, // Red
+ 0, 255, 0, // Green
+ 0, 0, 255, // Blue
+ 255, 255, 0 // Yellow
+ };
+
+GLuint glcamera::createSimpleTexture2D(GLuint _textureid, GLubyte* pixels, int width, int height, int channels)
+{
+
+ // Bind the texture
+ glActiveTexture( GL_TEXTURE0);
+ checkGlError("glActiveTexture");
+ // Bind the texture object
+ glBindTexture(GL_TEXTURE_2D, _textureid);
+ checkGlError("glBindTexture");
+
+ GLenum format;
+ switch (channels)
+ {
+ case 3:
+ format = GL_RGB;
+ break;
+ case 1:
+ format = GL_LUMINANCE;
+ break;
+ case 4:
+ format = GL_RGBA;
+ break;
+ }
+ // Load the texture
+ glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, pixels);
+
+ checkGlError("glTexImage2D");
+ // Set the filtering mode
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+ return _textureid;
}
-GLuint glcamera::loadShader(GLenum shaderType, const char* pSource) {
- GLuint shader = glCreateShader(shaderType);
- if (shader) {
- glShaderSource(shader, 1, &pSource, NULL);
- glCompileShader(shader);
- GLint compiled = 0;
- glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
- if (!compiled) {
- GLint infoLen = 0;
- glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
- if (infoLen) {
- char* buf = (char*) malloc(infoLen);
- if (buf) {
- glGetShaderInfoLog(shader, infoLen, NULL, buf);
- LOGE("Could not compile shader %d:\n%s\n",
- shaderType, buf);
- free(buf);
- }
- glDeleteShader(shader);
- shader = 0;
- }
- }
- }
- return shader;
+GLuint glcamera::loadShader(GLenum shaderType, const char* pSource)
+{
+ GLuint shader = glCreateShader(shaderType);
+ if (shader)
+ {
+ glShaderSource(shader, 1, &pSource, NULL);
+ glCompileShader(shader);
+ GLint compiled = 0;
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
+ if (!compiled)
+ {
+ GLint infoLen = 0;
+ glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
+ if (infoLen)
+ {
+ char* buf = (char*)malloc(infoLen);
+ if (buf)
+ {
+ glGetShaderInfoLog(shader, infoLen, NULL, buf);
+ LOGE("Could not compile shader %d:\n%s\n",
+ shaderType, buf);
+ free(buf);
+ }
+ glDeleteShader(shader);
+ shader = 0;
+ }
+ }
+ }
+ return shader;
}
-GLuint glcamera::createProgram(const char* pVertexSource,
- const char* pFragmentSource) {
- GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
- if (!vertexShader) {
- return 0;
- }
-
- GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
- if (!pixelShader) {
- return 0;
- }
-
- GLuint program = glCreateProgram();
- if (program) {
- glAttachShader(program, vertexShader);
- checkGlError("glAttachShader");
- glAttachShader(program, pixelShader);
- checkGlError("glAttachShader");
- glLinkProgram(program);
- GLint linkStatus = GL_FALSE;
- glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
- if (linkStatus != GL_TRUE) {
- GLint bufLength = 0;
- glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
- if (bufLength) {
- char* buf = (char*) malloc(bufLength);
- if (buf) {
- glGetProgramInfoLog(program, bufLength, NULL, buf);
- LOGE("Could not link program:\n%s\n", buf);
- free(buf);
- }
- }
- glDeleteProgram(program);
- program = 0;
- }
- }
- return program;
+GLuint glcamera::createProgram(const char* pVertexSource, const char* pFragmentSource)
+{
+ GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
+ if (!vertexShader)
+ {
+ return 0;
+ }
+
+ GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
+ if (!pixelShader)
+ {
+ return 0;
+ }
+
+ GLuint program = glCreateProgram();
+ if (program)
+ {
+ glAttachShader(program, vertexShader);
+ checkGlError("glAttachShader");
+ glAttachShader(program, pixelShader);
+ checkGlError("glAttachShader");
+ glLinkProgram(program);
+ GLint linkStatus = GL_FALSE;
+ glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
+ if (linkStatus != GL_TRUE)
+ {
+ GLint bufLength = 0;
+ glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
+ if (bufLength)
+ {
+ char* buf = (char*)malloc(bufLength);
+ if (buf)
+ {
+ glGetProgramInfoLog(program, bufLength, NULL, buf);
+ LOGE("Could not link program:\n%s\n", buf);
+ free(buf);
+ }
+ }
+ glDeleteProgram(program);
+ program = 0;
+ }
+ }
+ return program;
}
//GLuint textureID;
-bool glcamera::setupGraphics(int w, int h) {
- printGLString("Version", GL_VERSION);
- printGLString("Vendor", GL_VENDOR);
- printGLString("Renderer", GL_RENDERER);
- printGLString("Extensions", GL_EXTENSIONS);
-
- LOGI("setupGraphics(%d, %d)", w, h);
- gProgram = createProgram(gVertexShader, gFragmentShader);
- if (!gProgram) {
- LOGE("Could not create program.");
- return false;
- }
- gvPositionHandle = glGetAttribLocation(gProgram, "a_position");
- gvTexCoordHandle = glGetAttribLocation(gProgram, "a_texCoord");
-
- gvSamplerHandle = glGetAttribLocation(gProgram, "s_texture");
-
- // Use tightly packed data
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
- // Generate a texture object
- glGenTextures(1, &textureID);
- textureID = createSimpleTexture2D(textureID, testpixels, 2, 2, 3);
-
- checkGlError("glGetAttribLocation");
- LOGI("glGetAttribLocation(\"vPosition\") = %d\n",
- gvPositionHandle);
-
- glViewport(0, 0, w, h);
- checkGlError("glViewport");
- return true;
+bool glcamera::setupGraphics(int w, int h)
+{
+ printGLString("Version", GL_VERSION);
+ printGLString("Vendor", GL_VENDOR);
+ printGLString("Renderer", GL_RENDERER);
+ printGLString("Extensions", GL_EXTENSIONS);
+
+ LOGI("setupGraphics(%d, %d)", w, h);
+ gProgram = createProgram(gVertexShader, gFragmentShader);
+ if (!gProgram)
+ {
+ LOGE("Could not create program.");
+ return false;
+ }
+ gvPositionHandle = glGetAttribLocation(gProgram, "a_position");
+ gvTexCoordHandle = glGetAttribLocation(gProgram, "a_texCoord");
+
+ gvSamplerHandle = glGetAttribLocation(gProgram, "s_texture");
+
+ // Use tightly packed data
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+
+ // Generate a texture object
+ glGenTextures(1, &textureID);
+ textureID = createSimpleTexture2D(textureID, testpixels, 2, 2, 3);
+
+ checkGlError("glGetAttribLocation");
+ LOGI("glGetAttribLocation(\"vPosition\") = %d\n",
+ gvPositionHandle);
+
+ glViewport(0, 0, w, h);
+ checkGlError("glViewport");
+ return true;
}
-void glcamera::renderFrame() {
-
- GLfloat vVertices[] = { -1.0f, 1.0f, 0.0f, // Position 0
- 0.0f, 0.0f, // TexCoord 0
- -1.0f, -1.0f, 0.0f, // Position 1
- 0.0f, 1.0f, // TexCoord 1
- 1.0f, -1.0f, 0.0f, // Position 2
- 1.0f, 1.0f, // TexCoord 2
- 1.0f, 1.0f, 0.0f, // Position 3
- 1.0f, 0.0f // TexCoord 3
- };
- GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
- GLsizei stride = 5 * sizeof(GLfloat); // 3 for position, 2 for texture
-
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- checkGlError("glClearColor");
-
- glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
- checkGlError("glClear");
-
- glUseProgram(gProgram);
- checkGlError("glUseProgram");
-
- // Load the vertex position
- glVertexAttribPointer(gvPositionHandle, 3, GL_FLOAT, GL_FALSE, stride,
- vVertices);
- // Load the texture coordinate
- glVertexAttribPointer(gvTexCoordHandle, 2, GL_FLOAT, GL_FALSE, stride,
- &vVertices[3]);
-
- glEnableVertexAttribArray(gvPositionHandle);
- glEnableVertexAttribArray(gvTexCoordHandle);
-
- // Bind the texture
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, textureID);
-
- // Set the sampler texture unit to 0
- glUniform1i(gvSamplerHandle, 0);
-
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
-
- //checkGlError("glVertexAttribPointer");
- //glEnableVertexAttribArray(gvPositionHandle);
- //checkGlError("glEnableVertexAttribArray");
- //glDrawArrays(GL_TRIANGLES, 0, 3);
- //checkGlError("glDrawArrays");
-}
+void glcamera::renderFrame()
+{
-void glcamera::init(int width, int height) {
- newimage = false;
- nimg = Mat();
- setupGraphics(width, height);
+ GLfloat vVertices[] = {-1.0f, 1.0f, 0.0f, // Position 0
+ 0.0f, 0.0f, // TexCoord 0
+ -1.0f, -1.0f, 0.0f, // Position 1
+ 0.0f, 1.0f, // TexCoord 1
+ 1.0f, -1.0f, 0.0f, // Position 2
+ 1.0f, 1.0f, // TexCoord 2
+ 1.0f, 1.0f, 0.0f, // Position 3
+ 1.0f, 0.0f // TexCoord 3
+ };
+ GLushort indices[] = {0, 1, 2, 0, 2, 3};
+ GLsizei stride = 5 * sizeof(GLfloat); // 3 for position, 2 for texture
-}
+ glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ checkGlError("glClearColor");
+
+ glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
+ checkGlError("glClear");
+
+ glUseProgram(gProgram);
+ checkGlError("glUseProgram");
+
+ // Load the vertex position
+ glVertexAttribPointer(gvPositionHandle, 3, GL_FLOAT, GL_FALSE, stride, vVertices);
+ // Load the texture coordinate
+ glVertexAttribPointer(gvTexCoordHandle, 2, GL_FLOAT, GL_FALSE, stride, &vVertices[3]);
-void glcamera::step() {
- if (newimage && !nimg.empty()) {
+ glEnableVertexAttribArray(gvPositionHandle);
+ glEnableVertexAttribArray(gvTexCoordHandle);
- textureID = createSimpleTexture2D(textureID,
- nimg.ptr (0), nimg.rows, nimg.cols,
- nimg.channels());
- newimage = false;
- }
- renderFrame();
+ // Bind the texture
+ glActiveTexture( GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, textureID);
+ // Set the sampler texture unit to 0
+ glUniform1i(gvSamplerHandle, 0);
+
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
+
+ //checkGlError("glVertexAttribPointer");
+ //glEnableVertexAttribArray(gvPositionHandle);
+ //checkGlError("glEnableVertexAttribArray");
+ //glDrawArrays(GL_TRIANGLES, 0, 3);
+ //checkGlError("glDrawArrays");
}
-void glcamera::setTextureImage(Ptr img) {
+void glcamera::init(int width, int height)
+{
+ newimage = false;
+ nimg = Mat();
+ setupGraphics(width, height);
- //int p2 = (int)(std::log(img->size().width)/0.69315);
- int sz = 256;//std::pow(2,p2);
- Size size(sz, sz);
+}
- resize(*img, nimg, size,cv::INTER_NEAREST);
+void glcamera::step()
+{
+ if (newimage && !nimg.empty())
+ {
- newimage = true;
+ textureID = createSimpleTexture2D(textureID, nimg.ptr (0), nimg.rows, nimg.cols, nimg.channels());
+ newimage = false;
+ }
+ renderFrame();
}
+#define NEAREST_POW2(x)((int)(0.5 + std::log(x)/0.69315) )
+void glcamera::setTextureImage(const Mat& img)
+{
+ Size size(256, 256);
+ resize(img, nimg, size, cv::INTER_NEAREST);
+ newimage = true;
+}
-void glcamera::drawMatToGL(int idx, image_pool* pool) {
+void glcamera::drawMatToGL(int idx, image_pool* pool)
+{
- Ptr img = pool->getImage(idx);
+ Mat img = pool->getImage(idx);
- if (img.empty())
- return; //no image at input_idx!
+ if (img.empty())
+ return; //no image at input_idx!
- setTextureImage(img);
+ setTextureImage(img);
}
-glcamera::glcamera():newimage(false) {
- LOGI("glcamera constructor");
+glcamera::glcamera() :
+ newimage(false)
+{
+ LOGI("glcamera constructor");
}
-glcamera::~glcamera() {
- LOGI("glcamera destructor");
+glcamera::~glcamera()
+{
+ LOGI("glcamera destructor");
}
-
diff --git a/android/android-jni/jni/glcamera.h b/android/android-jni/jni/glcamera.h
index e96041058c..923fc535e4 100644
--- a/android/android-jni/jni/glcamera.h
+++ b/android/android-jni/jni/glcamera.h
@@ -6,35 +6,34 @@
#include
#include "image_pool.h"
-class glcamera {
- Mat nimg;
- bool newimage;
- GLuint textureID;
-
- GLuint gProgram;
- GLuint gvPositionHandle;
-
- GLuint gvTexCoordHandle;
- GLuint gvSamplerHandle;
+class glcamera
+{
public:
- glcamera();
- ~glcamera();
- void init(int width, int height);
- void step();
+ glcamera();
+ ~glcamera();
+ void init(int width, int height);
+ void step();
- void drawMatToGL(int idx, image_pool* pool);
- void setTextureImage(Ptr img);
+ void drawMatToGL(int idx, image_pool* pool);
+ void setTextureImage(const cv::Mat& img);
private:
- GLuint createSimpleTexture2D(GLuint _textureid, GLubyte* pixels, int width,
- int height, int channels);
- GLuint loadShader(GLenum shaderType, const char* pSource);
- GLuint
- createProgram(const char* pVertexSource,
- const char* pFragmentSource);
- bool setupGraphics(int w, int h);
- void renderFrame();
+ GLuint createSimpleTexture2D(GLuint _textureid, GLubyte* pixels, int width, int height, int channels);
+ GLuint loadShader(GLenum shaderType, const char* pSource);
+ GLuint
+ createProgram(const char* pVertexSource, const char* pFragmentSource);
+ bool setupGraphics(int w, int h);
+ void renderFrame();
+ cv::Mat nimg;
+ bool newimage;
+ GLuint textureID;
+
+ GLuint gProgram;
+ GLuint gvPositionHandle;
+
+ GLuint gvTexCoordHandle;
+ GLuint gvSamplerHandle;
};
#endif
diff --git a/android/android-jni/jni/image_pool.cpp b/android/android-jni/jni/image_pool.cpp
index 42e76b4a9a..6b0ccf4da6 100644
--- a/android/android-jni/jni/image_pool.cpp
+++ b/android/android-jni/jni/image_pool.cpp
@@ -5,92 +5,97 @@
#include
#include
+using namespace cv;
+
#define LOG_TAG "libandroid-opencv"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
-JNIEnv *env;
-LOGI("JNI_OnLoad called for opencv");
-return JNI_VERSION_1_4;
+ JNIEnv *env;
+ LOGI("JNI_OnLoad called for opencv");
+ return JNI_VERSION_1_4;
}
JNIEXPORT void JNICALL Java_com_opencv_jni_opencvJNI_addYUVtoPool(JNIEnv * env,
- jclass thiz, jlong ppool, jobject _jpool, jbyteArray jbuffer,
- jint jidx, jint jwidth, jint jheight, jboolean jgrey) {
- image_pool *pool = (image_pool *) ppool;
-
- Ptr mat = pool->getYUV(jidx);
+ jclass thiz, jlong ppool, jobject _jpool, jbyteArray jbuffer,
+ jint jidx, jint jwidth, jint jheight, jboolean jgrey)
+{
+ int buff_height = jheight + (jheight/2);
+ Size buff_size(jwidth,buff_height);
+ image_pool *pool = (image_pool *) ppool;
- if (mat.empty() || mat->cols != jwidth || mat->rows != jheight * 2) {
- //pool->deleteGrey(jidx);
- mat = new Mat(jheight * 2, jwidth, CV_8UC1);
- }
+ Mat mat = pool->getYUV(jidx);
- jsize sz = env->GetArrayLength(jbuffer);
- uchar* buff = mat->ptr (0);
+ if (mat.empty() || mat.size() != buff_size )
+ {
+ mat.create(buff_size, CV_8UC1);
+ }
- env->GetByteArrayRegion(jbuffer, 0, sz, (jbyte*) buff);
+ jsize sz = env->GetArrayLength(jbuffer);
+ uchar* buff = mat.ptr (0);
- pool->addYUVMat(jidx, mat);
- Ptr color = pool->getImage(jidx);
- if (color.empty() || color->cols != jwidth || color->rows != jheight) {
- //pool->deleteImage(jidx);
- color = new Mat(jheight, jwidth, CV_8UC3);
- }
- if (!jgrey) {
+ env->GetByteArrayRegion(jbuffer, 0, sz, (jbyte*) buff);
- //doesn't work unfortunately..
- //cvtColor(*mat,*color, CV_YCrCb2RGB);
- color_convert_common(buff, buff + jwidth * jheight, jwidth, jheight,
- color->ptr (0), false);
+ pool->addYUVMat(jidx, mat);
- }
+ Mat color = pool->getImage(jidx);
- if (jgrey) {
- Mat grey;
- pool->getGrey(jidx, grey);
+ if (!jgrey)
+ {
- cvtColor(grey, *color, CV_GRAY2RGB);
+ if (color.cols != jwidth || color.rows != jheight || color.channels() != 3)
+ {
+ color.create(jheight, jwidth, CV_8UC3);
+ }
+ //doesn't work unfortunately..
+ //TODO cvtColor(mat,color, CV_YCrCb2RGB);
+ color_convert_common(buff, buff + jwidth * jheight, jwidth, jheight,
+ color.ptr (0), false);
+ }
- }
+ if (jgrey)
+ {
+ Mat grey = pool->getGrey(jidx);
+ color = grey;
+ }
- pool->addImage(jidx, color);
+ pool->addImage(jidx, color);
}
-image_pool::image_pool() {
+image_pool::image_pool()
+{
}
-image_pool::~image_pool() {
- __android_log_print(ANDROID_LOG_INFO, "image_pool", "destructor called");
+image_pool::~image_pool()
+{
+ __android_log_print(ANDROID_LOG_INFO, "image_pool", "destructor called");
}
-cv::Ptr image_pool::getImage(int i) {
- return imagesmap[i];
+Mat image_pool::getImage(int i)
+{
+ return imagesmap[i];
}
-void image_pool::getGrey(int i, Mat & grey) {
-
- cv::Ptr tm = yuvImagesMap[i];
- if (tm.empty())
- return;
- grey = (*tm)(Range(0, tm->rows / 2), Range::all());
+Mat image_pool::getGrey(int i)
+{
+ Mat tm = yuvImagesMap[i];
+ if (tm.empty())
+ return tm;
+ return tm(Range(0, tm.rows * (2.0f/3)), Range::all());
}
-cv::Ptr image_pool::getYUV(int i) {
-
- return yuvImagesMap[i];
-
+Mat image_pool::getYUV(int i)
+{
+ return yuvImagesMap[i];
}
-void image_pool::addYUVMat(int i, cv::Ptr mat) {
-
- yuvImagesMap[i] = mat;
-
+void image_pool::addYUVMat(int i, Mat mat)
+{
+ yuvImagesMap[i] = mat;
}
-void image_pool::addImage(int i, cv::Ptr mat) {
-
- imagesmap[i] = mat;
-
+void image_pool::addImage(int i, Mat mat)
+{
+ imagesmap[i] = mat;
}
diff --git a/android/android-jni/jni/image_pool.h b/android/android-jni/jni/image_pool.h
index bb0ea94455..6882cb63ce 100644
--- a/android/android-jni/jni/image_pool.h
+++ b/android/android-jni/jni/image_pool.h
@@ -1,12 +1,14 @@
-#ifndef IMAGE_POOL_H
-#define IMAGE_POOL_H
+#ifndef IMAGE_POOL_H_ANDROID_KDJFKJ
+#define IMAGE_POOL_H_ANDROID_KDJFKJ
#include
-#include
#include