hello. 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 make the modified rgba frame (in code, mRgba to be displayed to the image. 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 What is the problem with the code?
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){
switch(status){
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG,"OpenCV loaded Successfully");
mOpenCvCameraView.enableView();
}break;
default:
{
super.onManagerConnected(status);
}break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG,"called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mOpenCvCameraView=(CameraBridgeViewBase)findViewById(R.id.HelloOpenCvView);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
//mOpenCvCameraView.setCvCameraViewListener((CvCameraViewListener2) this);
}
public void onPause(){
super.onPause();
if(mOpenCvCameraView!=null)
mOpenCvCameraView.disableView();
}
public void onDestroy(){
super.onDestroy();
if(mOpenCvCameraView!=null)
mOpenCvCameraView.disableView();
}
public void onResume(){
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6,this,mLoaderCallback);
}
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
//mProc.copyTo(mRgba);
return mRgba;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//Image Processing
//Main Function
public Mat processImage(Mat inputFrame){
Mat inputFrame_HSV=new Mat();
//initialize pointArray
for(int i=0;i<6;i++){
for(int j=0;j<2;j++)
pointArray[i][j]=-1;
}
//Convert frame into HSV format(from RGB)
Imgproc.cvtColor(inputFrame, inputFrame_HSV, Imgproc.COLOR_RGBA2RGB);
Imgproc.cvtColor(inputFrame, inputFrame_HSV, Imgproc.COLOR_RGB2HSV);
//Create threshold image
Mat threshImage=getThresholdImg(inputFrame_HSV,HSV1,HSV2);
return threshImage;
}
//Get Threshold Image
public Mat getThresholdImg(Mat inputFrame_HSV,Scalar HSV1, Scalar HSV2){
Mat threshImg=new Mat(inputFrame_HSV.size(),inputFrame_HSV.type());
Core.inRange(inputFrame_HSV, HSV1, HSV2, threshImg);
Imgproc.cvtColor(threshImg, threshImg, Imgproc.COLOR_HSV2RGB);
Imgproc.cvtColor(threshImg, threshImg, Imgproc.COLOR_RGB2RGBA);
return threshImg;
}
}