我正在尝试为Android编写图像拼接应用。我正在使用Android NDK作为OpenCV的本地部分。有3种不应该发生的行为,我会喜欢任何解释为什么他们会发生。需要解释OpenCV Android行为
只有一些图像(来自设备/相同分辨率的同一摄像头)不会崩溃。它发生崩溃时的错误低于我的C++代码。
图像针迹的结果看起来像只是一个图像。 (我在20%的时间里得到这个结果,而另外80%的时间则崩溃)。我认为这与for循环中的调整行大小有关。书中的例子将列和行分成10列。当我这样做时,图像只是稍微桶了一点,但非常非常像素化。同样,在这种情况下,它看起来只有一个图像。
如果我不设定拼接设置是这样的:
stitcher.setRegistrationResol(-1); /// 0.6 stitcher.setSeamEstimationResol(-1); /// 0.1 stitcher.setCompositingResol(-1); //1 stitcher.setPanoConfidenceThresh(-1); //1 stitcher.setWaveCorrection(true); stitcher.setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);
输出图像将是空的。这对我来说很奇怪,因为书中的例子在没有它们的情况下运行良好。
我一直在使用第6章this book作为我项目的C++部分。这里是我的C++代码:
#include <jni.h>
#include "aaron_picstitch_MyNDK.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/stitching/stitcher.hpp>
#include <opencv2/core/mat.hpp>
#include <vector>
#include <android/log.h>
using namespace std;
using namespace cv;
char FILEPATH[100] = "/storage/emulated/0/PicStitch/cppResult.jpg";
//char FILEPATH1[100] = "/storage/emulated/0/PicStitch/cppTesta.jpg";
//char FILEPATH2[100] = "/storage/emulated/0/PicStitch/cppTestb.jpg";
JNIEXPORT void JNICALL Java_aaron_picstitch_CameraActivity_stitchImages(JNIEnv *env, jobject , jobjectArray images, jint size, jlong panoAddr)
{
vector <Mat> imgs = vector<Mat>();
Mat pano = Mat();
Mat temp = Mat();
Mat &srcRes = *(Mat *)panoAddr, img;
jclass clazz = (env)->FindClass("org/opencv/core/Mat");
jmethodID getNativeObjAddr = (env)->GetMethodID(clazz, "getNativeObjAddr", "()J");
__android_log_print(ANDROID_LOG_VERBOSE, "IN CPP TESTTEST", "ADDR: %lld", panoAddr);
int i = 0;
for (i = 0; i < size; i++)
{
jobject obj = (env->GetObjectArrayElement(images, i));
jlong result = (env)->CallLongMethod(obj, getNativeObjAddr, NULL);
img = *(Mat *)result;
resize(img, temp, Size(img.rows/2, img.cols/2));
imgs.push_back(temp);
env->DeleteLocalRef(obj);
}
env->DeleteLocalRef(images);
Stitcher stitcher = Stitcher::createDefault();
__android_log_print(ANDROID_LOG_VERBOSE, "IN CPP", "HERE 1 temp rows is: %d", temp.rows);
stitcher.setRegistrationResol(-1); /// 0.6
stitcher.setSeamEstimationResol(-1); /// 0.1
stitcher.setCompositingResol(-1); //1
stitcher.setPanoConfidenceThresh(-1); //1
stitcher.setWaveCorrection(true);
stitcher.setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);
__android_log_print(ANDROID_LOG_VERBOSE, "IN CPP", "Right before .stitch");
Stitcher::Status status = stitcher.stitch(imgs, pano);
__android_log_print(ANDROID_LOG_VERBOSE, "IN CPP", "HERE 2 Pano rows is : %d", pano.rows);
if (status == Stitcher::OK)
{
__android_log_print(ANDROID_LOG_VERBOSE, "IN CPP", "STITCHING SHOULD WORK");
}
//pano.copyTo(srcRes);
imwrite(FILEPATH, pano);
}
这里是子弹#1的错误:
04-22 20:51:47.192 32115-32651/aaron.picstitch E/cv::error(): OpenCVError Assertion failed (s >= 0) in void cv::setSize(cv::Mat&, int, int const*, const size_t*, bool), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix.cpp, line 116
04-22 20:51:47.192 32115-32651/aaron.picstitch A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 32651 (AsyncTask #1)
另一个奇怪的问题是没有那么大的一个问题是,我使用getNativeObjAddr()为该项目的Java部分中的对象,因此我可以将结果放入其中,但每次尝试访问它时都会遇到段错误。不知道为什么会发生,但这个问题可以解决。
有关我的问题的任何想法,赞赏!
请检查链接无效。 –