How can I fix my OpenCV4 installation on Ubuntu - c++

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 ?

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

Sublime Text 3: SDL2/SDL.h: No such file or directory

So I was doing things with this tutorial: https://www.youtube.com/watch?v=KsG6dJlLBDw&t=640s and everything was grate til I pressed f7 in main.cpp and I got this response "fatal error: SDL2/SDL.h: No such file or directory". I watched every step of the tutorial once again and corrected some mistakes but this error still comes up.
My code looks like this:
{
"folders":
[
{
"path": "bin/..",
"file_exclude_patterns": ["*.sublime-project"]
}
],
"build_systems":
[
{
"name": "Build Debug",
"working_dir": "${project_path}",
"cmd": "g++ -c src/*.cpp -std=c++14 -g -Wall -m64 -I include -I C:/SDL2-w64/include && g++ *.o -o bin/debug/main -L C:/SDL2-w64/lib -lmingw32 -lSDL2main -lSDL2 -lSDL2_image && start bin/debug/main",
"selector": "source.c++",
"shell": true
}
]
}
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
int main(int argc, char* args[])
{
std::cout << "test" << std::endl;
std::cin.get()
retrun 0;
}
I'm new to coding so sorry if it's totally easy but I'm trying to solve it for 2h and nothing helps.

Yocto recipe for an application that uses opencv and cpp

