2017-06-19 160 views
0

我正在制作一个android应用程序,它可以检测从视频捕获的图像帧中的对象。OpenCV,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; 
     } 

回答

2

我希望你已经在你的项目中集成了opencv 4 android库。现在 ,你可以转换图像使用OpenCV的功能

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

希望它会帮助!