2016-04-23 47 views
1

我正在嘗試爲Android編寫圖像拼接應用。我正在使用Android NDK作爲OpenCV的本地部分。有3種不應該發生的行爲,我會喜歡任何解釋爲什麼他們會發生。需要解釋OpenCV Android行爲

  1. 只有一些圖像(來自設備/相同分辨率的同一攝像頭)不會崩潰。它發生崩潰時的錯誤低於我的C++代碼。

  2. 圖像針跡的結果看起來像只是一個圖像。 (我在20%的時間裏得到這個結果,而另外80%的時間則崩潰)。我認爲這與for循環中的調整行大小有關。書中的例子將列和行分成1​​0列。當我這樣做時,圖像只是稍微桶了一點,但非常非常像素化。同樣,在這種情況下,它看起來只有一個圖像。

  3. 如果我不設定拼接設置是這樣的:

    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部分中的對象,因此我可以將結果放入其中,但每次嘗試訪問它時都會遇到段錯誤。不知道爲什麼會發生,但這個問題可以解決。

有關我的問題的任何想法,讚賞!

回答

0

沒有找出我的問題的解決方案。相反,我用this example的代碼重寫了很多代碼,然後神奇地工作。我仍然不知道什麼是錯,但至少我有工作代碼。

+0

請檢查鏈接無效。 –