Home > Blockchain >  opencv build in debug mode with Ubuntu 18.04 for c/c code debugging
opencv build in debug mode with Ubuntu 18.04 for c/c code debugging

Time:03-04

I tried to installed latest opencv 4.5.5 in debug mode (for c/cpp code). So followed below steps -

$ sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
    libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
    libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
    gfortran openexr libatlas-base-dev python3-dev python3-numpy \
    libtbb2 libtbb-dev libdc1394-22-dev
    
$ mkdir ~/opencv_build && cd ~/opencv_build
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git

$ cd ~/opencv_build/opencv
$ mkdir build && cd build

$ cmake -D CMAKE_BUILD_TYPE=DEBUG \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..
    
$ make -j8
    
$ sudo make install
    
$ pkg-config --modversion opencv4
    4.5.5

I have installed successfully opencv in my system.

I want to execute opencv optical flow sample code in .cpp .

I want to enter inside goodFeaturesToTrack function and wants to check its implementation. But with current library I am not able to go inside this function. The execute just going to the next line.

Query:

  1. How can I build the opencv in debug mode correctly for C/CPP sample code debugging.

There are lots of links available on internet for using cmake -D CMAKE_BUILD_TYPE=DEBUG flag. But that did not work for me .

  1. How to cross check weather opencv libraries are in debug mode or release mode.

Sample code for optical flow code -

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
            "Using OpenCV version %s\n" << CV_VERSION << "\n"
            << endl;

    cout << "\nHot keys: \n"
            "\tESC - quit the program\n"
            "\tr - auto-initialize tracking\n"
            "\tc - delete all the points\n"
            "\tn - switch the \"night\" mode on/off\n"
            "To add/remove a feature point click it\n" << endl;
}

Point2f point;
bool addRemovePt = false;

static void onm ouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
    if( event == CV_EVENT_LBUTTONDOWN )
    {
        point = Point2f((float)x,(float)y);
        addRemovePt = true;
    }
}

int main( int argc, char** argv )
{
    VideoCapture cap;
    TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03);
    Size subPixWinSize(10,10), winSize(31,31);

    const int MAX_COUNT = 500;
    bool needToInit = false;
    bool nightMode = false;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc == 2 )
        cap.open(argv[1]);

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return 0;
    }

    help();

    namedWindow( "LK Demo", 1 );
    setMouseCallback( "LK Demo", onm ouse, 0 );

    Mat gray, prevGray, image;
    vector<Point2f> points[2];

    for(;;)
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;

        frame.copyTo(image);
        cvtColor(image, gray, CV_BGR2GRAY);

        if( nightMode )
            image = Scalar::all(0);

        if( 1 ) //needToInit )
        {
            // automatic initialization
            goodFeaturesToTrack(gray, points[1],
                                MAX_COUNT, 0.01, 10,
                                Mat(), 3,
                                0, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            addRemovePt = false;
        }
        else if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
                gray.copyTo(prevGray);
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
                                 3, termcrit, 0, 0.001);
            size_t i, k;
            for( i = k = 0; i < points[1].size(); i   )
            {
                if( addRemovePt )
                {
                    if( norm(point - points[1][i]) <= 5 )
                    {
                        addRemovePt = false;
                        continue;
                    }
                }

                if( !status[i] )
                    continue;

                points[1][k  ] = points[1][i];
                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
            }
            points[1].resize(k);
        }

        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
        {
            vector<Point2f> tmp;
            tmp.push_back(point);
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
            addRemovePt = false;
        }

        needToInit = false;
        imshow("LK Demo", image);

        char c = (char)waitKey(10);
        if( c == 27 )
            break;
        switch( c )
        {
        case 'r':
            needToInit = true;
            break;
        case 'c':
            points[1].clear();
            break;
        case 'n':
            nightMode = !nightMode;
            break;
        default:
            ;
        }

        std::swap(points[1], points[0]);
        swap(prevGray, gray);
    }

    return 0;
}

CodePudding user response:

I have fixed this issue this way -

  1. I have deleted earlier all installed opencv files from /user/{bin, lib, include} folders
  2. Freshly followed all above steps mentioned in question to install opencv in debug mode.
  3. After successful installation, Just debug the code and successfully entered inside opencv functions.

NOTE: My error was, I installed opencv in RELEASE MODE initially and later same one tried to installed with debug mode without folder change. But with clean approach I am able to solve my problem.

CodePudding user response:

If you're using OpenCV in C then the answer is simple. Don't use any evil and obnoxious GNU package managers. Download the source code for OpenCV, build it in debug mode as a static library (.a files) using VC Code CMake extensions.

Then in your C project that uses OpenCV, set up your compiler include directories, linker include directories, and static library (.a) file names.

That way the version of OpenCV you're using will be embedded into your executable, instead of your having to rely on your client to install the specific requested version of OpenCV onto their Ubuntu system.

Nobody likes DLLs

That's a small victory contra Richard Stallman.

  • Related