I'm building a project in Java and I have C++ and header file for image processing that I want to use.
Their name: "SPImageProc.cpp" and "SPImageProc.h".
So I used JNI by the instructions in this tutorial:
https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html#zz-2.6
(I'm using only the part: "2.6 JNI in Eclipse").
This tutorial instruct me to create the header file and the C++ file.
Because of that, I'm copying the original C++ and header file to the C++ file that I created (that has the same name as the original- "SPImageProc.cpp").
Now one of the lines that I tried to copy are lines that use opencv:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
So I need to configure opencv .
In the tutorial that I found for doing that, one of the instructions was:
Go to “Project -> Properties -> C/C++ Build -> Settings” And click on >“Includes”
And in the screenshot, "Includes" is in "tool settings" tab.
But I don't have this tab.
The solution in this post:
Can't find the tool settings in Eclipse CDT
was to check "Generate Makefiles automatically".
But I'm afraid that it will cause me problems. Especially because I'm using JNI. And because I'm using CDT as a plugin for Eclipse Java.
And I don't know the effects of checking that, so I won't know how to solve the problems that it will create (if it will).
Development Environment
+Eclipse IDE for Java Developers (32 bit) Version: Kepler Service Release 2.
+CDT plugin for Eclipse
+Windows 10 64-bit (I use eclipse 32-bit because at some point, the 64-bit eclipse couldn't open and the solution was to use 32-bit eclipse)
The original C++ file that I want to use
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <cstdio>
#include "SPImageProc.h"
extern "C" {
#include "SPLogger.h"
}
using namespace cv;
using namespace std;
#define PCA_MEAN_STR "mean"
#define PCA_EIGEN_VEC_STR "e_vectors"
#define PCA_EIGEN_VAL_STR "e_values"
#define STRING_LENGTH 1024
#define WARNING_MSG_LENGTH 2048
#define GENERAL_ERROR_MSG "An error occurred"
#define PCA_DIM_ERROR_MSG "PCA dimension couldn't be resolved"
#define PCA_FILE_NOT_EXIST "PCA file doesn't exist"
#define PCA_FILE_NOT_RESOLVED "PCA filename couldn't be resolved"
#define NUM_OF_IMAGES_ERROR "Number of images couldn't be resolved"
#define NUM_OF_FEATS_ERROR "Number of features couldn't be resolved"
#define MINIMAL_GUI_ERROR "Minimal GUI mode couldn't be resolved"
#define IMAGE_PATH_ERROR "Image path couldn't be resolved"
#define IMAGE_NOT_EXIST_MSG ": Images doesn't exist"
#define MINIMAL_GUI_NOT_SET_WARNING "Cannot display images in non-Minimal-GUI mode"
#define ALLOC_ERROR_MSG "Allocation error"
#define INVALID_ARG_ERROR "Invalid arguments"
void sp::ImageProc::initFromConfig(const SPConfig config) {
SP_CONFIG_MSG msg = SP_CONFIG_SUCCESS;
pcaDim = spConfigGetPCADim(config, &msg);
if (msg != SP_CONFIG_SUCCESS) {
spLoggerPrintError(PCA_DIM_ERROR_MSG, __FILE__, __func__, __LINE__);
throw Exception();
}
numOfImages = spConfigGetNumOfImages(config, &msg);
if (msg != SP_CONFIG_SUCCESS) {
spLoggerPrintError(NUM_OF_IMAGES_ERROR, __FILE__, __func__, __LINE__);
throw Exception();
}
numOfFeatures = spConfigGetNumOfFeatures(config, &msg);
if (msg != SP_CONFIG_SUCCESS) {
spLoggerPrintError(NUM_OF_FEATS_ERROR, __FILE__, __func__, __LINE__);
throw Exception();
}
minimalGui = spConfigMinimalGui(config, &msg);
if (msg != SP_CONFIG_SUCCESS) {
spLoggerPrintError(MINIMAL_GUI_ERROR, __FILE__, __func__, __LINE__);
throw Exception();
}
}
void sp::ImageProc::getImagesMat(vector<Mat>& images, const SPConfig config) {
char warningMSG[WARNING_MSG_LENGTH] = { '\0' };
for (int i = 0; i < numOfImages; i++) {
char imagePath[STRING_LENGTH + 1] = { '\0' };
if (spConfigGetImagePath(imagePath, config, i) != SP_CONFIG_SUCCESS) {
spLoggerPrintError(IMAGE_PATH_ERROR, __FILE__, __func__, __LINE__);
throw Exception();
}
Mat img = imread(imagePath, IMREAD_GRAYSCALE);
if (img.empty()) {
sprintf(warningMSG, "%s %s", imagePath, IMAGE_NOT_EXIST_MSG);
spLoggerPrintWarning(warningMSG, __FILE__, __func__, __LINE__);
continue;
}
images.push_back(img);
}
}
void sp::ImageProc::getFeatures(vector<Mat>& images, Mat& features) {
//To store the keypoints that will be extracted by SIFT
vector<KeyPoint> keypoints;
//To store the SIFT descriptor of current image
Mat descriptor;
//To store all the descriptors that are extracted from all the images.
//The SIFT feature extractor and descriptor
Ptr<xfeatures2d::SiftDescriptorExtractor> detector =
xfeatures2d::SIFT::create(numOfFeatures);
//feature descriptors and build the vocabulary
for (int i = 0; i < static_cast<int>(images.size()); i++) {
//detect feature points
detector->detect(images[i], keypoints);
//compute the descriptors for each keypoint
detector->compute(images[i], keypoints, descriptor);
//put the all feature descriptors in a single Mat object
features.push_back(descriptor);
}
}
void sp::ImageProc::preprocess(const SPConfig config) {
try {
vector<Mat> images;
Mat features;
char pcaPath[STRING_LENGTH + 1] = { '\0' };
getImagesMat(images, config);
getFeatures(images, features);
pca = PCA(features, Mat(), CV_PCA_DATA_AS_ROW, pcaDim);
if (spConfigGetPCAPath(pcaPath, config) != SP_CONFIG_SUCCESS) {
spLoggerPrintError(PCA_FILE_NOT_RESOLVED, __FILE__, __func__,
__LINE__);
throw Exception();
}
FileStorage fs(pcaPath, FileStorage::WRITE);
fs << PCA_EIGEN_VEC_STR << pca.eigenvectors;
fs << PCA_EIGEN_VAL_STR << pca.eigenvalues;
fs << PCA_MEAN_STR << pca.mean;
fs.release();
} catch (...) {
spLoggerPrintError(GENERAL_ERROR_MSG, __FILE__, __func__, __LINE__);
throw Exception();
}
}
void sp::ImageProc::initPCAFromFile(const SPConfig config) {
if (!config) {
spLoggerPrintError(GENERAL_ERROR_MSG, __FILE__, __func__, __LINE__);
throw Exception();
}
char pcaFilename[STRING_LENGTH + 1] = { '\0' };
if (spConfigGetPCAPath(pcaFilename, config) != SP_CONFIG_SUCCESS) {
spLoggerPrintError(PCA_FILE_NOT_RESOLVED, __FILE__, __func__, __LINE__);
throw Exception();
}
FileStorage fs(pcaFilename, FileStorage::READ);
if (!fs.isOpened()) {
spLoggerPrintError(PCA_FILE_NOT_EXIST, __FILE__, __func__, __LINE__);
throw Exception();
}
fs[PCA_EIGEN_VEC_STR] >> pca.eigenvectors;
fs[PCA_EIGEN_VAL_STR] >> pca.eigenvalues;
fs[PCA_MEAN_STR] >> pca.mean;
fs.release();
}
sp::ImageProc::ImageProc(const SPConfig config) {
try {
if (!config) {
spLoggerPrintError(INVALID_ARG_ERROR, __FILE__, __func__, __LINE__);
throw Exception();
}
SP_CONFIG_MSG msg;
bool preprocMode = false;
initFromConfig(config);
if ((preprocMode = spConfigIsExtractionMode(config, &msg))) {
preprocess(config);
} else {
initPCAFromFile(config);
}
} catch (...) {
spLoggerPrintError(GENERAL_ERROR_MSG, __FILE__, __func__, __LINE__);
throw Exception();
}
}
SPPoint* sp::ImageProc::getImageFeatures(const char* imagePath, int index,
int* numOfFeats) {
vector<KeyPoint> keypoints;
Mat descriptor, img, points;
double* pcaSift = NULL;
char errorMSG[STRING_LENGTH * 2];
Ptr<xfeatures2d::SiftDescriptorExtractor> detector;
if (!imagePath || !numOfFeats) {
spLoggerPrintError(INVALID_ARG_ERROR, __FILE__, __func__, __LINE__);
return NULL;
}
img = imread(imagePath, IMREAD_GRAYSCALE);
if (img.empty()) {
sprintf(errorMSG, "%s %s", imagePath, IMAGE_NOT_EXIST_MSG);
spLoggerPrintError(errorMSG, __FILE__, __func__, __LINE__);
return NULL;
}
detector = xfeatures2d::SIFT::create(numOfFeatures);
detector->detect(img, keypoints);
detector->compute(img, keypoints, descriptor);
points = pca.project(descriptor);
pcaSift = (double*) malloc(sizeof(double) * pcaDim);
if (!pcaSift) {
spLoggerPrintError(ALLOC_ERROR_MSG, __FILE__, __func__, __LINE__);
return NULL;
}
*numOfFeats = points.rows;
SPPoint* resPoints = (SPPoint*) malloc(sizeof(*resPoints) * points.rows);
if (!resPoints) {
free(pcaSift);
spLoggerPrintError(ALLOC_ERROR_MSG, __FILE__, __func__, __LINE__);
return NULL;
}
for (int i = 0; i < points.rows; i++) {
for (int j = 0; j < points.cols; j++) {
pcaSift[j] = (double) points.at<float>(i, j);
}
resPoints[i] = spPointCreate(pcaSift, pcaDim, index);
}
free(pcaSift);
return resPoints;
}
void sp::ImageProc::showImage(const char* imgPath) {
if (minimalGui) {
Mat img = imread(imgPath, cv::IMREAD_COLOR);
if (img.empty()) {
spLoggerPrintWarning(IMAGE_NOT_EXIST_MSG, __FILE__, __func__,
__LINE__);
return;
}
imshow(windowName, img);
waitKey(0);
destroyAllWindows();
} else {
spLoggerPrintWarning(MINIMAL_GUI_NOT_SET_WARNING, __FILE__, __func__,
__LINE__);
}
}
The original header file that I want to use
#ifndef SPIMAGEPROC_H_
#define SPIMAGEPROC_H_
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <vector>
extern "C" {
#include "SPConfig.h"
#include "SPPoint.h"
}
namespace sp {
/**
* A class which supports different image processing functionalites.
*/
class ImageProc {
private:
const char* windowName = "Software Project CBIR";
int pcaDim;
int numOfImages;
int numOfFeatures;
cv::PCA pca;
bool minimalGui;
void initFromConfig(const SPConfig);
void getImagesMat(std::vector<cv::Mat>&, const SPConfig);
void getFeatures(std::vector<cv::Mat>&,
cv::Mat&);
void preprocess(const SPConfig config);
void initPCAFromFile(const SPConfig config);
public:
/**
* Creates a new object for the purpose of image processing based
* on the configuration file.
* @param config - the configuration file from which the object is created
*/
ImageProc(const SPConfig config);
/**
* Returns an array of features for the image imagePath. All SPPoint elements
* will have the index given by index. The actual number of features extracted
* for this image will be stored in the pointer given by numOfFeats.
*
* @param imagePath - the target imagePath
* @param index - the index of the image in the database
* @param numOfFeats - a pointer in which the actual number of feats extracted
* will be stored
* @return
* An array of the actual features extracted. NULL is returned in case of
* an error.
*/
SPPoint* getImageFeatures(const char* imagePath,int index,int* numOfFeats);
/**
* Displays the image given by imagePath. Notice that this function works
* only in MinimalGUI mode (otherwise a warnning message is printed).
*
* @param imagePath - the path of the image to be displayed
*/
void showImage(const char* imagePath);
};
}
#endif
The C++ file that I created (by the jni tutorial)
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <jni.h>
#include <stdio.h>
#include "SPImageProc.h"
JNIEXPORT void JNICALL Java_SPImageProc_cppFunc(JNIEnv *env, jobject thisObj) {
printf("After adding include in the cpp file !\n");
return;
}
The header file that was created by my makefile
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class SPImageProc */
#ifndef _Included_SPImageProc
#define _Included_SPImageProc
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: SPImageProc
* Method: cppFunc
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_SPImageProc_cppFunc
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
makefile
# Define a variable for classpath
CLASS_PATH = ../bin
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : spimageproc.dll
# $@ matches the target, $< matches the first dependency
spimageproc.dll : SPImageProc.o
g++ -Wl,--add-stdcall-alias -shared -o $@ $<
# $@ matches the target, $< matches the first dependency
SPImageProc.o : SPImageProc.cpp SPImageProc.h
g++ -I"C:\Program Files (x86)\Java\jdk1.8.0_212\include" -I"C:\Program Files (x86)\Java\jdk1.8.0_212\include\win32" -c $< -o $@
# $* matches the target filename without the extension
SPImageProc.h : SPImageProc.class
javah -classpath $(CLASS_PATH) $*
clean :
rm SPImageProc.h SPImageProc.o spimageproc.dll
SPImageProc.java
public class SPImageProc {
static {
System.loadLibrary("spimageproc"); // spimageproc.dll
}
// Declare native method
private native void cppFunc();
public static void function() {
new SPImageProc().cppFunc(); // Allocate an instance and invoke the native
// method
}
}
CBIR.java
public class CBIR {
public static void main(String[] args) {
SPImageProc.function();
}
}