并行图像检测和相机preVIEW OpenCV的机器人 [英] Parallel image detection and camera preview OpenCV Android

查看:330
本文介绍了并行图像检测和相机preVIEW OpenCV的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenCV的检测图像。这里是我的问题:我的功能 detect_image(mRgba)需要一些时间来进行操作,并给予了一定的成效。虽然功能是计算相机preVIEW被冻结,因为它只能显示图像时code到达返回inputFrame.rgba()我想知道如何使这些操作并行,功能将被计算在后台,而摄像机preVIEW正在与正常速度。

 公共垫onCameraFrame(CvCameraViewFrame inputFrame){
mRgba = inputFrame.rgba();
detect_image(mRgba);
返回inputFrame.rgba();
}
 

解决方案

要只得到一尝在并行,简单的办法是只使用一个AsyncTask的处理图像:

AsyncTask的参考页

一个更友好的介绍可以在这里找到:

<一个href="http://android-developers.blogspot.co.il/2010/07/multithreading-for-performance.html">http://android-developers.blogspot.co.il/2010/07/multithreading-for-performance.html

而这样的:

<一个href="http://developer.att.com/developer/forward.jsp?passedItemId=11900176">http://developer.att.com/developer/forward.jsp?passedItemId=11900176

是一个很好的全方位介绍了多线程在Android上。

如果你想刚上手的,一个简单的算法应该是这样的:

  • 从在您的onCameraFrame的方法检查,如果你有一个AsyncThread用于处理图像已经运行
  • 如果答案是是,只是显示mRgba在preVIEW窗口,返回
  • ,如果答案是否开始新AsyncThread并让它上mRgba运行detectImage,在确认的结果保存在onPostExecute方法

使用这种算法,如果你的系统可以检测到4帧每秒,而采取了preVIEW在60fps(例如),你就可以得到一个流畅的视频大约每20-30新的结果框架上单处理器的设备,该detect_image现实的假设下是CPU密集型的,而相机preVIEW /展出的I / O密集​​型。

   捕获:xxxxxxxxxxxxxxxxxxxx ...
   处理:1 ....... 1 ....... 1 ....... 1 ..... 1 ....... 1 ....
                时间------------------------------------>

与蜂窝开始,一个更精细的方法是考虑到在CPU核心数量(多核手机/平板电脑正变得越来越普遍),并开始ñAsyncTask的并行(每个核心),喂食不同的preVIEW图像的每一个(也许用一个线程池...)。

如果您通过一个固定的延时(约detectImage / N的持续时间)分隔每个线程,你应该得到的频率结果络绎不绝这应该是单线程的版本多。

   捕获:xxxxxxxxxxxxxxxxxxxx ...
   处理:1.2.3.4.1.2.3.4.1.2.3.4.1.2.3.4.1.2.3.4 ....
                时间------------------------------------>

希望这有助于

I'm using OpenCV to detect an image. Here is my problem: my function detect_image(mRgba) needs some time to perform operations and give some results. While function is computing camera preview is frozen because it only shows image when code reaches return inputFrame.rgba() I would like to know how to make those operation parallel, function will be computing in a background while camera preview is working with normal speed.

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
detect_image(mRgba);
return inputFrame.rgba();
}

解决方案

To just get a taste at parallelization, the simple approach would be to just use an AsyncTask to process your images:

AsyncTask reference page

A more friendly introduction can be found here:

http://android-developers.blogspot.co.il/2010/07/multithreading-for-performance.html

while this:

http://developer.att.com/developer/forward.jsp?passedItemId=11900176

is a nice all-around introduction to multi-threading on Android.

If you want to just get started, a simple algorithm should work like this:

  • from within your "onCameraFrame" method check if you have an AsyncThread for processing the image which is already running
  • if the answer is "yes", just show mRgba in the preview window and return
  • if the answer is "no" start a new AsyncThread and let it run "detectImage" on mRgba, making sure that the results are saved in the onPostExecute method.

With this algorithm, if your system can detect 4 images per second while taking a preview at 60fps (for example), you will be able to get a smooth video with a new result about each 20-30 frames on a single processor device, under the realistic assumption that detect_image is CPU intensive while the camera preview/display are I/O intensive.

   Capture:     x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x....
   Processing:  1.......1.......1.......1.....1.......1....
                time ------------------------------------>

Starting with HoneyComb, a more refined approach would be to account for the number of cores in your CPU (multicore phones/tablets are becoming increasingly common) and start N AsyncTask in parallel (one for each core), feeding a different preview image to each one (maybe using a thread pool...).

If you separate each thread by a fixed delay (about the duration of detectImage/N ), you should get a constant stream of results with a frequency that should be a multiple of the single threaded version.

   Capture:     x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x....
   Processing:  1.2.3.4.1.2.3.4.1.2.3.4.1.2.3.4.1.2.3.4....
                time ------------------------------------>

Hope this helps

这篇关于并行图像检测和相机preVIEW OpenCV的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