# fpe/app.cpp

/*******************************************************************************
    Copyright (c) 2021-2022 Qualcomm Technologies, Inc.
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without
    modification, are permitted (subject to the limitations in the disclaimer
    below) provided that the following conditions are met:
    
    * Redistributions of source code must retain the above copyright notice, this
        list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation
        and/or other materials provided with the distribution.
    * Neither the name of Qualcomm Technologies, Inc. nor the names of its
        contributors may be used to endorse or promote products derived from this
        software without specific prior written permission.
    
    NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
    THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
    CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
    NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
    @brief
    Program to run the FastADAS Feature Point Extraction pipeline(s).
    *******************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #ifdef USE_OPENCV
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/calib3d.hpp>
    #include <opencv2/opencv.hpp>
    #include <opencv2/core/mat.hpp>
    #endif
    
    #include <fadas.h>
    #include "../util/util.h"
    
    #define ST  // single- or multi-threaded versions
    #define ROI_FULL  // ROI set to full image or UL quarter image

    int32_t main( int32_t argc, char** argv )
    {
        int32_t retVal = 0;
        if( FadasInit( nullptr ) != FADAS_ERROR_NONE )  // initialize all FADAS features
        {
            UTIL_ERROR( "FadasInit failed\n" );
            return -1;
        }
    
        /**************************************************************************/
        // Ex. #1:  8-bit example
        const char* path = "stones_640x448.pgm";
        uint16_t barrier = 140;
        FadasImage_t srcImg = { 0 };
        if( UTIL_ReadPGM( path, srcImg, true, FADAS_BUF_TYPE_IN ) == false )
        {
            UTIL_ERROR( "reading file" );
            FadasDestroyImage( &srcImg );
            FadasDeInit();
            return -1;
        }
    
    #ifdef ROI_FULL
        FadasROI_t roi = { 0 };
    #else
        FadasROI_t roi = { srcImg.props.width / 2, 0, srcImg.props.width / 2, srcImg.props.height / 2 };
    #endif
    
        const uint32_t nCornersMax = 1024;
        float32_t FADAS_ALIGN128( ptX[nCornersMax] );
        float32_t FADAS_ALIGN128( ptY[nCornersMax] );
        uint32_t FADAS_ALIGN128( ptScore[nCornersMax] );
        uint32_t nCorners = 0;
    
        FadasFPE* fpe = FadasFPE_Create( srcImg.props );
        if( nullptr != fpe )
        {
    #ifdef ST
            nCorners = 0;
            if( FadasFPE_Run( FADAS_FPE_PIPELINE_FAST10ScoresNMS, fpe, &srcImg, &roi, barrier,
                              nCornersMax, &nCorners, ptX, ptY, ptScore ) != FADAS_ERROR_NONE )
            {
                UTIL_ERROR( "FadasFPE_Run failed\n" );
                retVal = -1;
            }
    #else
            int32_t n_threads_affinity[4] = {0, 1, 2, 3};
            void* wrkrs = FadasFPE_CreateWorkers( 4, n_threads_affinity, FADAS_FPE_PIPELINE_FAST10ScoresNMS );
            if( nullptr == wrkrs )
            {
                UTIL_ERROR( "creating FADAS_FPE_PIPELINE_FAST10ScoresNMS workers" );
                retVal = -1;
            }
            else
            {
                nCorners = 0;
                if( FADAS_ERROR_NONE != FadasFPE_RunMT( wrkrs, fpe, &srcImg, &roi, barrier, nCornersMax,
                                                        &nCorners, ptX, ptY, ptScore ) )
                {
                    UTIL_ERROR( "FadasFPE_RunMT failed\n" );
                    retVal = -1;
                }
    
                FadasFPE_DestroyWorkers( wrkrs );
            }
    #endif
    
            if( FADAS_ERROR_NONE != FadasFPE_Destroy( fpe ) )
            {
                UTIL_ERROR( "FadasFPE_DestroyWorkers failed\n" );
                retVal = -1;
            }
    
            printf( "%d corners found, 20 expected (8b)\n", nCorners );
    #ifdef USE_OPENCV
            cv::Mat img_ocv( srcImg.props.height, srcImg.props.width, CV_8UC1, srcImg.plane[0], srcImg.props.stride[0] );
            cv::Mat img_display( srcImg.props.height, srcImg.props.width, CV_8UC3 );
            cv::cvtColor( img_ocv, img_display, cv::COLOR_GRAY2RGB );
            cv::namedWindow( "Key Points (8b)", cv::WINDOW_AUTOSIZE );
    
            // display points
            for( uint32_t k = 0; k < nCorners; ++k )
                cv::circle( img_display, cv::Point( ptX[k], ptY[k] ), 1, cv::Scalar( 0, 255, 0 ), 2 );
    
                cv::imshow( "Key Points (8b)", img_display );
    #else
            printf( "Key Points (8b)\n" );
            for( uint32_t k = 0; k < nCorners; ++k )
                 printf( "{ %.01f, %.01f } = %d\n", ptX[k], ptY[k], ptScore[k] );
    #endif
        }

        if( 0 == retVal )
        {
            /**************************************************************************/
            // Ex. #2:  12-bit example
            FadasImage_t srcImg12b = { 0 };
            srcImg12b.props = srcImg.props;
            srcImg.props.format = FADAS_IMAGE_FORMAT_Y12;
            srcImg12b.props.stride[0] = srcImg.props.width * sizeof( uint16_t );
            srcImg12b.props.stride[0] = FADAS_MODULO128( srcImg12b.props.stride[0] );
            const int32_t memsz12b = srcImg12b.props.stride[0] * srcImg12b.props.height;
            srcImg12b.plane[0] = (uint16_t*)FadasMemAlloc( memsz12b, 128, nullptr );
            if( nullptr == srcImg12b.plane[0] )
            {
                UTIL_ERROR( "error allocating image 12b memory" );
                FadasDestroyImage( &srcImg );
                FadasDeInit();
                return -1;
            }
    
            memset( srcImg12b.plane[0], 0, memsz12b );
    
            uint8_t* img8b = (uint8_t*)srcImg.plane[0];
            uint16_t* img12b = (uint16_t*)srcImg12b.plane[0];
            for ( uint32_t y = 0; y < srcImg12b.props.height; ++y )
            {
                uint8_t* row8b = img8b + y * srcImg.props.stride[0];
                uint16_t* row12b = img12b + y * ( srcImg12b.props.stride[0] ) / sizeof( uint16_t );
                for ( uint32_t x = 0; x < srcImg12b.props.width; ++x )
                {
                    uint8_t pxl8 = row8b[x];
                    row12b[x] = uint16_t( pxl8 ) << 4;
                }
            }
    
            FadasError_e ret = FadasRegBuf( FADAS_BUF_TYPE_IN, srcImg12b.plane[0], size_t( memsz12b ) );
            if( FADAS_ERROR_NONE == ret )
            {
    #ifdef ROI_FULL
                roi = { 0 };
    #else
                roi = { srcImg12b.props.width / 2, 0, srcImg12b.props.width / 2, srcImg12b.props.height / 2 };
    #endif
                float32_t FADAS_ALIGN128( ptX12[nCornersMax] );
                float32_t FADAS_ALIGN128( ptY12[nCornersMax] );
                uint32_t FADAS_ALIGN128( ptScore12[nCornersMax] );
                uint32_t nCorners12 = 0;
    
                FadasFPE* fpe12 = FadasFPE_Create( srcImg12b.props );
                if( nullptr == fpe12 )
                {
                    UTIL_ERROR( "FadasFPE_Create(12b) failed\n" );
                    retVal = -1;
                }
                else
                {
    #ifdef ST
                    if( FadasFPE_Run( FADAS_FPE_PIPELINE_FAST10ScoresNMS12b, fpe12, &srcImg12b, &roi,
                                      barrier << 4, nCornersMax, &nCorners12, ptX12, ptY12,
                                      ptScore12 ) != FADAS_ERROR_NONE )
                    {
                        UTIL_ERROR( "FadasFPE_Run(12b) failed\n" );
                        retVal = -1;
                    }
    #else
                    int32_t n_threads_affinity[4] = {0, 1, 2, 3};
                    void* wrkrs12 = FadasFPE_CreateWorkers( 4, n_threads_affinity,
                                                            FADAS_FPE_PIPELINE_FAST10ScoresNMS12b );
                    if( nullptr == wrkrs12 )
                    {
                        UTIL_ERROR( "creating workers" );
                        retVal = -1;
                    }
                    else
                    {
                        if( FADAS_ERROR_NONE != FadasFPE_RunMT(
                                                    wrkrs12, fpe12, &srcImg12b, &roi, barrier << 4,
                                                    nCornersMax, &nCorners12, ptX12, ptY12, ptScore12) )
                        {
                            UTIL_ERROR( "FadasFPE_RunMT(12b) failed\n" );
                            retVal = -1;
                        }
    
                        FadasFPE_DestroyWorkers( wrkrs12 );
                    }
    #endif
    
                    if( FADAS_ERROR_NONE != FadasFPE_Destroy( fpe12 ) )
                    {
                        UTIL_ERROR( "FadasFPE_DestroyWorkers(12b) failed\n" );
                        retVal = -1;
                    }
    
                    printf( "%d corners found, 20 expected (12b)\n", nCorners12 );
    
    #ifdef USE_OPENCV
                    // use 8-bit image for display
                    cv::Mat img12_ocv( srcImg.props.height, srcImg.props.width, CV_8UC1, srcImg.plane[0], srcImg.props.stride[0] );
                    cv::Mat img12_display( srcImg.props.height, srcImg.props.width, CV_8UC3 );
                    cv::cvtColor( img12_ocv, img12_display, cv::COLOR_GRAY2RGB );
                    cv::namedWindow( "Key Points (12b)", cv::WINDOW_AUTOSIZE );
    
                    // display points
                    for( uint32_t k = 0; k < nCorners12; ++k )
                         cv::circle( img12_display, cv::Point( ptX12[k], ptY12[k] ), 1, cv::Scalar( 0, 255, 0 ), 2 );
    
                         cv::imshow( "Key Points (12b)", img12_display );
                         cv::waitKey( 0 );
    #else
                    printf( "Key Points (12b)\n" );
                    for( uint32_t k = 0; k < nCorners12; ++k )
                         printf( "{ %.01f, %.01f }\n", ptX12[k], ptY12[k] );
    #endif
                }
            }
    
            FadasDeregBuf(srcImg12b.plane[0]);
            FadasMemFree(srcImg12b.plane[0]);
        }
    
        FadasDestroyImage( &srcImg );
    
        FadasDeInit();
    
        return retVal;
    }
    Copy to clipboard

Last Published: Sep 30, 2024

[Previous Topic
uyuv\_nv12\_y/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/uyvy-nv12-y.md) [Next Topic
fpt/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/fpt.md)