我正在使用版本2.3.5和4.0.4的android,同时代码执行时版本崩溃。 我一直试图从视频中获取一个帧,保存它,然后将其转换为HSV。
(我在opencv网站上发布了同样的问题here)
这是我的代码和我得到的错误。
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;
}
这是在运行4.0.4的设备上执行cvSaveImage或cvCvtColor时获得的logcat
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 android.view.View.performClick(View.java:3627)
08-07 12:26:25.974: I/dalvikvm(6719): at android.view.View$PerformClick.run(View.java:14305)
08-07 12:26:25.974: I/dalvikvm(6719): at android.os.Handler.handleCallback(Handler.java:605)
08-07 12:26:25.974: I/dalvikvm(6719): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 12:26:25.974: I/dalvikvm(6719): at android.os.Looper.loop(Looper.java:137)
08-07 12:26:25.974: I/dalvikvm(6719): at android.app.ActivityThread.main(ActivityThread.java:4511)
08-07 12:26:25.974: I/dalvikvm(6719): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 12:26:25.974: I/dalvikvm(6719): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 12:26:25.974: I/dalvikvm(6719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
08-07 12:26:25.974: I/dalvikvm(6719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
08-07 12:26:25.974: I/dalvikvm(6719): at dalvik.system.NativeStart.main(Native Method)
08-07 12:26:25.974: E/dalvikvm(6719): VM aborting
08-07 12:26:25.974: A/libc(6719): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)
这是2.3.5设备的logcat:
08-07 12:32:05.503: E/javacpp(995): Error loading class com/googlecode/javacpp/Pointer.
08-07 12:32:09.620: I/dalvikvm(995): threadid=4: reacting to signal 3
08-07 12:32:10.620: W/dalvikvm(995): threadid=4: spin on suspend #1 threadid=1 (pcf=0)
08-07 12:32:11.370: W/dalvikvm(995): threadid=4: spin on suspend #2 threadid=1 (pcf=0)
08-07 12:32:11.370: I/dalvikvm(995): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
08-07 12:32:11.370: I/dalvikvm(995): | group="system" sCount=0 dsCount=0 obj=0x4050e148 self=0x288d08
08-07 12:32:11.370: I/dalvikvm(995): | sysTid=999 nice=0 sched=0/0 cgrp=default handle=2078152
08-07 12:32:11.370: I/dalvikvm(995): at dalvik.system.NativeStart.run(Native Method)
08-07 12:32:11.370: I/dalvikvm(995): "main" prio=5 tid=1 RUNNABLE
08-07 12:32:11.370: I/dalvikvm(995): | group="main" sCount=1 dsCount=0 obj=0x4001f190 self=0xce60
08-07 12:32:11.370: I/dalvikvm(995): | sysTid=995 nice=0 sched=0/0 cgrp=default handle=-1345006496
08-07 12:32:11.456: I/dalvikvm(995): at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:32:11.472: I/dalvikvm(995): at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:32:11.476: I/dalvikvm(995): at java.lang.Runtime.loadLibrary(Runtime.java:432)
08-07 12:32:11.488: I/dalvikvm(995): at java.lang.System.loadLibrary(System.java:554)
08-07 12:32:11.507: I/dalvikvm(995): at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:593)
08-07 12:32:11.511: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:489)
08-07 12:32:11.519: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:431)
08-07 12:32:11.527: I/dalvikvm(995): at com.googlecode.javacv.cpp.opencv_imgproc.<clinit>(opencv_imgproc.java:97)
08-07 12:32:11.530: I/dalvikvm(995): at java.lang.Class.classForName(Native Method)
08-07 12:32:11.534: I/dalvikvm(995): at java.lang.Class.forName(Class.java:234)
08-07 12:32:11.542: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:453)
08-07 12:32:11.550: I/dalvikvm(995): at com.googlecode.javacv.cpp.opencv_highgui.<clinit>(opencv_highgui.java:85)
08-07 12:32:11.554: I/dalvikvm(995): at ch.golfer.TrackSpot.process(TrackSpot.java:-1)
08-07 12:32:11.562: I/dalvikvm(995): at ch.golfer.FullscreenActivity$7.onClick(FullscreenActivity.java:196)
08-07 12:32:11.570: I/dalvikvm(995): at android.view.View.performClick(View.java:2538)
08-07 12:32:11.581: I/dalvikvm(995): at android.view.View$PerformClick.run(View.java:9152)
08-07 12:32:11.589: I/dalvikvm(995): at android.os.Handler.handleCallback(Handler.java:587)
08-07 12:32:11.593: I/dalvikvm(995): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 12:32:11.605: I/dalvikvm(995): at android.os.Looper.loop(Looper.java:130)
08-07 12:32:11.616: I/dalvikvm(995): at android.app.ActivityThread.main(ActivityThread.java:3688)
08-07 12:32:11.620: I/dalvikvm(995): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 12:32:11.624: I/dalvikvm(995): at java.lang.reflect.Method.invoke(Method.java:507)
08-07 12:32:11.632: I/dalvikvm(995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-07 12:32:11.640: I/dalvikvm(995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-07 12:32:11.648: I/dalvikvm(995): at dalvik.system.NativeStart.main(Native Method)
08-07 12:32:12.405: W/dalvikvm(995): threadid=4: spin on suspend #3 threadid=1 (pcf=0)
08-07 12:32:12.405: I/dalvikvm(995): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
08-07 12:32:12.405: I/dalvikvm(995): | group="system" sCount=0 dsCount=0 obj=0x4050e148 self=0x288d08
08-07 12:32:12.405: I/dalvikvm(995): | sysTid=999 nice=0 sched=0/0 cgrp=default handle=2078152
08-07 12:32:12.405: I/dalvikvm(995): at dalvik.system.NativeStart.run(Native Method)
08-07 12:32:12.405: I/dalvikvm(995): "main" prio=5 tid=1 RUNNABLE
08-07 12:32:12.405: I/dalvikvm(995): | group="main" sCount=1 dsCount=0 obj=0x4001f190 self=0xce60
08-07 12:32:12.405: I/dalvikvm(995): | sysTid=995 nice=0 sched=0/0 cgrp=default handle=-1345006496
08-07 12:32:12.460: I/dalvikvm(995): at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:32:12.472: I/dalvikvm(995): at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:32:12.480: I/dalvikvm(995): at java.lang.Runtime.loadLibrary(Runtime.java:432)
08-07 12:32:12.484: I/dalvikvm(995): at java.lang.System.loadLibrary(System.java:554)
08-07 12:32:12.488: I/dalvikvm(995): at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:593)
08-07 12:32:12.488: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:489)
08-07 12:32:12.488: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:431)
08-07 12:32:12.488: I/dalvikvm(995): at com.googlecode.javacv.cpp.opencv_imgproc.<clinit>(opencv_imgproc.java:97)
08-07 12:32:12.488: I/dalvikvm(995): at java.lang.Class.classForName(Native Method)
08-07 12:32:12.488: I/dalvikvm(995): at java.lang.Class.forName(Class.java:234)
08-07 12:32:12.488: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:453)
08-07 12:32:12.488: I/dalvikvm(995): at com.googlecode.javacv.cpp.opencv_highgui.<clinit>(opencv_highgui.java:85)
08-07 12:32:12.488: I/dalvikvm(995): at ch.golfer.TrackSpot.process(TrackSpot.java:-1)
08-07 12:32:12.488: I/dalvikvm(995): at ch.golfer.FullscreenActivity$7.onClick(FullscreenActivity.java:196)
08-07 12:32:12.488: I/dalvikvm(995): at android.view.View.performClick(View.java:2538)
08-07 12:32:12.488: I/dalvikvm(995): at android.view.View$PerformClick.run(View.java:9152)
08-07 12:32:12.488: I/dalvikvm(995): at android.os.Handler.handleCallback(Handler.java:587)
08-07 12:32:12.488: I/dalvikvm(995): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 12:32:12.488: I/dalvikvm(995): at android.os.Looper.loop(Looper.java:130)
08-07 12:32:12.488: I/dalvikvm(995): at android.app.ActivityThread.main(ActivityThread.java:3688)
08-07 12:32:12.488: I/dalvikvm(995): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 12:32:12.488: I/dalvikvm(995): at java.lang.reflect.Method.invoke(Method.java:507)
08-07 12:32:12.488: I/dalvikvm(995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-07 12:32:12.488: I/dalvikvm(995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-07 12:32:12.488: I/dalvikvm(995): at dalvik.system.NativeStart.main(Native Method)
08-07 12:32:13.241: W/dalvikvm(995): threadid=4: spin on suspend #4 threadid=1 (pcf=0)
08-07 12:32:13.241: I/dalvikvm(995): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
08-07 12:32:13.241: I/dalvikvm(995): | group="system" sCount=0 dsCount=0 obj=0x4050e148 self=0x288d08
08-07 12:32:13.241: I/dalvikvm(995): | sysTid=999 nice=0 sched=0/0 cgrp=default handle=2078152
08-07 12:32:13.241: I/dalvikvm(995): at dalvik.system.NativeStart.run(Native Method)
08-07 12:32:13.241: I/dalvikvm(995): "main" prio=5 tid=1 RUNNABLE
08-07 12:32:13.241: I/dalvikvm(995): | group="main" sCount=1 dsCount=0 obj=0x4001f190 self=0xce60
08-07 12:32:13.241: I/dalvikvm(995): | sysTid=995 nice=0 sched=0/0 cgrp=default handle=-1345006496
08-07 12:32:13.280: I/dalvikvm(995): at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:32:13.292: I/dalvikvm(995): at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:32:13.292: I/dalvikvm(995): at java.lang.Runtime.loadLibrary(Runtime.java:432)
08-07 12:32:13.292: I/dalvikvm(995): at java.lang.System.loadLibrary(System.java:554)
08-07 12:32:13.292: I/dalvikvm(995): at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:593)
08-07 12:32:13.292: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:489)
08-07 12:32:13.292: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:431)
08-07 12:32:13.292: I/dalvikvm(995): at com.googlecode.javacv.cpp.opencv_imgproc.<clinit>(opencv_imgproc.java:97)
08-07 12:32:13.292: I/dalvikvm(995): at java.lang.Class.classForName(Native Method)
08-07 12:32:13.292: I/dalvikvm(995): at java.lang.Class.forName(Class.java:234)
08-07 12:32:13.292: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:453)
08-07 12:32:13.292: I/dalvikvm(995): at com.googlecode.javacv.cpp.opencv_highgui.<clinit>(opencv_highgui.java:85)
08-07 12:32:13.296: I/dalvikvm(995): at ch.golfer.TrackSpot.process(TrackSpot.java:-1)
08-07 12:32:13.296: I/dalvikvm(995): at ch.golfer.FullscreenActivity$7.onClick(FullscreenActivity.java:196)
08-07 12:32:13.296: I/dalvikvm(995): at android.view.View.performClick(View.java:2538)
08-07 12:32:13.296: I/dalvikvm(995): at android.view.View$PerformClick.run(View.java:9152)
08-07 12:32:13.296: I/dalvikvm(995): at android.os.Handler.handleCallback(Handler.java:587)
08-07 12:32:13.296: I/dalvikvm(995): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 12:32:13.296: I/dalvikvm(995): at android.os.Looper.loop(Looper.java:130)
08-07 12:32:13.296: I/dalvikvm(995): at android.app.ActivityThread.main(ActivityThread.java:3688)
08-07 12:32:13.296: I/dalvikvm(995): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 12:32:13.296: I/dalvikvm(995): at java.lang.reflect.Method.invoke(Method.java:507)
08-07 12:32:13.296: I/dalvikvm(995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-07 12:32:13.296: I/dalvikvm(995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-07 12:32:13.296: I/dalvikvm(995): at dalvik.system.NativeStart.main(Native Method)
08-07 12:32:14.046: W/dalvikvm(995): threadid=4: spin on suspend #5 threadid=1 (pcf=0)
08-07 12:32:14.050: I/dalvikvm(995): "Signal Catcher" daemon prio=5 tid=4 RUNNABLE
08-07 12:32:14.050: I/dalvikvm(995): | group="system" sCount=0 dsCount=0 obj=0x4050e148 self=0x288d08
08-07 12:32:14.050: I/dalvikvm(995): | sysTid=999 nice=0 sched=0/0 cgrp=default handle=2078152
08-07 12:32:14.050: I/dalvikvm(995): at dalvik.system.NativeStart.run(Native Method)
08-07 12:32:14.050: I/dalvikvm(995): "main" prio=5 tid=1 RUNNABLE
08-07 12:32:14.050: I/dalvikvm(995): | group="main" sCount=1 dsCount=0 obj=0x4001f190 self=0xce60
08-07 12:32:14.050: I/dalvikvm(995): | sysTid=995 nice=0 sched=0/0 cgrp=default handle=-1345006496
08-07 12:32:14.062: I/dalvikvm(995): at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:32:14.062: I/dalvikvm(995): at java.lang.Runtime.nativeLoad(Native Method)
08-07 12:32:14.062: I/dalvikvm(995): at java.lang.Runtime.loadLibrary(Runtime.java:432)
08-07 12:32:14.066: I/dalvikvm(995): at java.lang.System.loadLibrary(System.java:554)
08-07 12:32:14.070: I/dalvikvm(995): at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:593)
08-07 12:32:14.073: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:489)
08-07 12:32:14.073: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:431)
08-07 12:32:14.077: I/dalvikvm(995): at com.googlecode.javacv.cpp.opencv_imgproc.<clinit>(opencv_imgproc.java:97)
08-07 12:32:14.077: I/dalvikvm(995): at java.lang.Class.classForName(Native Method)
08-07 12:32:14.077: I/dalvikvm(995): at java.lang.Class.forName(Class.java:234)
08-07 12:32:14.077: I/dalvikvm(995): at com.googlecode.javacpp.Loader.load(Loader.java:453)
08-07 12:32:14.077: I/dalvikvm(995): at com.googlecode.javacv.cpp.opencv_highgui.<clinit>(opencv_highgui.java:85)
08-07 12:32:14.077: I/dalvikvm(995): at ch.golfer.TrackSpot.process(TrackSpot.java:-1)
08-07 12:32:14.077: I/dalvikvm(995): at ch.golfer.FullscreenActivity$7.onClick(FullscreenActivity.java:196)
08-07 12:32:14.077: I/dalvikvm(995): at android.view.View.performClick(View.java:2538)
08-07 12:32:14.077: I/dalvikvm(995): at android.view.View$PerformClick.run(View.java:9152)
08-07 12:32:14.077: I/dalvikvm(995): at android.os.Handler.handleCallback(Handler.java:587)
08-07 12:32:14.077: I/dalvikvm(995): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 12:32:14.077: I/dalvikvm(995): at android.os.Looper.loop(Looper.java:130)
08-07 12:32:14.077: I/dalvikvm(995): at android.app.ActivityThread.main(ActivityThread.java:3688)
08-07 12:32:14.077: I/dalvikvm(995): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 12:32:14.077: I/dalvikvm(995): at java.lang.reflect.Method.invoke(Method.java:507)
08-07 12:32:14.077: I/dalvikvm(995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-07 12:32:14.085: I/dalvikvm(995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-07 12:32:14.085: I/dalvikvm(995): at dalvik.system.NativeStart.main(Native Method)
提前感谢您的帮助。
答案 0 :(得分:0)
我发现了这个问题,这是我的理论:
此代码正在通过JNI运行;因此,当我抓取图像并将其设置为等于另一个图像时,它只是传递指针。另外,因为我在getFrame函数中启动了我的抓取器,当返回图像时抓取器被破坏,并且由于一个奇怪的原因,内存被刷新并且传递的指针变得无用。
该问题的解决方案是将抓取器设置为全局变量,将抓取器作为参数传递给getFrame函数,或者(奇怪地)执行在返回图像之前触摸内存的opencv操作(即在随机的东西上调用cvCvSaveimage或cvCvtColor甚至不必与图像相关联。)
我希望我的解释有意义,但我不知道它是否应该被报告为错误?