保存图像时的JavaCV EXCEPTION_ACCESS_VIOLATION

时间:2012-08-05 06:39:18

标签: java opencv video-processing image-recognition javacv

所以我正在使用JavaCV并尝试将帧捕获为来自mp4文件的图像。当我尝试保存图像并且无法在任何地方找到解决方案时,我不断获得EXCEPTION_ACCESS_VIOLATION。 OpenCV,JavaCV和Java都具有相同的位数(64)。我也安装了64位visual c ++。以下是我正在使用的代码,知道导致这个或替代解决方案的原因。感谢您的帮助。

      public class VideoTest {

        public static void main(final String args[]) {
            VideoTest vt = new VideoTest();
            IplImage[] images = vt.extractVideoFrames("video.mp4");
            int i = 1;
            //Save images
            for(IplImage image : images) {
                /* Exception_ACCESS_VIOLATION occurs here */
                cvSaveImage(Integer.toString(i) + ".jpg", image);
                i++;
            }
        }

        /* Extract at least 30 frames from the video at even intervals */
        private IplImage[] extractVideoFrames(String videoName) {
            FrameGrabber grabber = new FFmpegFrameGrabber(videoName);
            try {
                grabber.start();
            } catch (com.googlecode.javacv.FrameGrabber.Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            double frameLength = grabber.getLengthInFrames();
            int increment = (int) Math.ceil(frameLength / 30);
            if (frameLength < 30) {
                System.err.println("Not enough frames");
            }

            IplImage[] faceImgArr = new IplImage[30];

            /* Ensure we don't go past end of video and get at least 30 frames */
            for (int i = 0, j = 0; i < frameLength && j <= 30; i += increment, j++) {
                try {
                    if ((grabber.getFrameNumber() % increment) == 0) {
                        faceImgArr[j] = grabber.grab();
                    } else {
                        grabber.grabFrame();
                    }
                } catch (Exception e) {
                    System.err.println("Error grabbing frame");
                }
            }

            return faceImgArr;
        }

    }

Error Message:
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000066de1630, pid=1328, tid=7016
#
# JRE version: 6.0_32-b05
# Java VM: Java HotSpot(TM) 64-Bit Server VM (20.7-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [opencv_ffmpeg242_64.dll+0x5a1630]
#
# An error report file with more information is saved as:
# C:\Users\name\workspace\Video\hs_err_pid1328.log
#


Snippet from log:
Stack: [0x0000000002900000,0x0000000002a00000],  sp=0x00000000029fec30,  free space=1019k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [opencv_ffmpeg242_64.dll+0x5a1630]  cvWriteFrame_FFMPEG+0x59e2b0

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.googlecode.javacv.cpp.opencv_highgui.cvCreateFileCapture(Ljava/lang/String;)Lcom/googlecode/javacv/cpp/opencv_highgui$CvCapture;+0
j  com.googlecode.javacv.OpenCVFrameGrabber.start()V+22
j  main.recognition.VideoTest.extractVideoFrames(Ljava/lang/String;)[Lcom/googlecode/javacv/cpp/opencv_core$IplImage;+10
j  main.recognition.VideoTest.main([Ljava/lang/String;)V+11
v  ~StubRoutines::call_stub

1 个答案:

答案 0 :(得分:4)

由于变量超出范围,Javacv代码中通常会发生访问冲突(JNI与正常情况略有不同。请参阅此帖子我如何解决访问冲突问题http://tech.thecoolblogs.com/2012/10/exceptionaccessviolation-while-using.html

要解决您的问题,首先在一个范围内尝试整个代码。我的意思是在一个方法中并在一个try .. catch块中。如果可行,我们可以假设这个问题显然与一些超出范围的变量有关。然后尝试移动IplImage创建并查看它是否消失。

如果它仍然无法工作(在合并到相同的方法和范围之后),请检查temp文件夹(C:\ users \ ur id \ AppData \ Local \ Temp \ _javacpp **文件夹以查看是否有dll文件被复制