Java 如何从ArrayList获取最大值<;MatOfPoint>;?
首先,我尝试在OpenCV Android中在矩阵上绘制轮廓和凹凸。我按照我的程序如下所示:Java 如何从ArrayList获取最大值<;MatOfPoint>;?,java,android,opencv,image-processing,arraylist,Java,Android,Opencv,Image Processing,Arraylist,首先,我尝试在OpenCV Android中在矩阵上绘制轮廓和凹凸。我按照我的程序如下所示: /// Start contourImg Log.i(TAG, "called contourImg"); //init List<MatOfInt> hull = new ArrayList<MatOfInt>(); List<Point[]> hullPoints = new ArrayList&
/// Start contourImg
Log.i(TAG, "called contourImg");
//init
List<MatOfInt> hull = new ArrayList<MatOfInt>();
List<Point[]> hullPoints = new ArrayList<Point[]>();
List<MatOfPoint> hullMOP = new ArrayList<MatOfPoint>();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat overlay = input.clone();
Mat hierarchy = new Mat(mask.rows(), mask.cols(), CvType.CV_8UC1, new Scalar(0));
Point titik1 = new Point(0,0);
Imgproc.findContours(mask, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, titik1);
//Find the convex hull
for (int i = 0; i < contours.size(); i++) {
hull.add(new MatOfInt());
}
for (int i = 0; i < contours.size(); i++) {
Imgproc.convexHull(contours.get(i), hull.get(i), false);
}
// Convert MatOfInt to MatOfPoint for drawing convex hull
// Loop over all contours
for (int i = 0; i < contours.size(); i++) {
Point[] points = new Point[hull.get(i).rows()];
// Loop over all points that need to be hulled in current contour
for (int j = 0; j < hull.get(i).rows(); j++) {
int index = (int) hull.get(i).get(j, 0)[0];
points[j] = new Point(contours.get(i).get(index, 0)[0], contours.get(i).get(index, 0)[1]);
}
hullPoints.add(points);
}
// Convert Point arrays into MatOfPoint
for (int i = 0; i < hullPoints.size(); i++) {
MatOfPoint mop = new MatOfPoint();
mop.fromArray(hullPoints.get(i));
hullMOP.add(mop);
}
// Draw contours + hull results
for (int i = 0; i < contours.size(); i++) {
Imgproc.drawContours(overlay, contours, i, green);
Imgproc.drawContours(overlay, hullMOP, i, red);
}
,但我打不到。任何建议都会有帮助。谢谢。如果有点晚了,我很抱歉 我想您可以使用
Imgproc.contourArea
来检查hullMOP
和contours
的最大面积
以下是一个例子:
Mat maxContour = null;
double maxContourarea=0;
for (int idx = 0; idx < contours.size(); idx++) {
Mat contour = contours.get(idx);
double contourarea = Imgproc.contourArea(contour);
if (contourarea > maxContourarea){
maxContourarea = contourarea;
maxContour = contour;
}
}
Mat maxContour=null;
双面积=0;
对于(int idx=0;idx最大轮廓面积){
maxContourarea=contourarea;
maxContour=轮廓;
}
}
因此,
maxContour
将包含最大的轮廓,maxContourarea
是面积值。您可以使用Imgproc.RETR\u EXTERNAL
CV\u RETR\u EXTERNAL
仅检索最外层轮廓。它为所有轮廓设置hierarchy[i][2]=hierarchy[i][3]=-1
。
换句话说,使用Imgproc.findContours(掩码、轮廓、层次、Imgproc.RETR\u外部、Imgproc.CHAIN\u近似、titik1)
Mat maxContour = null;
double maxContourarea=0;
for (int idx = 0; idx < contours.size(); idx++) {
Mat contour = contours.get(idx);
double contourarea = Imgproc.contourArea(contour);
if (contourarea > maxContourarea){
maxContourarea = contourarea;
maxContour = contour;
}
}