问题描述
我正在使用Opencv和android Ndk开发图像处理应用程序。 首先,我在Visual Studio中编写了代码,没有问题。 我有标题roi.hpp和roi.cpp:roi.hpp:
#ifndef ROI
#define ROI
#include <..\..\imgproc.hpp>
#include <..\..\opencv.hpp>
using namespace cv;
class My_ROI{
public:
My_ROI();
My_ROI(const My_ROI &my_roi);
My_ROI(Point upper_corner, Point lower_corner,Mat src);
Point upper_corner, lower_corner;
Mat roi_ptr;
Scalar color;
int border_thickness;
void draw_rectangle(Mat src);
};
#endif
roi.cpp:
#include "myImage.hpp"
#include <..\..\imgproc.hpp>
#include <..\..\opencv.hpp>
#include <..\..\highgui.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "roi.hpp"
using namespace cv;
using namespace std;
My_ROI::My_ROI(const My_ROI &my_roi)
{
int i;
// allocate variables
My_ROI();
// copy values
operator = (my_roi);
}
My_ROI::My_ROI(){
upper_corner=Point(0,0);
lower_corner=Point(0,0);
}
My_ROI::My_ROI(Point u_corner, Point l_corner, Mat src){
upper_corner=u_corner;
lower_corner=l_corner;
color=Scalar(0,255,0);
border_thickness=2;
roi_ptr=src(Rect(u_corner.x, u_corner.y, l_corner.x- u_corner.x,l_corner.y- u_corner.y));
}
void My_ROI::draw_rectangle(Mat src){
rectangle(src,upper_corner,lower_corner,color,border_thickness);
}
在另一个类中,我制作了一个My_ROI的向量,并可以毫无问题地使用它,就像这样:
vector <My_ROI> roi;
for (int j = 0; j<NSAMPLES; j++){
roi[j].draw_rectangle(m->src);
}
然后我将项目移植到android,我使用roi.hpp和roi.cpp作为外部c ++文件,这是我的android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES := on
OPENCV_INSTALL_MODULES := on
OPENCV_LIB_TYPE:=STATIC
include ../../jni/OpenCV.mk
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.cpp
LOCAL_SRC_FILES += ../../external_cpp/makegray.cpp
LOCAL_SRC_FILES += ../../external_cpp/roi.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external_cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8
最后在hello-jni.cpp文件中,我要使用roi的向量:
#include <jni.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <opencv2/opencv.hpp>
#include "../../external_cpp/roi.hpp"
vector <My_ROI> roi;
void fillROIArray(MyImage *m,Mat& mRgb,Mat& mGray)
{
m->src=mRgb;
flip(m->src,m->src,1);//reverse the order of rows or cols in a matrix
// My_ROI *f1 = new My_ROI;
// roi.push_back(*f1);
roi.push_back(My_ROI(Point(m->src.cols/3, m->src.rows/6),Point(m->src.cols/3+square_len,m->src.rows/6+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/4, m->src.rows/2),Point(m->src.cols/4+square_len,m->src.rows/2+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/3, m->src.rows/1.5),Point(m->src.cols/3+square_len,m->src.rows/1.5+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/2, m->src.rows/2),Point(m->src.cols/2+square_len,m->src.rows/2+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/2.5, m->src.rows/2.5),Point(m->src.cols/2.5+square_len,m->src.rows/2.5+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/2, m->src.rows/1.5),Point(m- >src.cols/2+square_len,m->src.rows/1.5+square_len),m->src));
roi.push_back(My_ROI(Point(m->src.cols/2.5, m->src.rows/1.8),Point(m->src.cols/2.5+square_len,m->src.rows/1.8+square_len),m->src));
mGray=m->src;
}
void myMethod(Image &m)
{
for (int j = 0; j<NSAMPLES; j++){
roi[j].draw_rectangle(m->src);
}
}
但这会导致错误:
method draw_rectangle(...) couldn't be resolved!
我可以访问我的roi对象的所有成员,但是当我创建此类的向量时,我无权访问其成员! 我搜索了很多有关制作自定义类的向量的信息,但是几天来我一直在努力解决这个问题,但我做不到。
任何帮助都非常感谢。 问候。
1楼
终于我找到了解决方案……也许对其他人有用。 我可以像这样访问vector的成员:
for (int j = 0; j<NSAMPLES; j++){
((My_ROI)roi[j]).draw_rectangle(m->src);
}
我现在可以从任何自定义类访问任何矢量对象! :)