Home > database >  How can I fix my OpenCV4 installation on Ubuntu
How can I fix my OpenCV4 installation on Ubuntu

Time:02-13

I need some help. I want to install Opencv4 on my computer (which runs on Ubuntu) and use it with VSCode.

A lot of tutorial explains how to do it, so here is one of thoses I followed: https://vitux.com/opencv_ubuntu/

I, next, took a program my teacher sent me to check installation:

#include <iostream>
#include <string>
#include <opencv4/opencv2/highgui.hpp>

using namespace cv;
using namespace std;

int main(void)
{
    string imageName("/home/baptiste/Documents/M1/infographie/images/lena.jpg"); // path to the image

    Mat img;
    img = imread(imageName, IMREAD_COLOR); // Read the file as a color image
    if( img.empty() )                      // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    Mat img_gray;

    img_gray = imread(imageName,IMREAD_GRAYSCALE); //Read the file as a grayscale image
    if( img_gray.empty() )                      // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    imshow( "Image read", img );                // Show our color image inside the window.

    imshow("Grayscale image read", img_gray); //Show our grayscale image inside the window.

    waitKey(0); // Wait for a keystroke in the window
    imwrite("/home/baptiste/Documents/M1/infographie/images/lena_gray.jpg", img_gray); //Save our grayscale image into the file
    return 0;
}

I also wrote a makefile:

CC=g  

read_image: read_image.cpp
    $(CC) read_image.cpp -o read_image `pkg-config --libs --cflags opencv4`

clean:
    rm -f read_image

But when I am compiling, I got this:

g   read_image.cpp -o read_image `pkg-config --libs --cflags opencv4`
In file included from read_image.cpp:3:
/usr/local/include/opencv4/opencv2/highgui.hpp:46:10: fatal error: opencv2/core.hpp: Aucun fichier ou dossier de ce type
   46 | #include "opencv2/core.hpp"
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [makefile:4 : read_image] Erreur 1

I also spot a problem with my opencv installation: in /usr/local/include, I have a opencv4 folder, which contains...a opencv2 folder, and then the whole installation.

I am interpreting this as: the inclusion made by the modules are not reading at the good place. My program actually recognize the location of highgui.hpp, but that module does not recognize the good location for core.hpp

I did not saw anyone having that problem so it might be quite weird but can someone help me with that ?

Also, I added "/usr/local/include/opencv4/**" into my VSCode configuration.

Thank you for your answer, and for the time you will invest.

EDIT 1: Here is my c_cpp_properties.json:

    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/opencv4/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c  14",
            "intelliSenseMode": "linux-clang-x64"
        }
    ],
    "version": 4
}

And how do I the "tasks.json" file ?

CodePudding user response:

I am interpreting this as: the inclusion made by the modules are not reading at the good place. My program actually recognize the location of highgui.hpp, but that module does not recognize the good location for core.hpp

nice & correct analysis, for a noob !!

your teacher's test program is already incorrect

#include <opencv4/opencv2/highgui.hpp>

should be:

#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>      // added for clarity
#include <opencv2/imgcodecs.hpp> // same

and you should probably drop the (opaque and badly supported) pkg-config parts from your makefile, and add paths / libs manually, so you at least "know, what you're doing":

CC=g  
INC=/usr/local/include/opencv4
LIB=/usr/local/lib
LIBS=-lopencv_core -lopencv_highgui -lopencv_imgcodecs
read_image: read_image.cpp
    $(CC) -I$(INC) read_image.cpp -o read_image -L$(LIB) $(LIBS)

clean:
    rm -f read_image

  • Related