Intel® Integrated Performance Primitives Developer Guide and Reference

ID 790148
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

GetPerspectiveTransform

Computes the perspective transform coefficients to map the source ROI to the quadrangle with the specified vertex coordinates.

Syntax

IppStatus ippiGetPerspectiveTransform(IppiRect srcRoi, const double quad[4][2], double coeffs[3][3]);

Include Files

ippi.h

Domain Dependencies

Headers: ippcore.h, ippvm.h, ipps.h

Libraries: ippcore.lib, ippvm.lib, ipps.lib

Parameters

srcRoi

Region of interest in the source image (of the IppiRect type).

quad

Vertex coordinates of the quadrangle, to which the source ROI is mapped by the perspective transform function.

coeffs

Output array. Contains the target perspective transform coefficients.

Description

This function operates with ROI (see ROI Processing in Geometric Transforms).

This function is used as a support function for the WarpPerspectiveNearest, WarpPerspectiveLinear, and WarpPerspectiveCubic functions. It computes the coefficientscoeffs that should be used by the function to map the source rectangular ROI to the quadrangle with the given vertex coordinates quad.

The first dimension [4] of the array quad[4][2] is equal to the number of vertices, and the second dimension [2] means x and y coordinates of the vertex. Quadrangle vertices have the following meaning:

quad[0] corresponds to the transformed top-left corner of the source ROI,

quad[1] corresponds to the transformed top-right corner of the source ROI,

quad[2] corresponds to the transformed bottom-right corner of the source ROI,

quad[3] corresponds to the transformed bottom-left corner of the source ROI.

Return Values

ippStsNoErr

Indicates no error. Any other value indicates an error.

ippStsSizeErr

Indicates an error condition if srcRoi has a size field with zero or negative value.

ippStsCoeffErr

Indicates an error condition if coefficient values are invalid.

ippStsRectErr

Indicates an error condition if width or height of the srcRoi is less than or equal to 1.

Example

/*******************************************************************************
* Copyright 2015 Intel Corporation.
*
*
* This software and the related documents are Intel copyrighted materials, and your use of them is governed by
* the express license under which they were provided to you ('License'). Unless the License provides otherwise,
* you may not use, modify, copy, publish, distribute, disclose or transmit this software or the related
* documents without Intel's prior written permission.
* This software and the related documents are provided as is, with no express or implied warranties, other than
* those that are expressly stated in the License.
*******************************************************************************/

// A simple example of perspective warping of an image
// using Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiGetPerspectiveTransform
//     ippiWarpPerspectiveSize
//     ippiWarpPerspectiveNearestInit
//     ippiWarpGetBufferSize
//     ippiWarpPerspectiveNearest_32f_C1R


#include <stdio.h>
#include "ipp.h"

#define WIDTH   8  /* source image width */
#define HEIGHT  8  /* source image height */

/* Next two defines are created to simplify code reading and understanding */
#define EXIT_MAIN exitLine:                                  /* Label for Exit */
#define check_sts(st) if((st) != ippStsNoErr) goto exitLine; /* Go to Exit if Intel(R) IPP function returned status different from ippStsNoErr */

static Ipp32f pSrc[WIDTH*HEIGHT] =/* Pointers to source images */
{
    1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f,
    1.f, 1.f, 1.f, 5.f, 5.f, 1.f, 1.f, 1.f,
    1.f, 1.f, 5.f, 5.f, 5.f, 5.f, 1.f, 1.f,
    1.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 1.f,
    1.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 1.f,
    1.f, 1.f, 5.f, 5.f, 5.f, 5.f, 1.f, 1.f,
    1.f, 1.f, 1.f, 5.f, 5.f, 1.f, 1.f, 1.f,
    1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f };

static const double quad[4][2] = /* Vertex coordinates of the quadrangle */
{
    { 0.0, 0.0 },
    { 8.0, 0.0 },
    { 6.0, 8.0 },
    { 0.0, 8.0 } };

/* Results of ippMalloc() are not validated because Intel(R) IPP functions perform bad arguments check and will return an appropriate status  */

