我正在使用OpenCV4Android版本2.4.11,我正在檢測由相機檢索的幀中的任何矩形形狀。如下圖image_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我希望能夠訪問到被探測到的物體圍繞在巴爾克畫出的輪廓的座標
請讓我知道如何讓輪廓的座標的任何座標本身?
code_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));
}
FindCorners:
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;
}
}
IMAGE_1:
IMAGE_2:
更新 我不希望有邊界矩形的座標,我想有是,黑色輪廓的精確座標。如image_3所示,我從代碼中得到的座標是紅色和黃色圓圈所在的位置......但我正在尋找可以訪問黑色「輪廓線」的座標,以便我可以在其上畫一些圓圈IMAGE_3。綠色的斑點只是爲了向你展示我想要座標的位置。
IMAGE_3:
_should給我小了x座標,最小y座標,最大x座標和最大Y型coordinates._這些** **是邊界的座標框。你可能需要'approxPolyDP'後面的4個角,而不是像你在我的代碼中看到的 – Miki
@Miki,我有以下行:「Imgproc.approxPolyDP(contour2f,approxCurve,approxDistance,true);」是「approxCurve」中的黑色輪廓包絡? – user2121
黑色輪廓是'largestContour' – Miki