我正在制作一个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;
        }

最佳答案

我希望您在项目中集成了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);

现在,您具有图像轮廓。因此,您可以从中获取最大轮廓并使用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

希望对你有帮助!

07-27 15:45