int main(void)
{
    IppStatus status = ippStsNoErr;
    IppiWarpSpec* pSpec  = NULL; /* Pointer to the specification structure for the forward transform */
    IppiWarpSpec* pSpecB = NULL; /* Pointer to the specification structure for the backward transform */
    IppiSize srcSize = { WIDTH, HEIGHT }; /* Size of source ROI in pixels */
    IppiRect srcRoi = { 0, 0, WIDTH, HEIGHT }; /* Region of interest in the source image */
    IppiRect dstRoi = { 0, 0, WIDTH, HEIGHT }; /* Region of interest in the destination image */
    IppiPoint dstOffset  = { 0, 0 }; /* Offset of the destination image ROI with respect to the destination image origin */
    IppiPoint dstbOffset = { 0, 0 }; /* Offset of the back destination image ROI with respect to the back destination image origin */
    int srcStep = WIDTH * sizeof(Ipp32f), dstStep = srcStep, dstbStep = srcStep; /* Steps, in bytes, through the source/destination/back destination images */
    Ipp32f pDst[WIDTH*HEIGHT], pDstB[WIDTH*HEIGHT]; /* Pointers to destination/back destination images */
    IppiSize dstSize  = {WIDTH, HEIGHT}; /* Size of the destination image */
    IppiSize dstbSize = {WIDTH, HEIGHT}; /* Size of the back destination image */
    double coeffs[3][3] = {0};
    int specSize = 0,  initSize = 0,  bufSize = 0;  /* Buffer sizes for the forward transform */
    int specSizeB = 0, initSizeB = 0, bufSizeB = 0; /* Buffer sizes for the backward transform */
    Ipp8u* pBuffer = NULL, *pBufferB = NULL; /* Work buffers */
    IppiBorderType   borderType  = ippBorderTransp;
    IppiWarpDirection direction  = ippWarpForward;  /* Forward transformation direction */
    IppiWarpDirection directionB = ippWarpBackward; /* Backward transformation direction */

    check_sts( status = ippiSet_32f_C1R(2, pDst, dstStep, dstSize) )

    check_sts( status = ippiSet_32f_C1R(3, pDstB, dstbStep, dstbSize) )

    /* Computes perspective transform matrix */
    check_sts( status = ippiGetPerspectiveTransform(srcRoi, quad, (double(*)[3])coeffs) )

    /* Spec and init buffer sizes for the forward transform*/
    check_sts( status = ippiWarpPerspectiveGetSize(srcSize, srcRoi, dstSize, ipp32f, coeffs, ippNearest, direction,  borderType, &specSize, &initSize) )

    /* Spec and init buffer sizes for the backward transform*/
    check_sts( status = ippiWarpPerspectiveGetSize(dstSize, dstRoi, dstbSize, ipp32f, coeffs, ippNearest, directionB, borderType, &specSizeB, &initSizeB) )

    pSpec  = (IppiWarpSpec*)ippsMalloc_8u(specSize);

    pSpecB = (IppiWarpSpec*)ippsMalloc_8u(specSizeB);

    /* Filter initialization for the forward transform */
    check_sts( status = ippiWarpPerspectiveNearestInit(srcSize, srcRoi, dstSize, ipp32f, coeffs, direction,  1, borderType, NULL, 0, pSpec) )

    /* Filter initialization for the backward transform */
    check_sts( status = ippiWarpPerspectiveNearestInit(dstSize, dstRoi, dstbSize, ipp32f, coeffs, directionB, 1, borderType, NULL, 0, pSpecB) )

    /* Get work buffer size for the forward transform */
    check_sts( status = ippiWarpGetBufferSize(pSpec, dstSize, &bufSize) )

    /* Get work buffer size for the backward transform */
    check_sts( status = ippiWarpGetBufferSize(pSpecB, dstSize, &bufSizeB) )

    pBuffer  = ippsMalloc_8u(bufSize);

    pBufferB = ippsMalloc_8u(bufSizeB);

    /* Warp perspective processing for the forward transform */
    check_sts( status = ippiWarpPerspectiveNearest_32f_C1R(pSrc, srcStep, pDst, dstStep, dstOffset, dstSize, pSpec, pBuffer) )

    /* Warp perspective processing for the backward transform */
    check_sts( status = ippiWarpPerspectiveNearest_32f_C1R(pDst, dstStep, pDstB, dstbStep, dstbOffset, dstbSize, pSpecB, pBufferB) )

EXIT_MAIN
    ippsFree(pSpec);
    ippsFree(pSpecB);
    ippsFree(pBuffer);
    ippsFree(pBufferB);
    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return (int)status;
}

/*
pDst
1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
1.0  1.0  1.0  5.0  5.0  1.0  1.0  1.0
1.0  1.0  1.0  5.0  5.0  1.0  1.0  1.0
1.0  1.0  5.0  5.0  5.0  5.0  1.0  1.0
1.0  5.0  5.0  5.0  5.0  5.0  5.0  1.0
1.0  5.0  5.0  5.0  5.0  5.0  5.0  2.0
1.0  1.0  5.0  5.0  5.0  5.0  1.0  2.0
1.0  1.0  1.0  5.0  5.0  1.0  1.0  2.0

pDstB
1.0  1.0  1.0  1.0  1.0  1.0  1.0  3.0
1.0  1.0  1.0  5.0  5.0  1.0  1.0  3.0
1.0  1.0  5.0  5.0  5.0  5.0  1.0  3.0
1.0  5.0  5.0  5.0  5.0  5.0  5.0  1.0
1.0  5.0  5.0  5.0  5.0  5.0  5.0  2.0
1.0  1.0  5.0  5.0  5.0  5.0  1.0  1.0
3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0
3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0
*/