opencv获取变量中的fps
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv获取变量中的fps相关的知识,希望对你有一定的参考价值。
我是android上的新手。我想用OpenCV制作应用程序。对于我的应用程序,我希望将预览的FPS存储在变量中。我在OpenCVLibrary文件夹中找到了一个名为FpsMeter的类,但是当我调用fpsmeter.init()时,我的应用程序崩溃了。我尝试了不同的东西,比如设置
cameraBridgeViewBase.enableFpsMeter();
要么
cameraBridgeViewBase.disableFpsMeter();
要么
fpsmeter.setResolution(320, 240);
但它每次都会崩溃。
有谁可以帮助我吗?提前致谢。
package be.anton.OpenCV_test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.widget.Toast;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.FpsMeter;
import org.opencv.android.JavaCameraView;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
CameraBridgeViewBase cameraBridgeViewBase;
BaseLoaderCallback baseLoaderCallback;
FpsMeter fpsMeter;
Mat mat1;
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraBridgeViewBase = (JavaCameraView) findViewById(R.id.cam);
cameraBridgeViewBase.setVisibility(SurfaceView.VISIBLE);
cameraBridgeViewBase.enableFpsMeter();
cameraBridgeViewBase.setCvCameraViewListener(this);
baseLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
// super.onManagerConnected(status);
switch (status) {
case BaseLoaderCallback.SUCCESS: {
cameraBridgeViewBase.enableView();
break;
}
default: {
super.onManagerConnected(status);
break;
}
}
}
};
fpsMeter.init(); // when I add this line my app crashes
}
@Override
protected void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
Toast.makeText(getApplicationContext(), "OpenCV could not be loaded.", Toast.LENGTH_SHORT).show();
} else {
baseLoaderCallback.onManagerConnected(BaseLoaderCallback.SUCCESS);
}
}
@Override
protected void onPause() {
super.onPause();
if (cameraBridgeViewBase != null) {
cameraBridgeViewBase.disableView();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (cameraBridgeViewBase != null) {
cameraBridgeViewBase.disableView();
}
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mat1 = inputFrame.rgba();
return mat1;
}
@Override
public void onCameraViewStopped() {
mat1.release();
}
@Override
public void onCameraViewStarted(int width, int height) {
mat1 = new Mat(width, height, CvType.CV_8UC4);
}
}
答案
解决方案:我创建了自己的FPS仪表。这是代码:
我使用的变量:
Textview txt1;
int mFPS;
long startTime = 0;
long currentTime = 1000;
在onCreate()中设置:
runOnUiThread(new Runnable() {
@Override
public void run() {
txt1 = (TextView) findViewById(R.id.txt1);
}
});
这是我的onCameraFrame():
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mat1 = inputFrame.rgba();
//Log.i(TAG, mat1.get(128, 128).toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
if (currentTime - startTime >= 1000) {
txt1.setText("FPS: " + String.valueOf(mFPS));
mFPS = 0;
startTime = System.currentTimeMillis();
}
currentTime = System.currentTimeMillis();
mFPS += 1;
}
});
return mat1;
}
以上是关于opencv获取变量中的fps的主要内容,如果未能解决你的问题,请参考以下文章
OpenCV:VideoCapture::get(CV_CAP_PROP_FPS) 返回 0 FPS
在 Python 多处理进程中运行较慢的 OpenCV 代码片段