3

I am trying to utilise OpenCV 4 on the Raspberry Pi Zero W, running Raspbian Buster (10). When doing any transformations on a captured image, the application exits with a floating-point exception.

I have cross-compiled OpenCV with various options, most notably the ENABLE_VFPV3 option, with no luck. I always get the floating-point exception. I use crosstool-ng for the cross-compilation on macOS, and CMake (through Jetbrains CLion, mostly).

OpenCV is configured with the following options before being built:

-DCMAKE_TOOLCHAIN_FILE=armv6-rpi-linux-gnueabi-toolchain.cmake  -DBUILD_JPEG=ON -DBUILD_PNG=ON -DBUILD_TIFF=ON -DWITH_V4L=ON -DENABLE_VFPV3=ON -DBUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF

OpenCV builds without any issues, and so do the applications that utilise the libraries. I don't build the shared libs, as I want the compiled application to not be dependent on 3rd party libraries to be installed.

The toolchain file that I use for cross-compilation of both OpenCV and the test application:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_CXX_FLAGS "-static-libstdc++")

set(CMAKE_SYSROOT /Volumes/crosstool/x-tools/armv6-rpi-linux-gnueabi/armv6-rpi-linux-gnueabi/sysroot)
set(CMAKE_OSX_SYSROOT /Volumes/crosstool/x-tools/armv6-rpi-linux-gnueabi/armv6-rpi-linux-gnueabi/sysroot)

set(CMAKE_C_COMPILER "/Volumes/crosstool/x-tools/armv6-rpi-linux-gnueabi/bin/armv6-rpi-linux-gnueabi-gcc")
set(CMAKE_CXX_COMPILER "/Volumes/crosstool/x-tools/armv6-rpi-linux-gnueabi/bin/armv6-rpi-linux-gnueabi-g++")

I have successfully compiled and run various applications with no issues up to this point, using this same setup.

A simple test application to reproduce the error:

#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/videoio/videoio.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    VideoCapture cap(0); //capture the video from web cam

    if ( !cap.isOpened() )  // if not success, exit program
    {
        cout << "Cannot open the web cam" << endl;
        return -1;
    }

    while (true)
    {
        Mat image;

        bool bSuccess = cap.grab();

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        cap.retrieve(image);

        cvtColor(image, image, COLOR_BGR2GRAY);

    }
    return 0;
}

The CMakefiles.txt:

cmake_minimum_required(VERSION 3.14)
project(vision)

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS "-static-libstdc++")

message( STATUS "Profile: ${PROFILE}" )

if ("${PROFILE}" STREQUAL "macos")
    include_directories(
            /usr/local/include/opencv4
    )
    link_directories(
            /usr/local/lib
            /usr/local/lib/opencv4/3rdparty
    )
elseif ("${PROFILE}" STREQUAL "arm")
    include_directories(
            ../opencv-arm/build/install/include/opencv4
    )
    link_directories(
            ../zlib/install/lib
            ../opencv-arm/build/install/lib
            ../opencv-arm/build/install/lib/opencv4/3rdparty
    )
else()
    message( ERROR "unsupported build profile" )
    return()
endif()

add_executable(vision main.cpp)

target_link_libraries(
        vision
        # video
        opencv_videoio
        opencv_video
        # gui
        opencv_highgui
        # image processing
        opencv_imgcodecs
        opencv_imgproc
        opencv_core
        libjpeg-turbo
        libjasper
        libtiff
        libpng
        libwebp
        ittnotify
        dl
        z
)

if ("${PROFILE}" STREQUAL "arm")
    target_link_libraries(
            vision
            tegra_hal
            pthread
    )
endif()

After attempting the color transformation (or any other transformation) on the RPi Zero, the application exits with a Floating point exception. Compiling and running on macOS works without any issues (-DPROFILE=macos).

My only thoughts at this stage are that the OpenCV compilation needs to do something special for the RPi Zero for the floating-point calculations to work. Any help/direction will be greatly appreciated.

bouwerp
  • 303
  • 2
  • 12
  • Typical - soon after I posted this question, a came across a solution. Using remote GDB, I was able to pinpoint a divide-by-zero in the OpenCV parallelism implementation (`modules/core/src/parallel.cpp`, line 316). It seems the `ParallelLoopBodyWrapperContext.nstripes` field is zero. Following [this post](https://stackoverflow.com/a/56281760/3177406), I re-compiled OpenCV with parallelism disabled, which allows the processing to succeed. A deeper investigation will probably be needed into why the parallelism fails on the Pi Zero, but for now this workaround is sufficient for me. – bouwerp Dec 29 '19 at 10:25
  • Could you submit an issue to [https://github.com/opencv/opencv/issues](https://github.com/opencv/opencv/issues)? – Catree Dec 29 '19 at 13:54
  • @Catree I will do that. The only processing that seems to work now is color conversion though - all the other other operations that I tried produce an empty/black image. – bouwerp Dec 30 '19 at 16:07
  • 1
    I opened an [issue](https://github.com/opencv/opencv/issues/16262). – bouwerp Dec 30 '19 at 16:27

1 Answers1

1

vfp v3 is not supported in Pi0 armv6