Skip to main content

Implement and deploy OpenCV sample application

Developer workflow

Prerequisites

Implement OpenCV rotate application with FastCV

See OpenCV rotate() for API usage details.
  1. Go to your SDK installation path on the Linux host and set the SDK environment by running the following command:
    source environment-setup-armv8-2a-qcom-linux
    
  2. Run the following command to set the SDKTARGETSYSROOT.
    export SDKTARGETSYSROOT={<absolute path to SDK install directory>}/sysroots
    
  3. Run the following commands to create and change the /opencv_rotate/ directory in the SDK install directory.
    mkdir opencv_rotate
    cd opencv_rotate
    
  4. Create the following configure.ac, Makefile.am, and opencv_rotate.cpp files in the /opencv_rotate/ directory.
    NoteThe JPG image referenced in this example is from https://github.com/opencv/opencv_extra/blob/4.x/testdata/stitching/boat1.jpg, and can be changed.
    • configure.ac
      AC_PREREQ([2.71])
      AC_INIT([opencv_rotate],[1.0.0])
      AM_INIT_AUTOMAKE([-Wall gnu foreign subdir-objects])
      AC_CONFIG_SRCDIR([Makefile.am])
      AC_CONFIG_HEADERS([config.h])
      AC_CONFIG_MACRO_DIR([m4])
      
      AC_ARG_WITH([sanitized-headers],
          [AS_HELP_STRING([--with-sanitized-headers=DIR],[location of the sanitized Linux headers])],
          [CPPFLAGS="$CPPFLAGS -I$withval"])
      
      AM_PROG_AS
      AM_PROG_AR
      AC_PROG_CC
      AM_PROG_CC_C_O
      AC_PROG_CPP
      AC_PROG_CXX
      LT_INIT
      AC_PROG_AWK
      AC_PROG_INSTALL
      AC_PROG_LN_S
      AC_PROG_MAKE_SET
      
      AC_ARG_WITH([opencv-path],
          [AS_HELP_STRING([--with-opencv-path=DIR],[path to opencv])],
          [OPENCV_PATH="$withval"])
      
      # The toolchain parameter passed by the bitbake is consumed here
      AC_ARG_WITH([toolchain-used],
              AS_HELP_STRING([--with-toolchain-used],
                      [Specify the toolchain-used for compilation]),
              [toolchain_used=$withval],
              toolchain_used=sdllvm)
      AM_CONDITIONAL(GCC_ENABLE, test "x$toolchain_used" = "xgcc")
      AM_CONDITIONAL(SDLLVM_ENABLE, test "x$toolchain_used" = "xsdllvm")
      
      AC_SUBST([OPENCV_PATH])
      
      AC_CONFIG_FILES([ Makefile])
      AC_OUTPUT
      
    • Makefile.am
      #ACLOCAL_AMFLAGS = -I m4
      # ---------------------------------------------------------------------------------
      #                       Make the libfastcvOPT library (libfastcvopt)
      # ---------------------------------------------------------------------------------
      
      common_flags = -O3 \
                     -fPIC \
                     -Wall \
                     -Wno-error \
                     -Wno-error=uninitialized \
                     -flax-vector-conversions \
                     -I${OPENCV_PATH}
      
      AM_CFLAGS = $(common_flags)
      
      AM_CPPFLAGS = $(common_flags)
      
      test_sources = opencv_rotate.cpp
      
      bin_PROGRAMS = opencv_rotate_test
      opencv_rotate_test_SOURCES  = $(test_sources)
      opencv_rotate_test_CPPFLAGS = $(AM_CPPFLAGS)
      opencv_rotate_test_CFLAGS = $(AM_CFLAGS)
      opencv_rotate_test_LDADD = -ldl -lpthread -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_fastcv
      
    • opencv_rotate.cpp
      #include<iostream>
      #include<chrono>
      #include "opencv2/opencv.hpp"
      #include "opencv2/imgcodecs.hpp"
      #include "opencv2/fastcv/scale.hpp"
      
      using namespace cv;
      
      int main()
      {
         Mat input = imread("/tmp/test/samples/boat1.jpg");
         Mat dst;
         auto start = std::chrono::high_resolution_clock::now();
         rotate(input, dst, ROTATE_90_CLOCKWISE);
         imwrite("/tmp/test/samples/output.jpg", dst);
         auto end = std::chrono::high_resolution_clock::now();
         std::chrono::microseconds time_span = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
         std::cout << "Total time taken: " << time_span.count() << " us.\n";
      
          return 0;
      }
      

