【问题标题】:OpenCV, Android: Detecting an object from an image instead of real-time detectionOpenCV,Android:从图像中检测对象而不是实时检测
【发布时间】:2017-06-19 06:53:19
【问题描述】:

我正在制作一个 android 应用程序,它可以从从视频中捕获的图像帧中检测对象。

openCV 中的示例应用程序只有实时检测的示例。

附加信息: -我正在使用 Haar 分类器

到目前为止,我将捕获的帧存储在 ImageView 数组中,如何使用 OpenCV 检测对象并在其周围绘制一个矩形?

for(int i=0 ;i <6; i++)
        {
            ImageView imageView = (ImageView)findViewById(ids_of_images[i]);

            imageView.setImageBitmap(retriever.getFrameAtTime(looper,MediaMetadataRetriever.OPTION_CLOSEST_SYNC));
            Log.e("MicroSeconds: ", ""+looper);
            looper +=10000;
        }

【问题讨论】:

    标签: android c++ android-studio opencv


    【解决方案1】:

    我希望你在你的项目中集成了 opencv 4 android 库。 现在,您可以使用 opencv 函数将图像转换为 Mat

    Mat srcMat = new Mat();
    Utils.bitmapToMat(yourbitmap,srcMat);
    

    一旦有了垫子,您就可以应用 opencv 函数从图像中查找矩形对象。 现在,按照代码检测矩形:

    Mat mGray = new Mat();
    cvtColor(mRgba, mGray, Imgproc.COLOR_BGR2GRAY, 1);
    Imgproc.GaussianBlur(mGray, mGray, new Size(3, 3), 5, 10, BORDER_DEFAULT);
    Canny(mGray, mGray, otsu_thresold, otsu_thresold * 0.5, 3, true); // edge detection using canny edge detection algorithm
    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(mGray,contours,hierarchy,Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    

    现在,您有来自 image 的轮廓。因此,您可以从中获取最大轮廓并使用 drawContour() 方法进行绘制:

    for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++){
    Imgproc.drawContours(src, contours, contourIdx, new Scalar(0, 0, 255)-1);
    }
    

    你就完成了!你可以参考这个链接: Android using drawContours to fill region

    希望对你有帮助!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2022-06-10
      • 2013-07-12
      • 2018-07-18
      • 2011-08-30
      相关资源
      最近更新 更多