Java OpenCV,Android:从图像的特定区域或部分进行颜色检测?

Java OpenCV,Android:从图像的特定区域或部分进行颜色检测?,java,android,opencv,bitmap,Java,Android,Opencv,Bitmap,我想探测黑色物体下面的整个区域 我设法得到了黑色物体下面的矩形区域,就像这样 Point leftPoint = new Point(0,yValueBlack); //far left, black object height Point rightPoint = new Point(sourceBitmap.getWidth(),sourceBitmap.getHeight()); //btm right of entire bitmap Rect bottomRe

我想探测黑色物体下面的整个区域

我设法得到了黑色物体下面的矩形区域,就像这样

    Point leftPoint = new Point(0,yValueBlack); //far left, black object height
    Point rightPoint = new Point(sourceBitmap.getWidth(),sourceBitmap.getHeight()); //btm right of entire bitmap

    Rect bottomRect = new Rect(leftPoint,rightPoint);
其中
Rect bottomRect=new Rect(leftPoint,righpoint)
是我想要检测绿色条带的区域,如图所示。这是为了防止应用程序搜索图片上方的任何内容,并在其他对象位于帧中时导致错误

我有一个由一个边框包围的黑色对象的位图,我只想检测
Rect bottomRect=new Rect(leftPoint,righpoint)然后绘制绿色条带的边框

我定义垫子尺寸的方式如下
Mat sourceMat=new Mat(sourceBitmap.getWidth()、sourceBitmap.getHeight()、CvType.CV_8UC3)

然而,当我尝试使用相同的方法来定义我的垫子大小以适合黑色对象下方的矩形区域时,如下所示:
Mat crappedmat=新垫(bottomRect,CvType.CV_8UC3)
这会给我一个错误

下面是我的想法:

  • 检测并绘制黑色对象周围的边框(完成)

  • 查找黑色对象下方的矩形区域(RAT)

  • 在RAT内检测并绘制绿色对象周围的边框(我可以检测并绘制绿色对象的边框,但似乎无法在指定RAT内执行此操作)

  • 如下图所示显示位图(完成)

  • 编辑:

    检测到黑色对象后,将绘制边界矩形,并且当前位于位图上。在
    imageview
    中剪切
    ROI位图并尝试显示它(我将检测到此位图中最终剪切出来的绿色胶带。)会给我错误:

    CvException[org.opencv.core.CvException:cv::Exception:/build/master_pack-android/opencv/modules/core/src/matrix.cpp:483:error:(-215)0您可以“提取”图像的一部分,然后在整个提取区域中查找轮廓,然后更正查找轮廓的坐标。类似于:

    提取部分
    sourceMat

    // set top left position and dimensions of extracted area
    int topLeftX = ...;
    int topLeftY = ...;
    int width = ...;
    int height = ...;
    
    // create Rect object for extracted area
    Rect extractedRect = new Rect (topLeftX, topLeftY, width, height);
    
    // create Mat from sourceMat
    Mat extractedMat = new Mat(sourceMat, extractedRect);
    
    List<Rect> rectsOnSourcemat = new ArrayList<>();
    
    for (MatOfPoint contour : contours) {
        MatOfPoint2f contourPoints = new MatOfPoint2f(contour.toArray());
        RotatedRect boundingRect = Imgproc.minAreaRect(areaPoints);
        Point rotated_rect_points[] = new Point[4];
        boundingRect.points(rotated_rect_points);
    
        // correct coords here for sourceMat:
        for (int ixPoint = 0; ixPoint < 4; ixPoint++) {
            rotated_rect_points[ixPoint].x += topLeftX;  
            rotated_rect_points[ixPoint].y += topLeftY;  
        }
    
        // crate bounding rect for sourceMat
        Rect rect = Imgproc.boundingRect(new MatOfPoint(rotated_rect_points));
        rectsOnSourcemat.add(rect);
    }
    
    在整个提取区域上查找轮廓/矩形或其他内容:

    List<MatOfPoint> contours = new ArrayList<>();
    Imgproc.findContours(extractedMat, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    

    现在,在
    rectsOnSourcemat
    变量中,您将获得基于提取的区域对象的矩形列表,但已使用
    sourceMat

    Mat crappedmat=new Mat(bottomRect,CvType.CV_8UC3)的坐标;
    给出了一个错误,因为没有这个Mat构造函数。我想你要找的是这个:
    Mat(matm,Rect-roi)
    。下次,请看第一个问题。嘿@elouanline我编辑了这个问题以反映我当前的问题:x如果你能找出问题所在,我将非常感谢你的帮助。非常感谢
    List<Rect> rectsOnSourcemat = new ArrayList<>();
    
    for (MatOfPoint contour : contours) {
        MatOfPoint2f contourPoints = new MatOfPoint2f(contour.toArray());
        RotatedRect boundingRect = Imgproc.minAreaRect(areaPoints);
        Point rotated_rect_points[] = new Point[4];
        boundingRect.points(rotated_rect_points);
    
        // correct coords here for sourceMat:
        for (int ixPoint = 0; ixPoint < 4; ixPoint++) {
            rotated_rect_points[ixPoint].x += topLeftX;  
            rotated_rect_points[ixPoint].y += topLeftY;  
        }
    
        // crate bounding rect for sourceMat
        Rect rect = Imgproc.boundingRect(new MatOfPoint(rotated_rect_points));
        rectsOnSourcemat.add(rect);
    }