Compile OpenCV rotate application

  1. Run the following commands.
    mkdir -p m4
    aclocal
    autoconf
    touch NEWS README AUTHORS ChangeLog
    autoreconf --install
    automake -a
    ./configure ${CONFIGURE_FLAGS} --with-opencv_path=${SDKTARGETSYSROOT}/qcom-armv8a/usr/include/opencv4
    
  2. Run the make command to compile the application.
    make
    
    The opencv_rotate_test test application generates in the /opencv_rotate/ directory.

Run the OpenCV rotate application

  1. To push the test bin, run the following command on the host.
    scp -r opencv_rotate_test root@{[IP-ADDR]}:/usr/bin/
    
    NoteIf you are unable to copy the file, remount using the following commands:
    mount -o remount,rw /usr
    
    Check permissions using:
    mount | grep /
    
  2. Run the following command to change directories (cd) to the path (for example ./images/boat1.jpg) of the test image.
    cd {<test image path>}
    
  3. Create a directory (for example /tmp/test/samples/) on the device to push the test image. This /tmp/test/samples/ directory is also the location of the output image in test code. Modify this directory according to the specified output location on your machine.
  4. Run the following command to push the test image to the device.
    scp -r boat1.jpg root@{[IP-ADDR]}:{/tmp/test/samples/}
    
  5. To start the test on the target device, run the following commands.
    chmod 777 /usr/bin/opencv_rotate_test
    /usr/bin/opencv_rotate_test
    
  6. To get the results, run the following command.
    scp -r root@{[IP-ADDR]}:/tmp/test/samples/output.jpg user2@{[HOST IP-ADDR]}:/workspace
    
    The /tmp/test/samples/ directory is the location of the output image in test code. Modify according to your specified output location. The following is the expected result.
    InputRotateOutput
    Rotate Input ImageROTATE_90_CLOCKWISERotate Output Image

Implement OpenCV resize application with FastCV extension

See OpenCV resize() for API usage details.
  1. Once the eSDK is installed, run the following commands to create and change the /opencv_resize_extension/ directory in the SDK install directory.
    mkdir opencv_resize_extension
    cd opencv_resize_extension
    
  2. Create the following configure.ac, Makefile.am, and opencv_resize_extension.cpp files in the /opencv_resize_extension/ directory.
    NoteThe PNG image referenced in this example is https://github.com/opencv/opencv/blob/4.x/samples/data/box_in_scene.png, but you can use another image if you want.
    • configure.ac
      AC_PREREQ([2.71])
      AC_INIT([opencv_resize],[1.0.0])
      AM_INIT_AUTOMAKE([-Wall gnu foreign subdir-objects])
      AC_CONFIG_SRCDIR([Makefile.am])
      AC_CONFIG_HEADERS([config.h])
      AC_CONFIG_MACRO_DIR([m4])
      AC_ARG_WITH([sanitized-headers],
          [AS_HELP_STRING([--with-sanitized-headers=DIR],[location of the sanitized Linux headers])],
          [CPPFLAGS="$CPPFLAGS -I$withval"])
      AM_PROG_AS
      AM_PROG_AR
      AC_PROG_CC
      AM_PROG_CC_C_O
      AC_PROG_CPP
      AC_PROG_CXX
      LT_INIT
      AC_PROG_AWK
      AC_PROG_INSTALL
      AC_PROG_LN_S
      AC_PROG_MAKE_SET
      AC_ARG_WITH([opencv-path],
          [AS_HELP_STRING([--with-opencv-path=DIR],[path to opencv])],
          [OPENCV_PATH="$withval"])
      # The toolchain parameter passed by the bitbake is consumed here
      AC_ARG_WITH([toolchain-used],
              AS_HELP_STRING([--with-toolchain-used],
                      [Specify the toolchain-used for compilation]),
              [toolchain_used=$withval],
              toolchain_used=sdllvm)
      AM_CONDITIONAL(GCC_ENABLE, test "x$toolchain_used" = "xgcc")
      AM_CONDITIONAL(SDLLVM_ENABLE, test "x$toolchain_used" = "xsdllvm")
      AC_SUBST([OPENCV_PATH])
      AC_CONFIG_FILES([ Makefile])
      AC_OUTPUT
      
    • Makefile.am
      #ACLOCAL_AMFLAGS = -I m4
      # ---------------------------------------------------------------------------------
      #                       Make the libfastcvOPT library (libfastcvopt)
      # ---------------------------------------------------------------------------------
      common_flags = -O3 \
                     -fPIC \
                     -Wall \
                     -Wno-error \
                     -Wno-error=uninitialized \
                     -flax-vector-conversions \
                     -I${OPENCV_PATH}
      AM_CFLAGS = $(common_flags)
      AM_CPPFLAGS = $(common_flags)
      test_sources = opencv_resize_extension.cpp
      bin_PROGRAMS = opencv_resize_test
      opencv_resize_test_SOURCES  = $(test_sources)
      opencv_resize_test_CPPFLAGS = $(AM_CPPFLAGS)
      opencv_resize_test_CFLAGS = $(AM_CFLAGS)
      opencv_resize_test_LDADD = -ldl -lpthread -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_fastcv
      
    • opencv_resize_extension.cpp
      #include<iostream>
      #include<chrono>
      #include "opencv2/opencv.hpp"
      #include "opencv2/imgcodecs.hpp"
      #include "opencv2/fastcv/scale.hpp"
      using namespace cv;
      int main()
      {
         Mat input = imread("/tmp/test/samples/box_in_scene.png", IMREAD_GRAYSCALE);
         Mat dst ;
         auto start = std::chrono::high_resolution_clock::now();
         fastcv::resizeDown(input, dst, cv::Size(), 0.5, 0.5);
         //resize(input, dst, Size(), 0.5, 0.5, INTER_LINEAR);
         imwrite("/tmp/test/samples/output.png", dst);
         auto end = std::chrono::high_resolution_clock::now();
         std::chrono::microseconds time_span = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
         std::cout << "Total time taken: " << time_span.count() << " us.\n";
         // if(!dst) std::cout << "Test not executed properly" << std::endl;
          return 0;
      }
      

