使用OpenCV的图像垫与英特尔IPP? [英] Using OpenCV Mat images with Intel IPP?

查看:193
本文介绍了使用OpenCV的图像垫与英特尔IPP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用英特尔性能原件(IPP)图像处理最近开始。对于那些还没有听说过IPP是谁,以为​​IPP作为 MKL 的图像处理模拟代替线性代数。

我已经在OpenCV中实现了一个比较复杂的视觉系统,我想换一些OpenCV的程序(如卷积和FFT)的更快IPP例程。我的OpenCV code总是使用 CV ::垫图像数据结构。然而,基于对IPP code样品,似乎IPP prefers的 CIppiImage 的数据结构。

我的系统确实在OpenCV的一些图像变换,那么我希望做一对夫妇在IPP的事情,然后做更多的工作在OpenCV中。下面就来让OpenCV的一个幼稚的方式和IPP很好地一起玩:

  CV ::垫=负载原始图像
 OpenCV的使用上做简历一些工作::垫
 写简历::垫到文件 CIppiImage =读简历::垫从文件//为IPP
 使用IPP上做CIppiImage一些工作
 写CIppiImage到文件 CV ::垫=从文件中读取CIppiImage
 OpenCV的使用上做简历更多的工作::垫
 写入最终图像文件

然而,这是一种单调乏味,和读/写文件,可能增加了整体的执行时间。


我试图使其更无缝的图像处理程序的OpenCV与IPP之间交替。这里有两件事是可以解决这个问题:


  1. 有一个班轮,将一个 CV ::垫转换为 CIppiImage ,反之亦然<? / LI>
  2. 我是pretty熟悉的 CV ::垫实施细则,但我不知道很多关于 CIppiImage 。做 CV ::垫 CIppiImage 有相同的数据布局?如果是这样,我可以做类似下面的投什么? CIppiImage CIMG =(CIppiImage)(安培; myMat.data [0])?


解决方案

有对OpenCV中的数据传递到IPP功能的清洁方式。

如果我们有一个OpenCV的,我们可以投 * Mat.data [0] 常量伊普&LT;&型GT; * 。例如,如果我们处理的是8位无符号字符( 8U )的数据,我们可以将(常量Ipp8u *)及IMG。数据[0] 成IPP的功能。以下是使用典型的莉娜 ippiFilter 函数的示例图片:

  IMG垫= imread(./ Lena.pgm); // OpenCV的8U_C1图像
垫outImg = img.clone(); //分配卷积结果空间INT步= img.cols; //沥青
常量Ipp32s内核[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锚= {2,2};
INT除数= 1;IppStatus状态= ippiFilter_8u_C1R((常量Ipp8u *)及img.data [0],步骤,
                                     (Ipp8u *)及outImg.data [0],步骤,dstRoiSize,
                                     内核,kernelSize,锚,除数);

当我写 outImg (从上面的code)到一个文件,它提供了预期的结果:

这符合我,当我跑Nvidia的版本, nppiFilter ,用同样的参数结果:


我提到在原来的问题叫做 CIppiImage 结构。 CIppiImage 只是一个简单的包装器阵列。

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])?

解决方案

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:

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


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

这篇关于使用OpenCV的图像垫与英特尔IPP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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