openCV cvSaveImage and cvCvtColor crash on android

asked 2013-08-07 15:18:07 -0600

One_pow gravatar image

updated 2013-08-08 13:39:27 -0600

I'm working on android with version 2.3.5 and 4.0.4 both version crash on the execution of the same code. I have been trying to get a frame from a video, save it, and then convert it to HSV.

(I posted this same question on stackoverflow here).

This is my code and the error I get.

  public void process(){
        IplImage orgImg = this.getFrame(2);
                cvSaveImage(Environment.getExternalStorageDirectory().toString() + "/openCV/orgimg.jpg", orgImg);
                IplImage hsv = hsv(orgImg);
    }

    private IplImage getFrame(int id){
            File testfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + videoFile);

            if(testfile.canRead()){ 

                FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(testfile);  
                try {
                    grabber.start();
                    grabber.setFrameNumber(id);
                    final int height=grabber.getImageHeight();
                    final int width=grabber.getImageWidth();
                    IplImage frame = IplImage.create(width, height, 8, 4);
                    frame = grabber.grab();
                    //grabber.stop();
                    return frame;
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return null;
        }

private IplImage hsv(IplImage orgImg) {
        // 8-bit, 3- color =(RGB)
        IplImage imgHSV = cvCreateImage(cvGetSize(orgImg), 8, 3);
        Log.e(TAG,"hsvThreshold - Image size" + cvGetSize(orgImg));
        cvCvtColor(orgImg, imgHSV, CV_BGR2HSV);

        return imgHSV;
    }

Here is the logcat I get when cvSaveImage or cvCvtColor is executed on the device running 4.0.4

08-07 12:26:25.974: I/dalvikvm(6719): "main" prio=5 tid=1 NATIVE
08-07 12:26:25.974: I/dalvikvm(6719):   | group="main" sCount=0 dsCount=0 obj=0x40bf1460 self=0x101aca8
08-07 12:26:25.974: I/dalvikvm(6719):   | sysTid=6719 nice=0 sched=0/0 cgrp=default handle=1074673032
08-07 12:26:25.974: I/dalvikvm(6719):   | schedstat=( 0 0 0 ) utm=710 stm=34 core=0
08-07 12:26:25.974: I/dalvikvm(6719):   at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:26:25.974: I/dalvikvm(6719):   at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:26:25.974: I/dalvikvm(6719):   at java.lang.Runtime.loadLibrary(Runtime.java:368)
08-07 12:26:25.974: I/dalvikvm(6719):   at java.lang.System.loadLibrary(System.java:535)
08-07 12:26:25.974: I/dalvikvm(6719):   at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:593)
08-07 12:26:25.974: I/dalvikvm(6719):   at com.googlecode.javacpp.Loader.load(Loader.java:489)
08-07 12:26:25.974: I/dalvikvm(6719):   at com.googlecode.javacpp.Loader.load(Loader.java:431)
08-07 12:26:25.974: I/dalvikvm(6719):   at com.googlecode.javacv.cpp.opencv_imgproc.<clinit>(opencv_imgproc.java:97)
08-07 12:26:25.974: I/dalvikvm(6719):   at java.lang.Class.classForName(Native Method)
08-07 12:26:25.974: I/dalvikvm(6719):   at java.lang.Class.forName(Class.java:217)
08-07 12:26:25.974: I/dalvikvm(6719):   at com.googlecode.javacpp.Loader.load(Loader.java:453)
08-07 12:26:25.974: I/dalvikvm(6719):   at com.googlecode.javacv.cpp.opencv_highgui.<clinit>(opencv_highgui.java:85)
08-07 12:26:25.974: I/dalvikvm(6719):   at ch.golfer.TrackSpot.process(TrackSpot.java:-1)
08-07 12:26:25.974: I/dalvikvm(6719):   at ch.golfer.FullscreenActivity$7.onClick(FullscreenActivity.java:196)
08-07 12:26:25.974: I/dalvikvm(6719):   at ...
(more)
edit retag flag offensive close merge delete

Comments

returning a clone() of what you grabbed there might help. (esp, if what the grabber returns is "driver memory", that will get out of scope when you delete it)

hmm, do i get this right, you're constructing a grabber for each new image ? ie, you open the video, seek to frame 13, retrieve that, and close the stream again ? sounds like a bad idea..

berak gravatar imageberak ( 2013-08-08 14:29:06 -0600 )edit