android - 过滤掉其他对象 - Android Open CV library

标签 android opencv

我正在尝试识别行人交通信号。我正在将图像转换为 HSV 颜色空间,然后应用范围内函数以仅获得绿灯。这是我的原始图像 enter image description here

这是我的代码..

  public void onManagerConnected(int status) {
switch (status) {
    case LoaderCallbackInterface.SUCCESS:
        {
            Log.i(TAG, "OpenCV loaded successfully...................");
            Mat img = null;
            try {
                img = Utils.loadResource(getBaseContext(), R.drawable.glarrygreen, Highgui.CV_LOAD_IMAGE_COLOR);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Mat mHSV = new Mat();
            Mat mRgba2 = new Mat();
            Mat mHSVThreshed = new Mat();
            Imgproc.cvtColor(img, mHSV, Imgproc.COLOR_BGR2HSV, 3);
            //This works for red lights
            Core.inRange(mHSV, new Scalar(0, 64, 200), new Scalar(69, 255, 255), mHSVThreshed);
            //this works for green lights
            Core.inRange(mHSV, new Scalar(85, 64, 200), new Scalar(170, 255, 255),

                mHSVThreshed);
            List < MatOfPoint > contours = new ArrayList < MatOfPoint > ();
            Mat hierarchy = new Mat();
            Imgproc.findContours(mRgba2, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
            double maxArea = -1;
            int maxAreaIdx = -1;
            for (int idx = 0; idx < contours.size(); idx++) {
                Mat contour = contours.get(idx);
                double contourarea = Imgproc.contourArea(contour);
                if (contourarea > maxArea) {

                    maxArea = contourarea;
                    maxAreaIdx = idx;


                }
            }
            Imgproc.cvtColor(mHSVThreshed, img, Imgproc.COLOR_GRAY2BGR, 0);
            Imgproc.cvtColor(img, mRgba2, Imgproc.COLOR_BGR2RGBA, 0);
            Bitmap bmp = Bitmap.createBitmap(img.cols(), img.rows(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(mRgba2, bmp);
        }
}

} 这是我的输出图像 enter image description here

现在我需要过滤掉场景中的其他绿色信号。我该怎么做?如何获得场景中最突出的绿色信号。

编辑 1: 我正在尝试使用 findCountours() 方法,获取轮廓列表,遍历结果并获得最大的轮廓,然后仅显示最大的轮廓。如何删除较小的轮廓?

最佳答案

您可以尝试使用非极大值抑制算法来过滤您的二值图像。 这是一个 Java Demo of non-max suppression . 请注意,NMS 算法可以使用形态函数(腐 eclipse 和膨胀)进行编码。

编辑 opencv 似乎已经有一个具有以下原型(prototype)的 NMS 函数:

void nonMaximaSuppression(const Mat& src, const int sz, Mat& dst, const Mat mask)

EDIT2 来自 opencv 文档:对于 src 中的每个可能的 (sz x sz) 区域,一个元素是 src 的局部最大值当且仅当它严格大于与给定相交的窗口的所有其他元素元素。

尝试使用 50x50 的补丁 (sz := 50)

仅供引用 该方法源自以下论文:A. Neubeck 和 L. Van Gool。 “高效非极大值抑制”,ICPR 2006

关于android - 过滤掉其他对象 - Android Open CV library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862521/

相关文章:

javascript - 在 native 三星浏览器中处理锁屏事件

android - android.os.SystemProperties 在哪里存储它的键/值?

android - 如何使用自己的按钮登录 Facebook? (Facebook SDK 4.0)

python - 如何在 OpenCV 中绘制图像的 3D 直方图

c++ - 使用 Opencv 如何在消除框内打印的对象的同时检测图像中的框?

c++ - 在 OpenCV 中使用 H.264 压缩编写视频文件

android - 查询 realmObject,其中 realmList 等于另一个 realmList

java - 我们可以使用 Android Studio 进行 AOSP 开发吗?

android - java.lang.NullPointerException - 使用 opencv 和 android

c++ - 计算 Mat OpenCV 子集的总和