How to use OpenCV functions in Metal on iOS? - ios

I have developed the Xcode project that uses OpenCV functions for image processing when the iPhone camera live stream.
It takes some time to process one frame and doesn't look like real time.
Is it possible to accelerate the calculation by integrating OpenCV and Metal?
For example, OpenCV function "grabCut" takes more than 1 second to detect certain foreground objects.
How can I reduce the processing time down to 10ms at least using Metal?

You can't call OpenCV functions from Metal.
If you want to speed up this algorithm, you could try porting it to Metal but that's only an option if the algorithm -- or major parts of it -- are highly parallel.
Now, it looks like grabCut has a CUDA implementation (which I found by googling for "grabcut cuda"), which means that implementing this in Metal might actually be worth doing. If you can find the CUDA source code, it's usually a relatively straightforward port.

Related

Replace Opencv Fillpoly functions with Halide Funcs

I'm trying to realize some opencv functions by Halide, one of the difficulty I have met is how to write the cv::fillpoly in Halide. This function, in opencv, fills a polygon according to the given vertex of this polygon. The details in Opencv: https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html
I wonder if it's possible to realize it in Halide?
It is possible, but likely difficult and not obviously productive as polygon rendering does not have a regular static data parallel pattern and is already really well optimized in various places. It would be interesting to see if it can be done and made performant but it is a lot of work and isn't obviously going to be as fast or faster than existing graphics libraries. Especially if running on GPU hardware where it has to compete with hardware rasterization. I'd look into using define_extern to call out to existing rendering routines.

pi3 GPU functionaly with camera

In my project I have 2 main tasks – image recognition for the camera frames and saving the vidoes.
I think to use pi GPU here for accelerate this.
Is it possible using pi GPU get the frames from camera, than convert and save them in SD card?
And meantime pass the frames to processor for doing image recognition?
Can someone please provide some info about how I can use GPU and processor separately and what video-camera related operations can GPU do.
Thanks
I think you really just want to use the umat class. It makes a lot of opencvs functions run on the GPU (if possible). It can in some cases release a lot of cpu time for other tasks.
Some opencv functions are also often multiple times faster when run on a GPU.
See opencv-transparent-api
You can also easily find examples using it here on stack overflow.

OpenCV BackgroundSubtratorMOG2

I've finished an algorithm aimed to foreground extraction based on video recently, but it processes too slowly per frame. There is an algorithm based on Mixed Gaussian Model named BackgroundSubtractorMOG2 in OpenCV3.0 and I find it processes quickly as nearly 15 times as mine per frame. I just wonder is it accelerated by OpenCL on GPU ? Or it is just run on CPU? p.s. I've seen some source codes of it and noticed there are OpenCL blocks but I'm not sure since I'm fresh. I will be very appreciated if anyone could help me figure it out!
If you look at the API page here You will find the line:
The function implements a sparse iterative version of the Lucas-Kanade optical flow in pyramids. See [Bouguet00]. The function is parallelized with the TBB library.
The TBB library is a parallization library and is used to "write parallel C++ programs that take full advantage of multicore performance" - this means that it is using more than just one CPU at a time, a much quicker way of processing. This can be seen on lines like this (Line 566):
parallel_for_(Range(0, image.rows),
MOG2Invoker(image, fgmask,
(GMM*)bgmodel.data,
(float*)(bgmodel.data + sizeof(GMM)*nmixtures*image.rows*image.cols),
bgmodelUsedModes.data, nmixtures, (float)learningRate,
(float)varThreshold,
backgroundRatio, varThresholdGen,
fVarInit, fVarMin, fVarMax, float(-learningRate*fCT), fTau,
bShadowDetection, nShadowDetection));

Re-write openCV functions using Cuda only

I have my code written in c++ and I used openCV functions for Image processing tasks.
I want to run my code in GPU (using cuda) to read a camera/stream inputs and do the image processing tasks in each frame in parallel.
I've read somewhere that I can't include the openCV functions in a .cu code, since the NVCC can't compile openCv functions (please correct me if this is not true)
I found the openCV gpu module in the openCV documentation, but I don't want to run the whole function in parallel, I want the whole algorithm to be processed in parallel ( in other way, include openCv in cuda not vise versa), so I've thought about writing all of my openCV functions in cuda, But I'm newbie to cuda.
My questions:
1- Are there cuda functions that can be used instead of openCv following functions :
split, inRange
fillHoles
Morphology (erosion, dilation, closing)
Countours (findContours, moments, boundingRect, approxPolyDP)
Drawing function (drawContours, rectangle, circle)
kmeans (or any other function for clustering)
I found some of them in Github, but still didn't test any, any documentation will be highly appreciated.
2- Does cuda reads only .pgm image format, and should I convert the .jpg frames before copying them to the device? Is it impossible to read the camera input directly to GPU global memory?
3- Do you suggest keeping my code in openCV and use another libraries for parallel processing like openCL? or use CPU (instead of GPU) for parallel processing using OpenMP? what might be the best option I should go with?
Before you begin down this route, i would recommend that you go through the first few lessons in this tutorial:
https://www.udacity.com/course/cs344
Then you will have a better idea about if a GPU is suitable for what your application requires.
In any case, openCV 1.0 is mostly in C, and cuda kernels are in C, so maybe you could try wrapping some of those into cuda kernels
Cheers

Image Processing on CUDA or OpenCV?

I need to develop an image processing program for my project in which I have to count the number of cars on the road. I am using GPU programming. Should I go for OpenCV program with GPU processing feature or should I develop my entire program on CUDA without any OpenCV library?
The algorithms which I am using for counting the number of cars is background subtraction, segmentation and edge detection.
You can use GPU functions in OpenCV.
First visit the introduction about this : http://docs.opencv.org/modules/gpu/doc/introduction.html
Secondly, I think above mentioned processes are already implemented in OpenCV optimized for GPU. So It will be much easier to develop with OpenCV.
Canny Edge Detection : http://docs.opencv.org/modules/gpu/doc/image_processing.html#gpu-canny
PerElement Operations (including subtraction): http://docs.opencv.org/modules/gpu/doc/per_element_operations.html#per-element-operations
For other functions, visit OpenCV docs.
OpenCV, no doubt, has the biggest collection of Image processing functionality and recently they've started porting functions to CUDA as well. There's a new GPU module in latest OpenCV with few functions ported to CUDA.
Being said that, OpenCV is not the best option to build a CUDA based application as there are many dedicated CUDA libraries like CUVI that beat OpenCV in Performance. If you're looking for an optimized solution, you should also give them a try.

Resources