PTT短網址: OpenCV4Android做即時影像背景相減的問題
網友想用OpenCV4Android做手機鏡頭的即時影像做背景相減–檢測差異處的程式
簡短地來說,程式想用背景相減來做,所以需要兩張影像比對。
Android Camera Capture
1.一張拍照的固定影像作比對的基礎(暫存圖片)
2.用固定影像比對Camera的即時影像
問題:暫存的那張影像,理論上是一張不會動的影像,但還是會持續被更新
解決的想法:
實際的作法:
1.做一個menu,去觸發count(計數用的變數,或稱計數器)
2.Android畫面加一個txtview輸出count是否有正確被更新(運作)
3.Android畫面加一個ImageView可以輸出一張圖,count觸發條件時,改變影像
4.執行1-3就會知道camera是否有抓到值可以回傳或是沒抓到
private static final int VIEW_MODE_GRAY = 1;
private int mViewMode;
private int count = 0;
private Mat mRgba;
private Mat mGray;
private Mat once;
private MenuItem mItemPreviewGray;
private Bitmap bitmap1 = null;
private TextView textView01;
private ImageView imageView01;
private void ViewSettings() {
textView01 = (TextView) findViewById(R.id.textView1);
imageView01 = (ImageView) findViewById(R.id.imageView1);
};
public void onCameraViewStarted(int width, int height) {
ViewSettings();
mRgba = new Mat(height, width, CvType.CV_8UC4);
mGray = new Mat(height, width, CvType.CV_8UC1);
once = new Mat(height, width, CvType.CV_8UC1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, “called onCreateOptionsMenu”);
mItemPreviewGray = menu.add(“Preview GRAY”);
return true;
}
public void onCameraViewStarted(int width, int height) {
ViewSettings();
mRgba = new Mat(height, width, CvType.CV_8UC4)
mGray = new Mat(height, width, CvType.CV_8UC1)
once = new Mat(height, width, CvType.CV_8UC1);
}
//OpenCV的函數取得Camera的inputFrame
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
final int viewMode = mViewMode;
final int mcount = count;
switch (viewMode) {
// input frame has gray scale format
Imgproc.cvtColor(inputFrame.gray(), mRgba, Imgproc.COLOR_GRAY2RGBA
, 4);
if (mcount == 1){
once = inputFrame.gray();
}
if (mcount == 2){
once = inputFrame.gray();
}
break;
}
return mRgba;
}
public boolean onOptionsItemSelected(MenuItem item) {
Log.i(TAG, “called onOptionsItemSelected; selected item: ” + ite
if (item == mItemPreviewGray) {
mViewMode = VIEW_MODE_GRAY;
count++;
textView01.setText(String.valueOf(count));
if (count == 1){
}
else if (count == 2){
Bitmap bm1 = Bitmap.createBitmap(mRgba.cols(), m
ows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(once, bm1);
imageView01.setImageBitmap(bm1);
}
else if (count == 3){
Bitmap bm1 = Bitmap.createBitmap(mRgba.cols(), mRgba.r
ows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(once, bm1);
imageView01.setImageBitmap(bm1);
}
else{
imageView01.setImageResource(R.drawable.lena);
}
}
return true;
}
—
趕著出門,有空再來修文章,Sorry..