Android Opencv为什么HOG描述符总是为零?

时间:2016-07-06 21:04:53

标签: java android opencv feature-detection

我在这个问题上堆叠了好几天。我想创建一个Android应用程序拍摄照片并提取该图像的HOG功能以供将来处理。问题是下面的代码总是返回带有rezo值的HOG descriptors

@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 个答案:

答案 0 :(得分:1)

问题出在图书馆内部。当我使用OpenCV 2.4.13 for Linux而不是Android执行相同的代码时,代码按预期工作得很好。所以我希望他们能解决OpenCV4Android HOGDescriptor的任何问题。