Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

i'd rather make all temporary Mat's class members, and allocate them only once.

public class MyActivity extends Activity implements CvCameraViewListener2 { CameraBridgeViewBase mOpenCvCameraView; Mat mRgba, mGray, mHsv;

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    Mat rgba = inputFrame.rgba();
    // convert to hsv, get binary img:
    Imgproc.cvtColor(rgba , mHsv, Imgproc.COLOR_BGR2HSV);
    Core.inRange(mHsv, new Scalar(50, 70, 100), new Scalar(150, 250, 250), mGray);

    // save a rgba copy of the binary for drawing later: 
    Imgproc.cvtColor(mGray , mRgba, Imgproc.COLOR_GRAY2RGBA);
    Imgproc.Canny(mGray, mGray, 80, 100, 5, true);
    // now, we *need* a new here !
    Mat lines = new Mat();
    Imgproc.HoughLinesP(mGray, lines, 1, Math.PI/180, 125, 20, 20);
    for (int x = 0; x < lines.cols(); x++) 
    {
          double[] vec = lines.get(0, x);
          double x1 = vec[0], 
                 y1 = vec[1],
                 x2 = vec[2],
                 y2 = vec[3];
          Point start = new Point(x1, y1);
          Point end = new Point(x2, y2);

          Core.line(mRgba, start, end, new Scalar(255,0,0), 2); 
    }
    lines.release(); // !!
    return mRgba;
}

public void onCameraViewStarted(int width, int height) {
    // allocate the temporary Mat's only once:
    mGray = new Mat(height,width,CvType.CV_8U);
    mHsv = new Mat(height,width,CvType.CV_8UC3);
    mRgba = new Mat(height,width,CvType.CV_8UC4);
}

i'd rather make all temporary Mat's class members, and allocate them only once.

public class MyActivity extends Activity implements CvCameraViewListener2 {
CameraBridgeViewBase mOpenCvCameraView;
Mat mRgba, mGray, mHsv;

mHsv;
...
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat rgba = inputFrame.rgba();
// convert to hsv, get binary img:
Imgproc.cvtColor(rgba , mHsv, Imgproc.COLOR_BGR2HSV);
Core.inRange(mHsv, new Scalar(50, 70, 100), new Scalar(150, 250, 250), mGray);
// save a rgba copy of the binary for drawing later:
Imgproc.cvtColor(mGray , mRgba, Imgproc.COLOR_GRAY2RGBA);
// can do Canny in-place:
Imgproc.Canny(mGray, mGray, 80, 100, 5, true);
// now, we *need* a new here !
Mat lines = new Mat();
Imgproc.HoughLinesP(mGray, lines, 1, Math.PI/180, 125, 20, 20);
for (int x = 0; x < lines.cols(); x++)
{
double[] vec = lines.get(0, x);
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
Core.line(mRgba, start, end, new Scalar(255,0,0), 2);
}
lines.release(); // !!
return mRgba;
}
public void onCameraViewStarted(int width, int height) {
// allocate the temporary Mat's only once:
mGray = new Mat(height,width,CvType.CV_8U);
mHsv = new Mat(height,width,CvType.CV_8UC3);
mRgba = new Mat(height,width,CvType.CV_8UC4);
}