Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using OpenCV Mat images with Intel IPP?

I've recently started using Intel Performance Primitives (IPP) for image processing. For those who haven't heard of IPP, think of IPP as the analogue of MKL for image processing instead of linear algebra.

I've already implemented a somewhat complicated vision system in OpenCV, and I'd like to swap out some of the OpenCV routines (e.g. convolution and FFT) for faster IPP routines. My OpenCV code always uses the cv::Mat image data structure. However, based on the IPP code samples, it seems that IPP prefers the CIppiImage data structure.

My system does several image transformations in OpenCV, then I want to do a couple of things in IPP, then do more work in OpenCV. Here's a naive way to make OpenCV and IPP play nicely together:

 cv::Mat = load original image
 use OpenCV to do some work on cv::Mat
 write cv::Mat to file

 CIppiImage = read cv::Mat from file //for IPP
 use IPP to do some work on CIppiImage
 write CIppiImage to file

 cv::Mat = read CIppiImage from file
 use OpenCV to do more work on cv::Mat
 write final image to file

However, this is kind of tedious, and reading/writing files probably adds to the overall execution time.


I'm trying to make it more seamless to alternate between OpenCV and IPP in an image processing program. Here are a couple of things that could solve the problem:

  1. Is there a one-liner that would convert a cv::Mat to CIppiImage and vice versa?
  2. I am pretty familiar with the cv::Mat implementation details, but I don't know much about CIppiImage. Do cv::Mat and CIppiImage have the same data layout? If so, could I do something similar to the following cast? CIppiImage cimg = (CIppiImage)(&myMat.data[0])?
like image 375
solvingPuzzles Avatar asked Nov 20 '12 02:11

solvingPuzzles


1 Answers

There's a clean way to pass OpenCV data into an IPP function.

If we have an OpenCV Mat, we can cast *Mat.data[0] to an const Ipp<type>*. For example, if we're dealing with 8-bit unsigned char (8u) data, we can plug (const Ipp8u*)&img.data[0] into an IPP function. Here's an example using the ippiFilter function with the typical Lena image:

Mat img = imread("./Lena.pgm"); //OpenCV 8U_C1 image
Mat outImg = img.clone(); //allocate space for convolution results

int step = img.cols; //pitch
const Ipp32s kernel[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
IppiSize kernelSize = {3,3};
IppiSize dstRoiSize = {img.cols - kernelSize.width + 1, img.rows - kernelSize.height + 1};
IppiPoint anchor = {2,2};
int divisor = 1;

IppStatus status = ippiFilter_8u_C1R((const Ipp8u*)&img.data[0], step,
                                     (Ipp8u*)&outImg.data[0], step, dstRoiSize,
                                     kernel, kernelSize, anchor, divisor);

When I write outImg (from the above code) to a file, it gives the expected result: enter image description here

This matches the result I got when I ran the Nvidia version, nppiFilter, with the same parameters: enter image description here


I mentioned a structure called CIppiImage in the original question. CIppiImage just a simple wrapper for an array.

like image 199
solvingPuzzles Avatar answered Oct 02 '22 11:10

solvingPuzzles