I created a recipe for a cpp application using opencv to stream an image. But The build fails with following error :
Log data follows:
| DEBUG: Executing shell function do_compile
| shot.cpp:1:10: fatal error: core.hpp: No such file or directory
| 1 | #include "core.hpp"
| | ^~~~~~~~~~
| compilation terminated.
| WARNING: exit code 1 from a shell command.
Although, I added the meta-openembedded/meta-oe that includes opencv folder in which I found core.hpp to bblayers.
Here is the recipe:
DESCRIPTION = " this is an application display an image"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://shot.cpp"
DEPENDS = "opencv"
S = "${WORKDIR}"
do_compile () {
${CXX} shot.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_videoio
}
do_install () {
install -d 0755 ${D}/${bindir}
install -m 0755 ${D}/${bindir}
}
When I ran the application using the opencv I installed on my ubuntu 18.04. it works fine (using cmake).
I tried adding manually the core folder next to the recipe and it didn't work of course, I also added the path of the core.hpp to the recipe in the SRC_URI variable but no luck whatsoever
I don't know what to do specially that I'm new to both yocto and opencv; help please
Here is the source code of the application:
#include "core.hpp"
#include "highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
Mat image;
// LOAD image
image = imread("image1.jpg"); // Read the file "image.jpg".
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
//DISPLAY image
namedWindow( "window"); // Create a window for display.
imshow( "window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
I think you need some modifications on your recipe:
[...]
inherit pkgconfig
DEPENDS = "opencv"
[...]
And you will need to add a RDEPENDS and FILES variable:
[...]
DEPENDS = "opencv"
# Add the runtime dependencies
RDEPENDS_${PN} = "libopencv-core libopencv-imgproc libopencv-highgui libopencv-videoio libopencv-imgcodecs"
[...]
# At the end, ship the files
FILES_${PN} = "${bindir}"
RDEPENDS_${PN} will tell Yocto that your recipe package ${PN} needs a list a runtime dependencies. These dependencies are requiered to run your application on your board (typically libraries).
FILES_${PN} will tell Yocto that your recipe package ${PN} will contains files. Without this line, Yocto will tell you that
${PN} is by default your recipe file name (without the version and .bb)
There is also some errors.
The compilation had some issues, lopencv_imgcodecs was not added, and there was no output name of your binary (I have named it shot):
do_compile () {
${CXX} ${WORKDIR}/shot.cpp -o ${WORKDIR}/shot -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs ${LDFLAGS}
}
The second install command is not right:
install -m 0755 ${WORKDIR}/shot ${D}/${bindir}
Your application does not have the correct headers:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
At the end, the recipe looks like:
DESCRIPTION = " this is an application display an image"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://shot.cpp"
inherit pkgconfig
DEPENDS = "opencv"
RDEPENDS_${PN} = "libopencv-core libopencv-imgproc libopencv-highgui libopencv-videoio libopencv-imgcodecs"
S = "${WORKDIR}"
do_compile () {
${CXX} ${WORKDIR}/shot.cpp -o ${WORKDIR}/shot -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs ${LDFLAGS}
}
do_install () {
install -d 0755 ${D}/${bindir}
install -m 0755 ${WORKDIR}/shot ${D}/${bindir}
}
FILES_${PN} = "${bindir}"
And the application:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( )
{
Mat image;
// LOAD image
image = imread("image1.jpg"); // Read the file "image.jpg".
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
//DISPLAY image
namedWindow( "window"); // Create a window for display.
imshow( "window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
The recipe was working on my Yocto warrior meta.

main.cpp:1:10: fatal error: opencv2/highgui.hpp: No such file or directory

I have install opencv in ubuntu 18.04 and it was installed successfully, I have tried this command:
$ pkg-config --modversion opencv
and its output is: 4.0.1-dev
after this i have tried to rum c++ code:
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace std;
int main( int argc, char** argv ) {
cv::Mat image;
image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR);
if(! image.data ) {
std::cout << "Could not open or find the image" << std::endl ;
return -1;
}
cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
cv::imshow( "Display window", image );
cv::waitKey(0);
return 0;
}
with this command: :~/cpp_test$ g++ main.cpp -o output pkg-config --cflags --libs opencv
but it throws a fatal error:
main.cpp:1:10: fatal error: opencv2/highgui.hpp: No such file or directory
#include <opencv2/highgui.hpp>
^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
I have reviewed some similar questions but i did not find my answer, i think this is because of environment variables and i do not know which variables i have to set.
In the compiling command add a "4" next to "opencv" (or the number of your version of OpenCV):
$ g++ main.cpp -o output \`pkg-config --cflags --libs opencv4\`

libopencv_core.so.2.4: error adding symbols: DSO missing from command line

I have installed OpenCV 3.3.0 to Ubuntu 16.04. Just want to compile this code.
#include <iostream>
using namespace std;
#include "opencv2/opencv.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cmath>
using namespace cv;
int main(int argc, char* argv[])
{
try
{
int kernel_size = 3;
cv::Mat src_host = cv::imread("crack2.jpg");
cv::Mat gray_img, avg, kernel;
cv::gpu::GpuMat dst, src;
src.upload(src_host);
cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);
cv::Mat result_host;
dst.download(result_host);
std::cout<< "Done!!!" <<std::endl;
}catch(const cv::Exception& ex)
{
std::cout<<"Error: " << ex.what() << std::endl;
}
return 0;
}
g++ -o main gpu_thresh.cpp 'pkg-config opencv --cflags --libs' -lopencv_gpu -lopencv_core
g++ -L/usr/local/lib -o main gpu_thresh.cpp 'pkg-config opencv --cflags --libs' -lopencv_gpu -lopencv_core
I tried to compile it with these ways but still giving same warning and error.
/usr/bin/ld: warning: libopencv_core.so.2.4, needed by
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libopencv_gpu.so,
may conflict with libopencv_core.so.3.3 /usr/bin/ld: /tmp/ccdhLGL0.o:
undefined reference to symbol '_ZN2cv3gpu6GpuMat7releaseEv'
//usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4: error adding
symbols: DSO missing from command line collect2: error: ld returned 1
exit status
What should I do?
There's no opencv2/gpu/gpu.hpp in OpenCV 3.3. If your code compiles then it means that you've both OpenCV 2.4 and 3.3 on your machine.
In OpenCV 3.3, include:
#include <opencv2/core/cuda.hpp>
and then use
cv::cuda::GpuMat img;
See details here.
Edit: I just noticed your compilation method. When using pkg-config opencv --cflags --libs, you don't need to manually add the libopencv files anymore.
Just do: g++ -o main gpu_thresh.cpp 'pkg-config opencv --cflags --libs'

Get a opencv_gpu function working in Qt5

I have an OpenSUSE 13.2 System with Qt5 and OpenCV installed with cudasupport. The Hardware is an Intel i5 processor with an integrated intel gpu chip and a NVidia GForce 940 M and i have tried to compile this file.
#include <iostream>
#include <time.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/gpu/gpu.hpp"
using namespace std;
int main()
{
try
{
cv::Mat dst_mat;
cv::Mat src_host = cv::imread("/home/peter/testCuda/testCuda/GothaOrangerie.JPG", CV_LOAD_IMAGE_GRAYSCALE);
cv::namedWindow("Result",cv::WINDOW_NORMAL);
cv::imshow("Result", src_host);
cv::waitKey();
cv::gpu::GpuMat dst, src;
src.upload(src_host);
clock_t t = clock();
cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);
t = clock() -t;
cv::Mat result_host(dst);
cout << ((float)t)/CLOCKS_PER_SEC << endl;
cv::imshow("Result", result_host);
cv::waitKey();
t = clock();
cv::threshold(src_host, dst_mat, 128.0, 255.0, CV_THRESH_BINARY);
t = clock() -t;
cout << ((float)t)/CLOCKS_PER_SEC << endl;
cv::imshow("Result", dst_mat);
cv::waitKey();
}
catch(const cv::Exception& ex)
{
cout << "Error: " << ex.what() << endl;
}
return 0;
}
The compilation in the shell with
g++ main.cpp -o threshold `pkg-config --cflags --libs opencv` -lopencv_gpu -L/usr/local/cuda-7.5/lib64
works pretty well i can run the small program without any issues. If i try it with the Qt5 IDE it returns me this error.
OpenCV Error: No GPU support (The library is compiled without CUDA support) in mallocPitch, file /home/abuild/rpmbuild/BUILD/opencv-2.4.9/modules/dynamicuda/include/opencv2/dynamicuda/dynamicuda.hpp, line 126
Error: /home/abuild/rpmbuild/BUILD/opencv-2.4.9/modules/dynamicuda/include/opencv2/dynamicuda/dynamicuda.hpp:126: error: (-216) The library is compiled without CUDA support in function mallocPitch
If i run the shellcompiled program with this command
optirun ./threshold
i get the same error.
The .pro File is
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-15T04:02:07
#
#-------------------------------------------------
TARGET = testCuda
LIBS += -L/usr/lib64/
LIBS += -L/usr/local/cuda-7.5/lib64 -lopencv_gpu
LIBS += `pkg-config opencv --cflags --libs`
SOURCES += main.cpp
and the Qt compilation command is
22:58:12: Running steps for project testCuda...
22:58:12: Configuration unchanged, skipping qmake step.
22:58:12: Starting: "/usr/bin/make"
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../testCuda -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I../testCuda -I. -o main.o ../testCuda/main.cpp
g++ -Wl,-O1 -o testCuda main.o -L/usr/lib64 -L/usr/lib64/ -L/usr/local/cuda-7.5/lib64 -lopencv_gpu `pkg-config opencv --cflags --libs` -lQtGui -L/usr/lib64 -L/usr/X11R6/lib -lQtCore -lpthread
22:58:13: The process "/usr/bin/make" exited normally.
22:58:13: Elapsed time: 00:01.
Anybody an idea how to fix that?
Deinstalling the preinstalled libopencv-2.4.9 package solved it.