Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to speeding up camera preview in android opencv?

I am working with android opencv and i am using CameraBridgeViewBase class. but when i am run my application and every time camera preview 5-6 second delay. Here is my code

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { return findRectangle(inputFrame.rgba()); }

private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }

    if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}

private double angle(Point p1, Point p2, Point p0) {
    double dx1 = p1.x - p0.x;
    double dy1 = p1.y - p0.y;
    double dx2 = p2.x - p0.x;
    double dy2 = p2.y - p0.y;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}

How to speeding up camera preview in android opencv?

I am working with android opencv and i am using CameraBridgeViewBase class. but when i am run my application and every time camera preview 5-6 second delay. Here is my code

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { return findRectangle(inputFrame.rgba()); }

private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }

    if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}

private double angle(Point p1, Point p2, Point p0) {
    double dx1 = p1.x - p0.x;
    double dy1 = p1.y - p0.y;
    double dx2 = p2.x - p0.x;
    double dy2 = p2.y - p0.y;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}

How to speeding up camera preview in android opencv?opencv using CameraBridgeViewBase class and JavaCameraView?

I am working with android opencv and i am using CameraBridgeViewBase class. but when i am run my application and every time camera preview 5-6 second delay. Here is my code

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { return findRectangle(inputFrame.rgba()); }

private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }

    if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}

private double angle(Point p1, Point p2, Point p0) {
    double dx1 = p1.x - p0.x;
    double dy1 = p1.y - p0.y;
    double dx2 = p2.x - p0.x;
    double dy2 = p2.y - p0.y;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}

How to speeding up camera preview in android opencv using CameraBridgeViewBase class and JavaCameraView?

I am working with android opencv and i am using CameraBridgeViewBase class. but when i am run my application and every time camera preview 5-6 second delay. Here is my code

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { return findRectangle(inputFrame.rgba()); }

private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }

    if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}

private double angle(Point p1, Point p2, Point p0) {
    double dx1 = p1.x - p0.x;
    double dy1 = p1.y - p0.y;
    double dx2 = p2.x - p0.x;
    double dy2 = p2.y - p0.y;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}

How to speeding up camera preview in android opencv using CameraBridgeViewBase class and JavaCameraView?

I am working with android opencv and i am using CameraBridgeViewBase class. but when i am run my application and every time camera preview 5-6 second delay. Here is my code

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { return findRectangle(inputFrame.rgba()); }

private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }

    if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}

private double angle(Point p1, Point p2, Point p0) {
    double dx1 = p1.x - p0.x;
    double dy1 = p1.y - p0.y;
    double dx2 = p2.x - p0.x;
    double dy2 = p2.y - p0.y;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}

How to speeding up camera preview in android opencv using CameraBridgeViewBase class and JavaCameraView?

I am working with android opencv OpenCV and i am using CameraBridgeViewBase class. but when i am run my application and every time camera preview 5-6 second delay. Here is my codecode.

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { return findRectangle(inputFrame.rgba()); }

private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }

    if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}

private double angle(Point p1, Point p2, Point p0) {
    double dx1 = p1.x - p0.x;
    double dy1 = p1.y - p0.y;
    double dx2 = p2.x - p0.x;
    double dy2 = p2.y - p0.y;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}

speeding up camera preview in android opencv using CameraBridgeViewBase class and JavaCameraView?

I am working with android OpenCV and i am using CameraBridgeViewBase class. but when i am run my application and every time camera preview 5-6 second delay. Here is my code.delay.

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { return findRectangle(inputFrame.rgba()); }

private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }

    if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}

private double angle(Point p1, Point p2, Point p0) {
    double dx1 = p1.x - p0.x;
    double dy1 = p1.y - p0.y;
    double dx2 = p2.x - p0.x;
    double dy2 = p2.y - p0.y;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}

speeding up camera preview in android opencv using CameraBridgeViewBase class and JavaCameraView?

I am working with android OpenCV and i am using CameraBridgeViewBase class. but when i am run my application and every time camera preview 5-6 second delay.

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { return findRectangle(inputFrame.rgba()); }

private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }

    if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}

private double angle(Point p1, Point p2, Point p0) {
    double dx1 = p1.x - p0.x;
    double dy1 = p1.y - p0.y;
    double dx2 = p2.x - p0.x;
    double dy2 = p2.y - p0.y;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}

speeding up camera preview in android opencv using CameraBridgeViewBase class and JavaCameraView?

I am working with android OpenCV and i am using CameraBridgeViewBase class. but when i am run my application and every time camera preview 5-6 second delay.

public Mat onCameraFrame(CvCameraViewFrame inputFrame) { return findRectangle(inputFrame.rgba()); }

private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }

    if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}

private double angle(Point p1, Point p2, Point p0) {
    double dx1 = p1.x - p0.x;
    double dy1 = p1.y - p0.y;
    double dx2 = p2.x - p0.x;
    double dy2 = p2.y - p0.y;
    return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}