2016-07-06 107 views
0

我與這個問題堆棧了幾天。我想製作一個android應用程序,用於拍攝照片並提取該圖像的HOG特徵以供將來處理。問題是,下面的代碼總是返回帶有rezo值的HOG descriptorsAndroid Opencv爲什麼HOG描述符總是爲零?

@Override 
public void onPictureTaken(byte[] data, Camera camera) { 
    Log.i(TAG, "Saving a bitmap to file"); 
    // The camera preview was automatically stopped. Start it again. 
    mCamera.startPreview(); 
    mCamera.setPreviewCallback(this); 
    this.disableView(); 
    Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, 0, data.length); 
    myImage = new Mat(bitmapPicture.getWidth(), bitmapPicture.getHeight(), CvType.CV_8UC1); 
    Utils.bitmapToMat(bitmapPicture, myImage); 
    Bitmap bm = Bitmap.createBitmap(myImage.cols(), myImage.rows(),Bitmap.Config.ARGB_8888); 
    Utils.matToBitmap(myImage.clone(), bm); 
    // find the imageview and draw it! 
    ImageView iv = (ImageView) getRootView().findViewById(R.id.imageView); 
    this.setVisibility(SurfaceView.GONE); 
    iv.setVisibility(ImageView.VISIBLE); 

    Mat forHOGim = new Mat(); 
    org.opencv.core.Size sz = new org.opencv.core.Size(64,128); 
    Imgproc.resize(myImage, myImage, sz); 
    Imgproc.cvtColor(myImage,forHOGim,Imgproc.COLOR_RGB2GRAY); 
    //forHOGim = myImage.clone(); 
    MatOfFloat descriptors = new MatOfFloat(); //an empty vector of descriptors 
    org.opencv.core.Size winStride = new org.opencv.core.Size(64/2,128/2); //50% overlap in the sliding window 
    org.opencv.core.Size padding = new org.opencv.core.Size(0,0); //no padding around the image 
    MatOfPoint locations = new MatOfPoint(); ////an empty vector of locations, so perform full search 
    //HOGDescriptor hog = new HOGDescriptor(); 
    HOGDescriptor hog = new HOGDescriptor(sz,new org.opencv.core.Size(16,16),new org.opencv.core.Size(8,8),new org.opencv.core.Size(8,8),9); 
    Log.i(TAG,"Constructed"); 
    hog.compute(forHOGim , descriptors, new org.opencv.core.Size(16,16), padding, locations); 
    Log.i(TAG,"Computed"); 
    Log.i(TAG,String.valueOf(hog.getDescriptorSize())+" "+descriptors.size()); 
    Log.i(TAG,String.valueOf(descriptors.get(12,0)[0])); 
    double dd=0.0; 
    for (int i=0;i<3780;i++){ 
     if (descriptors.get(i,0)[0]!=dd) Log.i(TAG,"NOT ZERO"); 
    } 

    Bitmap bm2 = Bitmap.createBitmap(forHOGim.cols(), forHOGim.rows(),Bitmap.Config.ARGB_8888); 
    Utils.matToBitmap(forHOGim,bm2); 
    iv.setImageBitmap(bm2); 
} 

所以在logcat中我從來沒有得到NOT ZERO消息。問題是,無論我對這段代碼做什麼修改,我總是在descriptors MatOfFloat中有零...而奇怪的部分是,如果我取消註釋HOGDescriptor hog = new HOGDescriptor();並使用那個而不是我現在使用的那個,我的應用程序就會崩潰。 .. 其餘的代碼運行良好,圖片總是採取和顯示在我的圖像視圖,如我所料。

任何幫助將不勝感激。 在此先感謝。

回答

1

問題出在圖書館裏面。當我使用OpenCV 2.4.13 for Linux而不是Android來執行相同的代碼時,代碼運行得非常好。所以我希望他們能解決OpenCV4Android的HOGDescriptor的任何問題。