我正在使用 OpenCV4Android 2.4.11 版,并且正在检测相机检索到的帧中的任何矩形形状。如下图 1 所示,我在检测到的对象周围绘制了一个黑色轮廓,我想要做的是,获取绘制轮廓的所有坐标,该坐标仅以黑色绘制。我尝试的是,如下面的code_1所示,我得到最大轮廓和最大轮廓的索引,并将它们分别保存在“largestContour”和“largest_contour_index”中。然后,我使用
Imgproc.drawContours(mMatInputFrame, contours, largest_contour_index, new Scalar(0, 0, 0), 2, 8, hierachy, 0, new Point());
然后我将最大轮廓的点传递给 FindCorners 类,因为我想找到用黑色绘制的轮廓的特定坐标,如下所示:
this.mFindCorners = new FindCorners(largestContour.toArray());
double[] cords = this.mFindCorners.getCords();
以下代码行:
double[] cords = this.mFindCorners.getCords();
应该给我较小的 x 坐标、最小的 y 坐标、最大的 x 坐标和最大的 y 坐标。但是当我围绕从“this.mFindCorners.getCords();”得到的坐标画一个圆圈时 我在下面的 image_2 中得到了类似的东西,这只是 BoundingRect 的角落。
实际上我不希望 boundingRect 中的任何坐标我想访问在黑色检测到的对象周围绘制的轮廓的坐标
请让我知道如何获取轮廓本身的坐标?
代码_1:
if (contours.size() > 0) {
for (int i = 0; i < contours.size(); i++) {
contour2f = new MatOfPoint2f(contours.get(i).toArray());
approxDistance = Imgproc.arcLength(contour2f, true) * .01;//.02
approxCurve = new MatOfPoint2f();
Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
points = new MatOfPoint(approxCurve.toArray());
double area = Math.abs(Imgproc.contourArea(points, true));
if (points.total() >= 4 && area >= 40000 && area <= 200000) {
if (area > largest_area) {
largest_area = area;
largest_contour_index = i;
pointsOfLargestContour = points;
largestContour = contours.get(i);
}
}
}
if (largest_area > 0) {
Imgproc.drawContours(mMatInputFrame, contours, largest_contour_index, new Scalar(0, 0, 0), 2, 8, hierachy, 0, new Point());
this.mFindCorners = new FindCorners(largestContour.toArray());
double[] cords = this.mFindCorners.getCords();
Core.circle(mMatInputFrame, new Point(cords[0], cords[1]), 10, new Scalar(255, 0, 0));
Core.circle(mMatInputFrame, new Point(cords[2], cords[3]), 10, new Scalar(255, 255, 0));
}
找角:
public class FindCorners {
private final static String TAG = FragOpenCVCam.class.getSimpleName();
private ArrayList<Double> mlistXCords = null;
private ArrayList<Double> mlistYCords = null;
private double mSmallestX;
private double mSmallestY;
private double mLargestX;
private double mLargestY;
private double[] mCords = null;
public FindCorners(Point[] points) {
this.mlistXCords = new ArrayList<>();
this.mlistYCords = new ArrayList<>();
this.mCords = new double[4];
Log.d(TAG, "points.length: " + points.length);
for (int i = 0; i < points.length; i++) {
this.mlistXCords.add(points[i].x);
this.mlistYCords.add(points[i].y);
}
//ascending
Collections.sort(this.mlistXCords);
Collections.sort(this.mlistYCords);
this.mSmallestX = this.mlistXCords.get(0);
this.mSmallestY = this.mlistYCords.get(0);
this.mLargestX = this.mlistXCords.get(this.mlistXCords.size() - 1);
this.mLargestY = this.mlistYCords.get(this.mlistYCords.size() - 1);
this.mCords[0] = this.mSmallestX;
this.mCords[1] = this.mSmallestY;
this.mCords[2] = this.mLargestX;
this.mCords[3] = this.mLargestY;
}
public double[] getCords() {
return this.mCords;
}
}
图像_1:
图像_2:
更新 我不想拥有边界矩形的坐标,我想要的是黑色轮廓的确切坐标。如图 3 所示,我从代码中获得的坐标是红色和黄色圆圈所在的位置..但我正在寻找能够访问黑线“轮廓”的坐标,因此我可以在其上绘制一些圆圈,如图所示图像_3。绿色的点只是为了告诉你我想要的坐标。
图像_3: