从一个Mat复制到另一个Mat只有近乎黑色的像素

从一个Mat复制到另一个Mat只有近乎黑色的像素

本文介绍了从一个Mat复制到另一个Mat只有近乎黑色的像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Mat差异它有一些黑色像素(或者真的几乎是黑色像素 - >如果发生了地震,建筑会移动等等)并且 Mat current ,包含自然色的真实图像。我想用 Mat差异中的这些黑色像素替换 Mat current 中的像素。如果我这样手动执行:

I have Mat difference which has some black pixels(or really nearly black pixels -> if earhquake occurs, buliding will move etc.) in it and the Mat current which consists real image with natural colors. I would like to replace pixels in Mat current with these black pixels in Mat difference. If I do it manually like this:

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);
                }
            }

非常慢。

完整代码(正在运行):

Full code(running):

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
    }

}


推荐答案

您可以执行以下操作。


  1. 使用。

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

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

The output mask will be a binary mask where its pixel is set to 255 if its under threshold cv::Scalar(9,9,9);

差异图像复制到当前图像通过掩码使用 copyTo()

Copy difference image into current image through mask using copyTo().

difference.copyTo(current, mask);


这篇关于从一个Mat复制到另一个Mat只有近乎黑色的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!