簡體   English   中英

在Android中應用OpenCV方法時出現java.lang.IndexOutOfBoundsException錯誤

[英]java.lang.IndexOutOfBoundsException error when applying OpenCV methods in Android

該代碼是關於使用OpenCV在Android設備的相機中查找最大的矩形。 該應用程序始終會強制關閉,但我找不到麻煩。

該方法的輸入是CvCameraViewFrame.rgba()獲得的Mat。

private Mat findLargestRectangle(Mat original_image)
{
    Mat imgSource = original_image;

    // convert the image to black and white
    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);

    // convert the image to black and white does (8 bit)
    Imgproc.Canny(imgSource, imgSource, 50, 50);

    // apply gaussian blur to smoothen lines of dots
    Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);

    // find the contours
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    double maxArea = -1;
    int maxAreaIdx = -1;
    MatOfPoint temp_contour = contours.get(0); // the largest is at the
                                                // index 0 for starting
                                                // point
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    Mat largest_contour = contours.get(0);
    List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
    for (int idx = 0; idx < contours.size(); idx++)
    {
        temp_contour = contours.get(idx);
        double contourarea = Imgproc.contourArea(temp_contour);
        // compare this contour to the previous largest contour found
        if (contourarea > maxArea)
        {
            // check if this contour is a square
            MatOfPoint2f new_mat = new MatOfPoint2f(temp_contour.toArray());
            int contourSize = (int) temp_contour.total();
            Imgproc.approxPolyDP(new_mat, approxCurve, contourSize * 0.05, true);
            if (approxCurve.total() == 4)
            {
                maxArea = contourarea;
                maxAreaIdx = idx;
                largest_contours.add(temp_contour);
                largest_contour = temp_contour;
            }
        }
    }
    MatOfPoint temp_largest = largest_contours.get(largest_contours.size() - 1);
    largest_contours = new ArrayList<MatOfPoint>();
    largest_contours.add(temp_largest);

    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
    Imgproc.drawContours(imgSource, largest_contours, -1, new Scalar(0, 255, 0), 1);

    // create the new image here using the largest detected square

    Toast.makeText(getApplicationContext(), "Largest Contour: ", Toast.LENGTH_LONG).show();

    return imgSource;
}

以下是LogCat中的錯誤信息:

error opening trace file: No such file or directory (2)
Tegra Version detected: 0
FATAL EXCEPTION: Thread-14346
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at org.opencv.samples.tutorial2.Tutorial2Activity.findLargestRectangle(Tutorial2Activity.java:221)
at org.opencv.samples.tutorial2.Tutorial2Activity.onCameraFrame(Tutorial2Activity.java:169)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:328)
at java.lang.Thread.run(Thread.java:856)

Tutorial2Avctivity的第221行是:

MatOfPoint temp_contour = contours.get(0);

請告訴我錯誤是什么。 非常感謝!

這僅表示輪廓的ArrayList的大小為空。

我建議添加一個條件塊,以允許您的應用程序處理在輸入圖像中找不到輪廓的情況。

如果始終沒有得到輪廓,則無論輸入的圖像如何,您都可能需要查看文檔 (或Javadoc ):

圖像 –源,一個8位單通道圖像。 非零像素被視為1。 零像素保持為0,因此圖像被視為binary。 您可以使用compare(),inRange(),threshold(),adaptiveThreshold(),Canny()和其他圖像來創建灰度或彩色的二進制圖像。

Canny()函數中的閾值可能未產生任何邊緣。

如果您仍然沒有運氣,也許您可​​以使用此博客文章中的輸入圖像和值進行測試。

試試這個

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

您必須在下面的行之前向計數器添加一些值

MatOfPoint temp_contour = contours.get(0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM