4

我有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
    }

}
4

1 回答 1

5

您可以执行以下操作。

  1. 使用inRange().

    Mat mask;
    void inRange(difference, Scalar(0,0,0), Scalar(9,9,9), mask)
    

    输出mask将是一个二进制掩码,如果低于阈值,则其像素设置为 255cv::Scalar(9,9,9);

  2. 通过使用将图像复制difference到图像中。currentmaskcopyTo()

    difference.copyTo(current, mask);
    
于 2013-06-13T18:40:11.833 回答