Compile OpenCV resize application

  1. Run the following commands.
    mkdir -p m4
    aclocal
    autoconf
    touch NEWS README AUTHORS ChangeLog
    autoreconf --install
    automake -a
    ./configure ${CONFIGURE_FLAGS} --with-opencv_path=${SDKTARGETSYSROOT}/qcom-armv8a/usr/include/opencv4
    
  2. Run the make command to compile the application.
    make
    
    The opencv_resize_test test application generates in the /opencv_resize_extension/ directory.

Run OpenCV resize application

  1. To push the test bin, run the following command on the host.
    scp -r opencv_resize_test root@{[IP-ADDR]}:/usr/bin/
    
  2. Run the following command to change directories (cd) to the path of the test image (for example ./images/box_in_scene.png).
    cd {<test image path>}
    
  3. Create a directory on the device to copy the test image to (for example /tmp/test/samples/). This /tmp/test/samples/ directory is also the location of the output image in the sample code. Modify this directory according to the specified output location on your machine.
  4. Run the following command to copy the test image to the device.
    scp -r box_in_scene.png root@{[IP-ADDR]}:{/tmp/test/samples/}
    
  5. To start the test on the target device, run the following commands.
    chmod 777 /usr/bin/opencv_resize_test
    /usr/bin/opencv_resize_test
    
  6. To get the results, run the following command.
    scp -r root@{[IP-ADDR]}:/tmp/test/samples/output.png user2@{[HOST IP-ADDR]}:/workspace
    
    The /tmp/test/samples/ directory is the location of the output image in the sample code. Modify this location according to your specified output location. The following is the expected result.
    InputResize factorOutput
    fx=1/2 fy=1/2

OpenCV rotate function usage

Rotates a 2D array in multiples of 90 degrees. The rotate() function rotates the array in one of the following ways.
  • 90 degrees clockwise: rotateCode = ROTATE_90_CLOCKWISE
  • 180 degrees clockwise: rotateCode = ROTATE_180
  • 270 degrees clockwise: rotateCode = ROTATE_90_COUNTERCLOCKWISE

Parameters
TypeParameterDescription
InputArraysrcInput array.
OutputArraydstOutput array of the same type as the source.
The size is the same with ROTATE_180 and the rows and columns are switched for ROTATE_90_CLOCKWISE and ROTATE_90_COUNTERCLOCKWISE.
intcodeAn enum to specify how to rotate the array; see the RotateFlags enum.
RotateFlags:
  • ROTATE_90_CLOCKWISE
  • ROTATE_180
  • ROTATE_90_COUNTERCLOCKWISE
For more information about rotate(), see the full API documentation

OpenCV resize function usage

Input to the OpenCV resize() function is a source (src) image in the form of CVMatrix.
NoteOnly grayscale images are supported.
Output is a destination image in the form of CVMatrix. The FastCV namespace is used to call the FastCV extension APIs. The resizeDown() extension API requires the input image and destination image as parameters.

Parameters
TypeParameterDescription
InputArrayinputInput image.
OutputArraydstOutput image.
SizedsizeThe desired size of the output image. If empty, it is calculated using inv_scale_x and inv_scale_y.
doubleinv_scale_xThe inverse scaling factor for the width. If dsize is provided, this parameter is ignored.
doubleinv_scale_yThe inverse scaling factor for the height. If dsize is provided, this parameter is ignored.
For more information about resize(), see the full API documentation.