如何使用 OpenCV 4 Android 检测彩色物体在普通(例如白色)背景上的位置?

How to detect the position of a colored object on a plain (e.g. white) background using OpenCV4Android?

OpenCV4Android, in ColorBlobDetectionActivity.java 的颜色斑点检测示例中,他们有一个方法 onTouch,它(在开始时)检测用户触摸的屏幕部分的颜色 - 因为它正在接收有关 MouseEvent event 参数中屏幕的哪一部分被触摸的信息。

我想写一个类似的方法,其中输出只是 blob 的 HSV 值(就像这个示例应用程序中用户触摸的屏幕部分),但我不想要用户通过触摸屏幕来指示,我更希望程序自动检测不同颜色的斑点(在普通背景上,例如白色背景)。

例如,在下图中,程序应该能够自动检测红色和绿色斑点的位置(而不是用户通过触摸斑点指示),然后计算 HSV(或RGB 我将从中计算 HSV) 那个 blob 的值。

我相信这应该可以使用 OpenCV4Android。问题是如何?应遵循哪些步骤(或应使用 API 中的哪些方法)?

来自 ColorBlobDetectionActivity.java 的相关截图:

public boolean onTouch(View v, MotionEvent event) {
        int cols = mRgba.cols();
        int rows = mRgba.rows();

        int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2;
        int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2;

        int x = (int)event.getX() - xOffset;
        int y = (int)event.getY() - yOffset;

        Log.i(TAG, "Touch image coordinates: (" + x + ", " + y + ")");

        if ((x < 0) || (y < 0) || (x > cols) || (y > rows)) return false;

        Rect touchedRect = new Rect();

        touchedRect.x = (x>4) ? x-4 : 0;
        touchedRect.y = (y>4) ? y-4 : 0;

        touchedRect.width = (x+4 < cols) ? x + 4 - touchedRect.x : cols - touchedRect.x;
        touchedRect.height = (y+4 < rows) ? y + 4 - touchedRect.y : rows - touchedRect.y;

        Mat touchedRegionRgba = mRgba.submat(touchedRect);

        Mat touchedRegionHsv = new Mat();
        Imgproc.cvtColor(touchedRegionRgba, touchedRegionHsv, Imgproc.COLOR_RGB2HSV_FULL);

        // Calculate average color of touched region
        mBlobColorHsv = Core.sumElems(touchedRegionHsv);

        ...

编辑:

范围内部分:

在语句 Utils.matToBitmap(rgbaFrame, bitmap); 上,出现以下异常:

在此代码段中,rgbaFrame 是从 onCameraFrame 返回的 Mat,它代表一个相机帧(在颜色斑点检测样本中是 mRgba,其 github link在题中)

private void detectColoredBlob () { 
     Mat hsvImage = new Mat(); 
     Imgproc.cvtColor(rgbaFrame, hsvImage, Imgproc.COLOR_RGB2HSV_FULL);

     Mat maskedImage = new Mat(); 
     Scalar lowerThreshold = new Scalar(100, 120, 120); 
     Scalar upperThreshold = new Scalar(179, 255, 255); 
     Core.inRange(hsvImage, lowerThreshold, upperThreshold, maskedImage);

     Mat dilatedMat= new Mat(); 
         //List<MatOfPoint> contours = new ArrayList<>(); 
         List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
         Mat outputHierarchy = new Mat(); 
         Imgproc.dilate(maskedImage, dilatedMat, new Mat() ); 
         Imgproc.findContours(dilatedMat, contours, outputHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); 

         Log.i(TAG, "IPAPP detectColoredBlob() outputHierarchy " + outputHierarchy.toString());

         /*for ( int contourIndex=0; contourIndex < contours.size(); contourIndex++ ) {
            //if(contours.get(contourIndex).size()>100) { //ERROR The operator > is undefined for the argument type(s) Size, int
                 Imgproc.drawContours ( rgbaFrame, contours, contourIndex, new Scalar(0, 255, 0), 4); 
            //} 
         }*/

     Bitmap bitmap = Bitmap.createBitmap(rgbaFrame.rows(), rgbaFrame.cols(), Bitmap.Config.ARGB_8888); 
     Utils.matToBitmap(maskedImage, bitmap); 
     imageView.setImageBitmap(bitmap); 
}

对于每种可能的斑点颜色,您可以执行 inRange() 操作以给出该范围内像素的二进制(黑白)垫。

Color detection in opencv

然后您可以使用 findContours() 找到斑点。您可以查看每个轮廓的大小,看看它是否满足您设置的某个 size/area 阈值。

-- 编辑--

如果背景总是白色,更简单的方法是立即将图像转换为灰度,然后进行二值化。参见

然后你可以找到轮廓,然后返回原始图像并计算你找到的轮廓内的平均颜色。

棘手的部分是将二进制阈值设置得恰到好处,以便彩色斑点在黑白图像中正确显示。