简体   繁体   English

并行图像检测和相机预览OpenCV Android

[英]Parallel image detection and camera preview OpenCV Android

I'm using OpenCV to detect an image. 我正在使用OpenCV来检测图像。 Here is my problem: my function detect_image(mRgba) needs some time to perform operations and give some results. 这是我的问题:我的函数detect_image(mRgba)需要一些时间来执行操作并给出一些结果。 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. 虽然功能是计算相机预览被冻结,因为它只显示图像,当代码到达return inputFrame.rgba()我想知道如何使这些操作并行,功能将在后台计算,而相机预览正常工作。

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来处理您的图像:

AsyncTask reference page AsyncTask参考页面

A more friendly introduction can be found here: 可以在这里找到更友好的介绍:

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

while this: 而这个:

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

is a nice all-around introduction to multi-threading on Android. 是一个很好的全面介绍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 从你的“onCameraFrame”方法中检查你是否有一个AsyncThread来处理已经运行的图像
  • if the answer is "yes", just show mRgba in the preview window and return 如果答案是“是”,只需在预览窗口中显示mRgba并返回
  • 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. 如果答案为“no”,则启动一个新的AsyncThread并让它在mRgba上运行“detectImage”,确保结果保存在onPostExecute方法中。

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. 使用此算法,如果您的系统在以60fps(例如)进行预览时每秒可以检测到4个图像,则您将能够在单个处理器设备上获得关于每个20-30帧的新结果的平滑视频,当摄像机预览/显示是I / O密集型时,detect_image是CPU密集型的现实假设。

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...). 从HoneyComb开始,更精细的方法是考虑CPU中的核心数量(多核手机/平板电脑变得越来越普遍)并且并行启动N AsyncTask(每个核心一个),为每个核心提供不同的预览图像一个(可能使用线程池...)。

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. 如果将每个线程分开一个固定的延迟(大约是detectImage / N的持续时间),你应该得到一个恒定的结果流,其频率应该是单线程版本的倍数。

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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM