Javacv:使用方法后,Mat数据变为null

时间:2017-04-05 11:17:25

标签: java android opencv ffmpeg javacv

我正在使用Android应用程序进行对象检测和计数。对于图像处理,我使用的是JavaCV(OpenCV和FFmpeg的Java包装器)。导入库后,我能够成功使用FFmpegFrameGrabber来获取视频帧。

我的问题:我将Frame转换为Mat对象并对该Mat对象执行某些操作后,数据变为空。

代码:

MainActivity

The child process exited normally with status 1.

Countmodule的构造函数

    public class MainActivity extends AppCompatActivity {



    OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();


    private CountModule countModule;
    FFmpegFrameGrabber retriever;

    ArrayList<opencv_core.Mat> frames;
    boolean frameloaded = false;

    File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
    File video = new File(folder, "test.mp4");

    AndroidFrameConverter converterToBitmap = new AndroidFrameConverter();



    private static WebStreamer webStreamer;

    static {
        System.loadLibrary("native-lib");
    }

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


        final Button button = (Button) findViewById(R.id.btnLdFrames);
        final ImageView img = (ImageView) findViewById(R.id.imageView);


        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ((TextView) button).setText("Loading");
                new Thread(new Runnable() {
                    public void run() {

                        try {
                            button.setClickable(false);
                            button.setAlpha((float) 0.3);
                            LoadFrames();
                            button.setAlpha((float) 1);

                        } catch (FrameGrabber.Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });
    }
    private void LoadFrames() throws FrameGrabber.Exception {
        if (!frameloaded){



            frameloaded = true;

            retriever = new FFmpegFrameGrabber(video);
            frames = new ArrayList<>();
            Log.d("Frame",": Start of loop");

            retriever.start();
            final ImageView img = (ImageView) findViewById(R.id.imageView);
            for (int i=0;i<50;i++){//155430
                retriever.setFrameNumber(i*100);
                Frame temp = new Frame();
                temp = retriever.grab();

                frames.add(converterToMat.convert(temp));

                Log.d("Frame",": " + i*100);



            }
            retriever.stop();

            countModule = new CountModule(frames);
            Log.d("Frame","CountModule instantiated");

        }
    }
}

变量(图片):

After convert from Frame to Mat. Data has values.

Data=null when using the method frame.copyTo(median). Data of Mat also null when using method Mat.ones()

正如您在每次使用OpenCV特定方法时所看到的那样,返回的Mat不是预期的。

0 个答案:

没有答案