# fpt/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 Tracking 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 MT  // single- or multi-threaded versions
    
    int main( int argc, char** argv )
    {
        char str[256];
        uint32_t max_v;
        bool ans = true;
    
        const size_t balign = 128;
        if( FADAS_ERROR_NONE != FadasInit( nullptr ) )  // initialize all FADAS features
        {
            UTIL_ERROR( "FadasInit failed\n" );
            return -1;
        }
    
        const char* path0 = "fpt_0000000.pgm";
        const char* seq_str = "fpt_%07u.pgm";
        uint32_t seq_first = 0;
        uint32_t seq_last = 2;
        const int32_t n_lvls = 3;
        const int32_t srch_win = 31;
        const int32_t fpe_qual = 60;
    
        FadasImage_t srcImg0 = { 0 };
        if( UTIL_ReadPGM( path0, srcImg0, true, FADAS_BUF_TYPE_IN ) == false )
        {
            UTIL_ERROR( "reading file" );
            FadasDeInit();
            return -1;
        }
    
        FadasImage_t srcImg1 = { 0 };
        if( FadasCreateImage( FADAS_BUF_TYPE_IN, srcImg0.props.width, srcImg0.props.height, FADAS_IMAGE_FORMAT_Y, &srcImg1, balign ) != FADAS_ERROR_NONE )
        {
            UTIL_ERROR( "FadasCreateImage failed" );
            FadasDestroyImage( &srcImg0 );
            FadasDeInit();
            return -1;
        }
    
        int32_t max_iter = 20;
        float32_t max_eps = 0.01f;
    
        const uint32_t nCornersMax = 1024;
        float32_t FADAS_ALIGN128( ptX[nCornersMax] );
        float32_t FADAS_ALIGN128( ptY[nCornersMax] );
        int32_t   FADAS_ALIGN128( ptStatus[nCornersMax] );
    
        // Let feature set ROI to entire image unless we only want to focus in on one ROI (e.g., bounding box)
        FadasROI_t roi = { 0 };
    
        float32_t FADAS_ALIGN128( ptU[nCornersMax] );
        float32_t FADAS_ALIGN128( ptV[nCornersMax] );
        uint32_t FADAS_ALIGN128( ptScore[nCornersMax] );
        uint32_t nCorners = 0;
    
        FadasFPE* fpe = nullptr;
        FadasFPT* fpt = nullptr;
        if( ans != false )
        {
            fpe = FadasFPE_Create( srcImg0.props );
            if( nullptr == fpe )
            {
                UTIL_ERROR( "Could not create Extractor" );
                ans = false;
            }
            else
            {
                fpt = FadasFPT_Create( FADAS_FPT_PIPELINE_LK, srcImg0.props, srch_win, n_lvls );
                if( nullptr == fpt )
                {
                    UTIL_ERROR( "Could not create tracker" );
                    ans = false;
                }
            }
        }
    #ifdef MT
        void* fpe_wrkrs = nullptr;
        void* fpt_wrkrs = nullptr;
        if( ans != false )
        {
            int32_t n_threads_affinity[4] = {0, 1, 2, 3};
            fpe_wrkrs = FadasFPE_CreateWorkers( 4, n_threads_affinity, FADAS_FPE_PIPELINE_FAST10ScoresNMS );
            if( nullptr == fpe_wrkrs )
            {
                UTIL_ERROR( "creating FADAS_FPE_PIPELINE_FAST10 workers" );
                ans = false;
            }
    
            fpt_wrkrs = FadasFPT_CreateWorkers( 4, n_threads_affinity, FADAS_FPT_PIPELINE_LK );
            if( nullptr == fpt_wrkrs )
            {
                UTIL_ERROR( "creating FADAS_FPT_PIPELINE_LK workers" );
                ans = false;
            }
        }
    #endif

        // START sequence loop
        for( uint32_t i = seq_first; i <= seq_last; i++ )
        {
            if( false == ans )
            {
                break;
            }
    
            snprintf( str, sizeof(str), seq_str, i );
            const char* path1 = str;
    
            if( UTIL_ReadPGM( path1, srcImg1.props.width, srcImg1.props.height, max_v, (uint8_t*)(srcImg1.plane[0]), srcImg1.props.stride[0] ) == false )
            {
                UTIL_ERROR( "Cannot read %s", path1 );
                ans = false;
                break;
            }
    
            // pseudo re-detection
            if( nCorners < 20 )
            {
    #ifdef MT
                if( FADAS_ERROR_NONE != FadasFPE_RunMT( fpe_wrkrs, fpe, &srcImg0, &roi, fpe_qual, nCornersMax, &nCorners, ptU, ptV, ptScore ) )
                {
                    UTIL_ERROR( "FadasFPE_RunMT failed\n" );
                    ans = false;
                    break;
                }
    #else
                if( FADAS_ERROR_NONE != FadasFPE_Run( FADAS_FPE_PIPELINE_FAST10ScoresNMS, fpe, &srcImg0, &roi, fpe_qual, nCornersMax, &nCorners, ptU, ptV, ptScore ) )
                {
                    UTIL_ERROR( "FadasFPE_Run failed\n" );
                    ans = false;
                    break;
                }
    #endif
                printf( "%d corners detected\n", nCorners );
    
                for( uint32_t k = 0; k < nCorners; ++k )
                {
                    ptX[k] = ptU[k];
                    ptY[k] = ptV[k];
                    ptStatus[k] = 1;
                }
    #if defined(USE_OPENCV) && 0
                cv::Mat ocvimg0( srcImg0.props.height, srcImg0.props.width, CV_8UC1, srcImg0.plane[0], srcImg0.props.stride[0] );
                cv::Mat ocvimg0_dsply( srcImg0.props.height, srcImg0.props.width, CV_8UC3 );
                cv::cvtColor( ocvimg0, ocvimg0_dsply, CV_GRAY2RGB );
                cv::namedWindow( "Detected", cv::WINDOW_AUTOSIZE );
                for( int k = 0; k < nCorners; ++k )
                    cv::circle( ocvimg0_dsply, cv::Point( ptU[k], ptV[k] ), 1, cv::Scalar( 0, 255, 0 ), 2 );
    
                cv::imshow( "Detected", ocvimg0_dsply );
    #endif
            }

    #ifdef MT
            if( FADAS_ERROR_NONE != FadasFPT_RunMT( fpt_wrkrs, fpt, &srcImg0, &srcImg1, &roi, nCorners, ptX, ptY, ptStatus, max_iter, max_eps ))
            {
                UTIL_ERROR( "FadasFPT_RunMT failed\n" );
                ans = false;
                break;
            }
    #else
            if( FADAS_ERROR_NONE != FadasFPT_Run( fpt, &srcImg0, &srcImg1, &roi, nCorners, ptX, ptY, ptStatus, max_iter, max_eps ) )
            {
                UTIL_ERROR( "FadasFPT_Run failed\n" );
                ans = false;
                break;
            }
    #endif
    
    #ifdef USE_OPENCV
            cv::Mat ocvimg1( srcImg1.props.height, srcImg1.props.width, CV_8UC1, srcImg1.plane[0], srcImg1.props.stride[0] );
            cv::Mat ocvimg1_dsply( srcImg1.props.height, srcImg1.props.width, CV_8UC3 );
            cv::cvtColor( ocvimg1, ocvimg1_dsply, cv::COLOR_GRAY2RGB );
            cv::namedWindow( "Tracked", cv::WINDOW_AUTOSIZE );
            for( uint32_t k = 0; k < nCorners; ++k )
            {
                if( ptStatus[k] == 1 )
                    cv::circle( ocvimg1_dsply, cv::Point( ptX[k], ptY[k] ), 1, cv::Scalar( 0, 255, 0 ), 2 );
                else
                    cv::circle( ocvimg1_dsply, cv::Point( ptX[k], ptY[k] ), 1, cv::Scalar( 0, 0, 255 ), 2 );
            }
    
            cv::imshow( "Tracked", ocvimg1_dsply );
            cv::waitKey( 0 );
    #endif
    
            memcpy( srcImg0.plane[0], srcImg1.plane[0], srcImg1.props.stride[0] * srcImg1.props.height );
        }
    
    #ifdef MT
        if(nullptr != fpe_wrkrs )
        {
            FadasFPE_DestroyWorkers( fpe_wrkrs );
        }
        if(nullptr != fpt_wrkrs )
        {
            FadasFPT_DestroyWorkers( fpt_wrkrs );
        }
    #endif
        if(nullptr != fpt )
        {
            if( FADAS_ERROR_NONE != FadasFPT_Destroy( fpt ) )
            {
                UTIL_ERROR( "FadasFPT_Destroy failed\n" );
            }
        }
    
        if(nullptr != fpe )
        {
            if( FADAS_ERROR_NONE != FadasFPE_Destroy( fpe ) )
            {
                UTIL_ERROR( "FadasFPE_Destroy failed\n" );
            }
        }
    
        FadasDestroyImage( &srcImg0 );
        FadasDestroyImage( &srcImg1 );
        FadasDeInit();  // stop all active FADAS features
    
        return 0;
    }
    Copy to clipboard

Last Published: Sep 30, 2024

[Previous Topic
fpe/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/fpe.md) [Next Topic
fpt\_trcks/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/fpt-trcks.md)