gpt4 book ai didi

java - OpenCV Java : Compare Bounding Rect's y value, 清除不需要的

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

在下图中,应用程序检测到多个“黑色”并在它们周围绘制了一个边框。现在我想比较每个矩形的rect3.tl().y值,只保留最低的一个,删除其他边界矩形。但我不确定如何着手做到这一点。

picure

代码:

Rect rectBlack = new Rect();
Bitmap roiBitmap = null;
Scalar green = new Scalar(0, 255, 0, 255);
Mat sourceMat = new Mat(sourceBitmap.getWidth(), sourceBitmap.getHeight(), CvType.CV_8UC3);
Utils.bitmapToMat(sourceBitmap, sourceMat);

Mat roiTmp = sourceMat.clone();
bitmapWidth = sourceBitmap.getWidth();
Log.e("bitmapWidth", "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
final Mat hsvMat = new Mat();
sourceMat.copyTo(hsvMat);

// convert mat to HSV format for Core.inRange()
Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV);

Scalar lowerb = new Scalar(85, 50, 40); // lower color border for BLUE
Scalar upperb = new Scalar(135, 255, 255); // upper color border for BLUE

Scalar lowerblack = new Scalar(0, 0, 0); // lower color border for BLACK
Scalar upperblack = new Scalar(180, 255, 40); // upper color border for BLACK

Scalar testRunL = new Scalar(60, 50, 40); // lower Green 83 100 51
Scalar testRunU = new Scalar(90, 255, 255); // upper Green

Core.inRange(hsvMat, lowerblack, upperblack, roiTmp); // select only blue pixels
// find contours
List<MatOfPoint> contours = new ArrayList<>();
List<RotatedRect> boundingRects = new ArrayList<>();
Imgproc.findContours(roiTmp, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

// find appropriate bounding rectangles
for (MatOfPoint contour : contours) {
MatOfPoint2f areaPoints = new MatOfPoint2f(contour.toArray());
RotatedRect boundingRect = Imgproc.minAreaRect(areaPoints);

double rectangleArea = boundingRect.size.area();

// test min ROI area in pixels
if (rectangleArea > 1300 ) {
Point rotated_rect_points[] = new Point[4];
boundingRect.points(rotated_rect_points);
Rect rect3 = Imgproc.boundingRect(new MatOfPoint(rotated_rect_points));


// test horizontal ROI orientation
if (rect3.height > rect3.width) {
Log.e("w,h", String.valueOf(rect3.width)+ " h " + String.valueOf(rect3.height));
double w = rect3.width;
double h = rect3.height;
double ratio= h/w;

Log.e("h:w ratio", String.valueOf(ratio));
Log.e("Black Area", String.valueOf(rect3.area()));
Imgproc.rectangle(sourceMat, rect3.tl(), rect3.br(), green, 3);
rectBlack = rect3;
Log.e("blackArea", String.valueOf(rect3.area()));
xBlack = rect3.br().x;
xBlackCenter = (rect3.br().x + rect3.tl().x) / 2;
yBlack = rect3.br().y;//bottom
battHeight = (rect3.br().y - rect3.tl().y); //batt height in pixel


}

}

}

最佳答案

您可以创建矩形列表:

List<Rect> rects = new ArrayList<>();

然后在您的for (MatOfPoint轮廓:轮廓)循环中将每个建立的矩形添加到该列表中:

// find appropriate bounding rectangles
for (MatOfPoint contour : contours) {
...
// test horizontal ROI orientation
if (rect3.height > rect3.width) {
...
rects.add(rect3)
}
}

然后使用方法找到最底部的矩形,如下所示:

public static Rect getBottomMostRect(List<Rect> rects) {
Rect bottomMostRect = null;

if (rects != null && rects.size() >= 1) {
Rect rect;
double minY;
int ixMinY = 0;

rect = rects.get(ixMinY);
minY = rect.tl().y;

for (int ix = 1; ix < rects.size(); ix++) {
rect = rects.get(ix);
if (rect.tl().y < minY) {
minY = rect.tl().y;
ixMinY = ix;
}
}

bottomMostRect = rects.get(ixMinY);
}

return bottomMostRect;
}

这样调用它:

Rect bottomMostRect = getBottomMostRect(rects)

或者将 getBottomMostRect() 实现直接添加到 for (MatOfPoint轮廓:轮廓) 循环中。

关于java - OpenCV Java : Compare Bounding Rect's y value, 清除不需要的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811097/

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