opencv android中背景减法中的应用方法错误
Posted
技术标签:
【中文标题】opencv android中背景减法中的应用方法错误【英文标题】:error in apply method in Background subtraction in opencv android 【发布时间】:2014-04-14 08:39:47 【问题描述】:我想在 InputFrame 上做背景减法,并想返回前景蒙版。 我面临错误,因为下面有人可以帮我解决它 在此先感谢
我的代码:
package org.opencv.samples.tutorial1;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.video.BackgroundSubtractorMOG;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.Toast;
public class Tutorial1Activity extends Activity implements CvCameraViewListener2
private static final String TAG = "Divyesh::Activity";
private CameraBridgeViewBase mOpenCvCameraView;
private boolean mIsJavaCamera = true;
private MenuItem mItemSwitchCamera = null;
public Point p1 = new Point(10, 2);
public Point p2 = new Point(100, 20);
public Scalar scalar = new Scalar(255, 255, 255);
public Mat fgMask;
public Mat mRgba, mGray, mRgb;
//public BackgroundSubtractor pMog, pMog2;
BackgroundSubtractorMOG pMog;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
@Override
public void onManagerConnected(int status)
switch (status)
case LoaderCallbackInterface.SUCCESS:
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
;
public Tutorial1Activity()
Log.i(TAG, "Instantiated new " + this.getClass());
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.tutorial1_surface_view);
if (mIsJavaCamera)
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
else
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
@Override
public void onPause()
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
@Override
public void onResume()
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
public void onDestroy()
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
@Override
public boolean onCreateOptionsMenu(Menu menu)
Log.i(TAG, "called onCreateOptionsMenu");
mItemSwitchCamera = menu.add("Toggle Native/Java camera");
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
String toastMesage = new String();
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
if (item == mItemSwitchCamera)
mOpenCvCameraView.setVisibility(SurfaceView.GONE);
mIsJavaCamera = !mIsJavaCamera;
if (mIsJavaCamera)
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
toastMesage = "Java Camera";
else
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
toastMesage = "Native Camera";
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();
Toast toast = Toast.makeText(this, toastMesage, Toast.LENGTH_LONG);
toast.show();
return true;
public void onCameraViewStarted(int width, int height)
mRgba = new Mat();
mGray = new Mat();
mRgb = new Mat();
fgMask = new Mat();
Log.i(TAG, "Before subtractor");
pMog = new BackgroundSubtractorMOG();
public void onCameraViewStopped()
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
Log.i(TAG, "Before input frame");
mRgba = inputFrame.rgba();
//Imgproc.cvtColor(mRgba, mRgb, Imgproc.COLOR_RGBA2RGB);
Log.i(TAG,"Before apply");
pMog.apply(mRgba, fgMask);
Log.i(TAG,"After apply");
//Imgproc.cvtColor(fgMask, mRgba, Imgproc.COLOR_GRAY2RGBA);
return fgMask;
我的错误在这里:
/** *
计算前景掩码。
* * @param image 下一个视频帧。 * @param fgmask 输出前景掩码为 8 位二进制图像。 * * @见org.opencv.video.BackgroundSubtractor.operator() */public void apply(Mat image, Mat fgmask)
apply_1(nativeObj, image.nativeObj, fgmask.nativeObj);
return;
如果你能解决它或者能给一些指导的话,非常感谢............
【问题讨论】:
现在你到底得到了什么错误? 我在运行时出现黑屏....但根据日志仍在接收帧 这可能令人不快,但不是真正的错误。一个模型必须在那里接受训练,你通常不会在返回的第一帧中得到正确的结果。使用历史等参数。 我会尝试和thanx兄弟 兄弟它不工作历史上可能有什么???? 【参考方案1】:我之前没有使用或使用过背景减法,但请尝试在您的代码中进行以下编辑。
mRgba = inputFrame.rgba();
Imgproc.cvtColor(mRgba, mRgb, Imgproc.COLOR_RGBA2BGR,4);
Log.i(TAG,"Before apply");
pMog.apply(mRgb, fgMask);
Log.i(TAG,"After apply");
Imgproc.cvtColor(fgMask, mRgb, Imgproc.COLOR_GRAY2BGR,3);
Imgproc.cvtColor(mRgb, mRgba, Imgproc.COLOR_BGR2RGBA,4);
return fgMask;
Also check this link. I think it might help:http://www.codeproject.com/Articles/791145/Motion-Detection-in-Android-Howto
希望这会有所帮助。
【讨论】:
以上是关于opencv android中背景减法中的应用方法错误的主要内容,如果未能解决你的问题,请参考以下文章