我的Mat difference
有一些黑色像素(或者实际上几乎是黑色的像素 - >如果发生了地震,建筑会移动等等),而Mat current
包含真实图像的自然色。我想将Mat current
中的像素替换为Mat difference
中的这些黑色像素。如果我像这样手动完成:
for(int i = 0; i < difference.rows(); i++){
for(int j = 0; j < difference.cols(); j++){
subtractedPixel = difference.get(i, j);
if(subtractedPixel[0] < 10 && subtractedPixel[1] < 10 && subtractedPixel[2] < 10){
originalPixel = current.get(i, j);
difference.put(i, j, originalPixel);
}
}
非常慢。
完整代码(正在运行):
package com.example.szpieg2;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Range;
import org.opencv.core.Size;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
public class TrackActivity extends Activity implements CvCameraViewListener {
private Mat current;
private CameraBridgeViewBase cameraView;
private Mat previous;
private Mat difference;//difference between previous and current
private boolean first = true;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
Log.i("Co sie dzieje?", "OpenCV loaded successfully");
cameraView.enableView();
// cameraView.setOnTouchListener(ColorBlobDetectionActivity.this);
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_track);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
cameraView = (CameraBridgeViewBase) findViewById(R.id.surface_view);
cameraView.setCvCameraViewListener(this);
}
// --------Activity Actions---------
@Override
public void onPause() {
if (cameraView != null)
cameraView.disableView();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this,
mLoaderCallback);
}
public void onDestroy() {
super.onDestroy();
if (cameraView != null)
cameraView.disableView();
}
// --------/Activity Actions/---------
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_track, menu);
return true;
}
// --------listener method implementation-------------
@Override
public void onCameraViewStarted(int width, int height) {
/*current = new Mat(width, height, CvType.CV_64FC4);
previous = new Mat(width, height, CvType.CV_64FC4);
difference = new Mat(width, height, CvType.CV_64FC4);*/
current = new Mat(width, height, CvType.CV_8UC4);
previous = new Mat(width, height, CvType.CV_8UC4);
difference = new Mat(width, height, CvType.CV_8UC4);//RGBA 0..255
}
@Override
public void onCameraViewStopped() {
current.release();
}
@Override
public Mat onCameraFrame(Mat inputFrame) {
inputFrame.copyTo(current);
if(first){//first is true at the first time
inputFrame.copyTo(previous);
first = false;
Log.i("First processing", "Pierwszy przebieg");
}
Core.absdiff(current, previous, difference);
// Core.absdiff( previous,current, difference);
//I leave black pixels and load original colors
double[] subtractedPixel, originalPixel;
String s = "";
for(int i = 0; i < difference.rows(); i++){
for(int j = 0; j < difference.cols(); j++){
subtractedPixel = difference.get(i, j);
if(subtractedPixel[0] < 10 && subtractedPixel[1] < 10 && subtractedPixel[2] < 10){
originalPixel = previous.get(i, j);
difference.put(i, j, originalPixel);
}
}
// s+="\n";
}
// Log.i("mat ", s);
// Log.i("mat ", difference.get(44,444)[0] + "");
//---------------------------------------------
inputFrame.copyTo(previous);
return difference;//UNREAL COLORS
}
}