Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultiInputFilter not working for 3 inputs in AndroidFastImageProcessing

I am using this library for GPUImage processing.

I am trying to run filters in parallel like we do in iOS GPUImage library, but I am having issues while adding 3 inputs to one filter which extends MultiInputFilter. I am trying to combine

  1. BasicCameraInputFilter
  2. CannyEdgeDetectionFilter
  3. SobelEdgeDetectionFilter

where BasicCameraInputFilter is direct camera input.

BasicCameraInputFilter cameraInput = new BasicCameraInputFilter();
CannyEdgeDetectionFilter cannyEdgeDetectionFilter = new CannyEdgeDetectionFilter(1.0f,0.3f,0.6f);
SobelEdgeDetectionFilter sobelEdgeDetectionFilter = new SobelEdgeDetectionFilter();
CombinationFilter combinationFilter = new CombinationFilter();

cameraInput.addTarget(combinationFilter);
cannyEdgeDetectionFilter.addTarget(combinationFilter);
sobelEdgeDetectionFilter.addTarget(combinationFilter);
combinationFilter.registerFilterLocation(cameraInput,0);
combinationFilter.registerFilterLocation(cannyEdgeDetectionFilter,1);
combinationFilter.registerFilterLocation(sobelEdgeDetectionFilter,2);
combinationFilter.addTarget(this);

registerInitialFilter(cameraInput);
registerFilter(cannyEdgeDetectionFilter);
registerFilter(sobelEdgeDetectionFilter);
registerTerminalFilter(combinationFilter);

Here is my fragment shader for my CombinationFilter

 "precision mediump float;\n"
+"uniform sampler2D " + UNIFORM_TEXTURE0 + ";\n"
+"uniform sampler2D " + UNIFORM_TEXTUREBASE + 1 + ";\n"
+"uniform sampler2D " + UNIFORM_TEXTUREBASE + 2 + ";\n"
+"varying vec2 " + VARYING_TEXCOORD + ";\n"

+"void main(){\n"
+"   vec4 color1 = texture2D(" + UNIFORM_TEXTURE0 + ", " + VARYING_TEXCOORD + ");\n"
+"   vec4 color2 = texture2D(" + UNIFORM_TEXTUREBASE + 1 + ", " + VARYING_TEXCOORD + ");\n"
+"   vec4 color3 = texture2D(" + UNIFORM_TEXTUREBASE + 2 + ", " + VARYING_TEXCOORD + ");\n"

+"   vec4 whiteColor = vec4(1.0);\n"
+"   whiteColor.r = color1.r * color2.r * color3.r;\n"
+"   whiteColor.g = color1.g * color2.g * color3.g;\n"
+"   whiteColor.b = color1.b * color2.b * color3.b;\n"
+"   gl_FragColor =  whiteColor;\n"
+"}\n";
like image 347
Thesoham24 Avatar asked Oct 13 '17 12:10

Thesoham24


1 Answers

The library you're using is out dated and I don't think its being maintained as its author has not been active since 2015. If you're trying to manipulate images on android, then I'll using opencv. Here is how you'd combine the Canny and Sobel filters.

public class ImageManipulationsActivity extends Activity implements CvCameraViewListener2 {
    private static final String  TAG                 = "OCVSample::Activity";

    private CameraBridgeViewBase mOpenCvCameraView;

    private Mat                  mIntermediateMat;

    public static int           viewMode = VIEW_MODE_RGBA;

    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 ImageManipulationsActivity() {
        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);


        mOpenCvCameraView = new CameraBridgeViewBase(this);
        mOpenCvCameraView.setCvCameraViewListener(this);

        setContentView(mOpenCvCameraView);
    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
        } else {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }

    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i(TAG, "called onCreateOptionsMenu");
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);

        return false;
    }

    public void onCameraViewStarted(int width, int height) {
        mIntermediateMat = new Mat();
    }

    public void onCameraViewStopped() {
        // Explicitly deallocate Mats
        if (mIntermediateMat != null)
            mIntermediateMat.release();

        mIntermediateMat = null;
    }

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        Mat rgba = inputFrame.rgba();
        Size sizeRgba = rgba.size();

        Mat rgbaInnerWindow;


            rgbaInnerWindow = rgba.submat(top, top + height, left, left + width);
            Imgproc.Canny(rgbaInnerWindow, mIntermediateMat, 80, 90);

            Mat grayInnerWindow = gray.submat(top, top + height, left, left + width);
            Imgproc.Sobel(mIntermediateMat, grayInnerWindow, CvType.CV_8U, 1, 1);
            Core.convertScaleAbs(grayInnerWindow, mIntermediateMat, 10, 0);
            Imgproc.cvtColor(mIntermediateMat, rgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
            grayInnerWindow.release();
            rgbaInnerWindow.release();


        return rgba;
    }
}
like image 80
Niza Siwale Avatar answered Oct 18 '22 09:10

Niza Siwale