gpt4 book ai didi

在Android中应用OpenCV方法时出现java.lang.IndexOutOfBoundsException错误

转载 作者:行者123 更新时间:2023-12-01 13:19:47 25 4
gpt4 key购买 nike

该代码是关于使用 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 的大小为空。

我建议添加一个条件 block ,以使您的应用程序能够应对在输入图像中找不到轮廓的情况。

如果无论输入图像如何,您始终没有得到轮廓,您可能需要 review the documentation (或Javadoc):

image – Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated as binary . You can use compare() , inRange() , threshold() , adaptiveThreshold() , Canny() , and others to create a binary image out of a grayscale or color one.

也许 Canny() 函数中的阈值没有产生任何边缘。

如果您仍然没有运气,也许您可​​以使用输入图像和 this blog post 中的值进行测试.

关于在Android中应用OpenCV方法时出现java.lang.IndexOutOfBoundsException错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22141969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com