【问题标题】:No "tool settings" in EclipseEclipse 中没有“工具设置”
【发布时间】:2019-05-14 01:00:50
【问题描述】:

我正在用 Java 构建一个项目,并且我有 C++ 和我想使用的用于图像处理的头文件。

他们的名字:“SPImageProc.cpp”和“SPImageProc.h”。

所以我按照本教程中的说明使用了 JNI:

https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html#zz-2.6

(我只使用部分:“2.6 JNI in Eclipse”)。

本教程指导我创建头文件和 C++ 文件。

因此,我将原始 C++ 和头文件复制到我创建的 C++ 文件(与原始文件同名 - “SPImageProc.cpp”)。

现在我尝试复制的行之一是使用 opencv 的行:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>

所以我需要配置 opencv 。

在我找到的用于执行此操作的教程中,其中一条说明是:

转到“项目 -> 属性 -> C/C++ 构建 -> 设置”,然后点击 >“包含”

在屏幕截图中,“包含”位于“工具设置”选项卡中。

但我没有这个标签。

本帖中的解决方案:

Can't find the tool settings in Eclipse CDT

是检查“自动生成 Makefiles”。

但我担心这会给我带来麻烦。 特别是因为我使用的是 JNI。因为我使用 CDT 作为 Eclipse Java 的插件。

而且我不知道检查的效果,所以我不知道如何解决它会产生的问题(如果会的话)。

开发环境

+Eclipse IDE for Java Developers(32 位)版本:Kepler Service Release 2。

+Eclipse 的 CDT 插件

+Windows 10 64 位(我使用 eclipse 32 位,因为在某些时候,64 位 eclipse 无法打开,解决方案是使用 32 位 eclipse)

我要使用的原始 C++ 文件

#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__);
    }

}


我要使用的原始头文件

#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

我创建的 C++ 文件(通过 jni 教程)

#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;
}

我的 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

制作文件

# 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();
   }

}

【问题讨论】:

    标签: eclipse opencv java-native-interface windows-10


    【解决方案1】:

    使用 Eclipse CDT,您可以选择:

    • 让 Eclipse 管理您的 makefile
    • 自己编写 makefile

    第一个选项为您提供 ToolSettings 对话框(除其他外),您可以在其中输入设置,CDT 会将它们转换为相应的编译器和链接器选项。因此,如果您添加包含路径,CDT 会添加一个-I 编译器选项。

    如果您选择自己编写 makefile,就是这样。您必须自己将-I&lt;your path&gt; 添加到makefile 中(在以g++ 开头的行中已经包含其他内容)。

    不知道将您的项目转换为 CDT 托管项目是否会读取您现有的 makefile 并保留其设置,但您始终可以备份文件并尝试一下。

    【讨论】:

    • 我试过“自动生成makefile”,它解决了与opencv相关的错误,但给了我错误:“jni.h:没有这样的文件或目录”。我注意到,当我单击“自动生成 makefile”时,发生了 3 件事:1. 检查了“扩展 ENv。Makefile 中的变量引用”。 2.“构建目录”更改为“${workspace_loc:/HelloJNI}/Default”。 3.“建造者类型”没有变灰
    • 您的 makefile 已经包含 JNI 的包含路径。如果您切换到 CDT 管理的 makefile,您也必须在工具设置中添加它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 2012-11-06
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多