【发布时间】:2015-07-05 13:52:42
【问题描述】:
对于初学者,我正在尝试在 OpenCV Android 中的矩阵上绘制轮廓和凸包。我跟随these codes 进入我的程序,如下所示:
/// 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);
}
基本上,输出将分别以绿色和红色绘制获得的轮廓和凸包。不幸的是,绘图变得太荒谬了,小轮廓和凸包也被绘制出来了。
编辑:这是图像示例。注意那些也出现的较小的红色和绿色区域,而我不希望它们出现。
问题是:我应该如何操作hullMOP 数组列表,以便获得指向MatOfPoint 的索引,并且只包含最大的轮廓和凸包?我试图通过尝试找到最大的区域(如下)来应用与Imgproc.boundingRect() 方法相同的想法:
for (int i = 0; i < contours.size(); i++) {
boundRect[i] = Imgproc.boundingRect(polyMOP.get(i));
}
Rect bigRect = new Rect();
for (int i = 0; i < boundRect.length; i++) {
if (bigRect.area() < boundRect[i].area()) {
bigRect = boundRect[i];
}
}
Core.rectangle(image3, bigRect.tl(), bigRect.br(), green, 2);
,但我没有受到任何打击。任何建议都会有所帮助。之前谢谢。
【问题讨论】:
标签: java android opencv image-processing arraylist