[OpenCV4Android] modifying frame(puttext, rectangle ... etc) not working on onCameraFrame

asked 2013-09-14 12:09:27 -0500

GongdoriCat gravatar image

updated 2013-09-14 12:19:59 -0500


I'm a student who is trying to create a program which creates threshold image(using inrange, for use in color blob tracking)

However, I'm stuck with displaying the threshold image on JavaCameraView(for testing purposes).

According to other example and documentations, it seems that modifying OnCameraFrame(CvCameraViewFrame inputFrame)'s inputFrame.rgba() changes the displayed frame. However, I cannot see the modifications I made to the given frame(in the code below, it's mRgba in OnCameraFrame)

I tried to do the following

  1. input text overlay to the frame using puttext

  2. make the frame to be gray using Imgproc.cvtcolor

  3. tried to make thresholdimage using processImage(created inside the code)

None of them change the frame displayed.

I hope to see why the modifications don't work.

PS. The Phone I used was Galaxy S4 LTE-A SK Telecom (SHV-E330S) with OpenCV 2.4.6

PPS. Same thing happens in Nexus S with CM10

package com.example.opencvtrialexample;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.ImageView;

public class MainActivity extends Activity implements CvCameraViewListener2{
    private static final String TAG="MyActivity_OpenCVTest";
    private CameraBridgeViewBase mOpenCvCameraView;
    private int[][] pointArray=new int[5][2];
    private final Scalar HSV1=new Scalar(20,100,100);
    private final Scalar HSV2=new Scalar(100,255,255);
    //OpenCV Initialization
    private BaseLoaderCallback mLoaderCallback=new BaseLoaderCallback(this){
        public void onManagerConnected(int status){
                case LoaderCallbackInterface.SUCCESS:
                    Log.i(TAG,"OpenCV loaded Successfully");
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG,"called onCreate");

        //mOpenCvCameraView.setCvCameraViewListener((CvCameraViewListener2) this);
    public void onPause(){

    public void onDestroy(){
    public void onResume(){
    public void onCameraViewStarted(int width, int height){

    public void onCameraViewStopped(){

    public Mat onCameraFrame(CvCameraViewFrame inputFrame){//**This is where Problem Happens**
        Mat mRgba=inputFrame.rgba();
        Mat mProc=processImage(mRgba);
            //Not working puttext
        Core.putText(mRgba, "=====TEST=====2013.09.15", new Point(100, 500), 3, 1, new Scalar(255, 0, 0, 255), 2);
        //replacing mRgba with mProc
        Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2GRAY);
        Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_GRAY2RGBA);
        //temporarily disabled to test whether puttext work
        return mRgba;

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is ...
edit retag flag offensive close merge delete


NOTE : I just discovered that i converted(I don't know why...) setCvCameraViewListener to comment(//). After reviving that line, it works very well. Ah! My precious 3 hours!

GongdoriCat gravatar imageGongdoriCat ( 2013-09-14 13:32:32 -0500 )edit

Just wanted to write the answer and saw you noticed it :D If you comment out that line, your OnCameraFrame will never be called since you dont "listen" to the camera. The inputframe will just be passed directly to the camera view.

Moster gravatar imageMoster ( 2013-09-14 13:37:18 -0500 )edit