Ask Your Question
0

OpenCV4Android crashes when initializing Mat image

asked 2013-06-18 11:41:53 -0600

boom10611 gravatar image

updated 2013-06-18 13:35:22 -0600

I'm using OpenCV4Android for a project. I think I've managed to set up the environment correctly, but when the code get's to this line:

image11 = Highgui.imread(filePath1,Highgui.IMREAD_GRAYSCALE);

It crashes and show's this error message:

VM aborting
Fatal signal 11 (SIGSEGV) at 0xdead00d (code=1), thread 1055 (mopencv_flann2)

EDIT: Here's the logcan output:

06-18 14:30:24.610: E/dalvikvm(8151): VM aborting
06-18 14:30:24.610: A/libc(8151): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 8151 (m.opencv_flann2)

I'm completely lost as to how to fix this and would appreciate some help. Here is all the code, with some this excluded:

public class FlannMainActivity extends Activity {   
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(final int status) {
        switch (status) {
        case LoaderCallbackInterface.SUCCESS:{
            _append ="Succelfully loaded OpenCV";
            _text.append(_append);
            image1 = Highgui.imread(filePath1, Highgui.IMREAD_GRAYSCALE);
            image1 = Highgui.imread(filePath2, Highgui.IMREAD_GRAYSCALE);
            descriptors1 = new Mat();
            descriptors2 = new Mat();
            extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
            featureDetector = FeatureDetector.create(FeatureDetector.SURF);
            keyPoint1 = new Vector<KeyPoint>();
            keyPoint2 = new Vector<KeyPoint>();
            matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
            matches = new Vector<DMatch>();
            goodMatches = new Vector<DMatch>();
            imgMatches = new Mat();

            if( !image1.empty() || !image2.empty()){
                _append = "ERROR: Cannot read an image";
                _text.append(_append);
            }

            List<Mat> imageMat = new Vector<Mat>();
            imageMat.add(image1);
            imageMat.add(image2);
            featureDetector.detect(imageMat, (List<MatOfKeyPoint>)(List<?>)keyPoint1);
            image1 = imageMat.get(0);
            image2 = imageMat.get(1);

            //Calculate descriptors (feature vectors)
            _append = "Calculate descriptors";
            _text.append(_append);
            List<Mat> discriptorMat = new Vector<Mat>();
            discriptorMat.add(descriptors1);
            discriptorMat.add(descriptors2);

            extractor.compute(imageMat, (List<MatOfKeyPoint>)(List<?>) keyPoint1, discriptorMat);

            //Matching descriptor vectors using FLANN matcher
            _append = "Matching descriptor vectors using FLANN matcher\n";
            _text.append(_append);
            double maxDist = 0, minDist = 100;

            matcher.radiusMatch(descriptors1, descriptors2, (List<MatOfDMatch>)(List<?>)matches, (float)maxDist);

            //Quick calculation of max and min distances between keypoints
            for(int i = 0; i < descriptors1.rows(); i++){
                final double dist = matches.get(i).distance;
                if(dist < minDist) minDist = dist;
                if(dist > maxDist) maxDist = dist;
            }

            _append = "-- Max dist: " + maxDist;
            _text.append(_append);
            _append = "-- Max dist: " + minDist;
            _text.append(_append);

            // Draw only "good" matches (i.e. whose distance is less than 2*minDist)
            // PS - radiusMatch can alco be used here

            for(int i = 0; i < descriptors1.rows(); i++) {
                if(matches.get(i).distance < 2*minDist) {
                    goodMatches.add(matches.get(i));
                }
            }

            //Draw only "good" matches
            final List<Character> m = new Vector<Character>();

            Features2d.drawMatches(image1, (MatOfKeyPoint) keyPoint1, image2, (MatOfKeyPoint) keyPoint2,
                    (MatOfDMatch) goodMatches, imgMatches, Scalar.all(-1), Scalar.all(-1),
                    (MatOfByte) m, Features2d.NOT_DRAW_SINGLE_POINTS);

            for(int i = 0; i < goodMatches.size(); i++){
                _append = "Good Match " + i + " Keypoint 1: " + goodMatches.get(i).queryIdx + " -- Keypoint 2 " + goodMatches.get(i).trainIdx + "\n";
                _text.append(_append);
            }
        }
        break;
        default: {
            super.onManagerConnected(status);
        }break;
        }
    }
}; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flann_main);

    if((!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback))) {
        _append = "Cannot connect to Open\n";
        _text.append(_append);
    }       

}
edit retag flag offensive close merge delete

Comments

Please, post logcat output of this crash.

Daniil Osokin gravatar imageDaniil Osokin ( 2013-06-18 13:15:28 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-06-18 20:31:19 -0600

Are you certain that filePath1 is correct? I don't have much experience with android, but it looks like you are trying to access something that either does not exist or you don't have permissions to access. If you are reading your image from an SD card, for example, you need to add READ_EXTERNAL_STORAGE permission to your manifest.

Also, this has nothing to do with your error, but when you load the images:

image1 = Highgui.imread(filePath1, Highgui.IMREAD_GRAYSCALE);
image1 = Highgui.imread(filePath2, Highgui.IMREAD_GRAYSCALE);

I guess you mean:

image1 = Highgui.imread(filePath1, Highgui.IMREAD_GRAYSCALE);
image2 = Highgui.imread(filePath2, Highgui.IMREAD_GRAYSCALE);
edit flag offensive delete link more

Comments

I found that I hadn't initialized my filePath1 string to the directory so I suppose that was the problem. I also added in the permission just in case. Thanks.

boom10611 gravatar imageboom10611 ( 2013-06-18 21:24:09 -0600 )edit

Question Tools

Stats

Asked: 2013-06-18 11:41:53 -0600

Seen: 1,839 times

Last updated: Jun 18 '13