使用ipp API替换OpenCV功能的问题

时间:2014-11-04 10:02:30

标签: opencv intel-ipp

我正在尝试使用Integrated Performance Primitives(IPP)API替换我的opencv resize()函数。
我使用的是IPP 7.1版本                                                                                                                                                                                      但在完成下面的替换之后,我没有调整宽度和高度。

这是OpenCV函数:

   Mat src,dest;    
   resize(src, dest, Size(cvRound(8.0 * src.cols/ width), cvRound(8.0 * src.rows / height)), INTER_LINEAR );
   //dest rows and cols are getting updated

这是IPP功能:

    IppiSize srcsize,dstsize;
    IppiPoint zoomedOffset = {0, 0};
    Ipp32s zoomOpSpecSize = 0, zoomOpInitSize = 0, zoomOpBufSize = 0;
    Ipp8u * zoomOpBuf = NULL;
    IppiResizeSpec_32f * zoomOpSpec = NULL;

    srcsize.height = src.rows;
    srcsize.width = src.cols;

    dstsize.height = dest.rows;
    dstsize.width = dest.cols;

    ippiResizeGetSize_8u(srcsize,dstsize,ippLinear, 0, &zoomOpSpecSize, &zoomOpInitSize);
    zoomOpSpec = (IppiResizeSpec_32f *)ippsMalloc_8u(zoomOpSpecSize);

    ippiResizeLinearInit_8u(srcsize, dstsize,zoomOpSpec);

    ippiResizeGetBufferSize_8u(zoomOpSpec, dstsize, 1, &zoomOpBufSize);
    zoomOpBuf = ippsMalloc_8u(zoomOpBufSize);

    ippiResizeLinear_8u_C1R(src.data,
                            src.cols,
                            dest.data,
                            dest.cols,
                            zoomedOffset,
                            dstsize,
                            ippBorderRepl,
                            0,
                            zoomOpSpec,
                            zoomOpBuf
                            );

   ippsFree(zoomOpSpec);
   ippsFree(zoomOpBuf);

2 个答案:

答案 0 :(得分:0)

我已经用ipp调用替换了opencv调用,并且在时序方面得到了很好的结果: 以下是我在代码中为实现此目的所做的更改:

IppiSize srcsize,dstsize;
    Ipp32s zoomOpSpecSize = 0, zoomOpInitSize = 0, zoomOpBufSize = 0;
    Ipp8u * zoomOpBuf = NULL;
    IppiResizeSpec_32f * zoomOpSpec = NULL;
    IppStatus status = ippStsNoErr;
    Ipp8u *pSrcT, *pDstT;

    IppiPoint srcOffset = {0, 0};
    IppiPoint dstOffset = {0, 0};

    srcsize.height = src.rows;
    srcsize.width = src.cols;
    dstsize = (IppiSize&)Size(cvRound(8.0 * iw / width), cvRound(8.0 * ih / height));

    dest.rows = dstsize.height;
    dest.cols = dstsize.width;

    /* Spec and init buffer sizes */
    status = ippiResizeGetSize_8u(srcsize,dstsize,ippLinear, 0, &zoomOpSpecSize, &zoomOpInitSize);

    /* Memory allocation */
    zoomOpSpec = (IppiResizeSpec_32f *)ippsMalloc_8u(zoomOpSpecSize);


    /* Filter initialization */
    status =ippiResizeLinearInit_8u(srcsize, dstsize,zoomOpSpec);


    ippiResizeGetBufferSize_8u(zoomOpSpec, dstsize, 3, &zoomOpBufSize);
    zoomOpBuf = ippsMalloc_8u(zoomOpBufSize);
    dest.data = ippsMalloc_8u(dest.rows * dest.cols * 3);
    //dstOffset.y  = dstOffset.y + dstsize.height;
    pSrcT = (Ipp8u*)((char*)src.data + srcOffset.y * src.step);
    pDstT = (Ipp8u*)((char*)dest.data + dstOffset.y * dest.step);

    dest.datastart = pDstT;
    dest.dataend = pDstT + (dest.rows *dest.cols*3);
    dest.datalimit = dest.dataend;

    status = ippiResizeLinear_8u_C3R(pSrcT,
                            src.step,
                            pDstT,
                            dest.cols * 3,
                            dstOffset,                          
                            dstsize,
                            ippBorderRepl,
                            0,
                            zoomOpSpec,
                            zoomOpBuf
                            );
   ippsFree(zoomOpSpec);
   ippsFree(zoomOpBuf); 

答案 1 :(得分:-1)

你为什么这样做? 当使用CMake启用时,OpenCV功能已包含IPP功能。 OpenCV的目标是保持最终代码的清洁,但你的方式正好相反。

如果目标是调整图片大小,请避免使用IPP。坚持使用OpenCV的版本。简单,干净,高效(也归功于IPP)