如何在 Ubuntu 上修复我的 OpenCV4 安装
How can I fix my OpenCV4 installation on Ubuntu
我需要一些帮助。我想在我的计算机上安装 Opencv4(在 Ubuntu 上运行)并将其与 VSCode.
一起使用
很多教程都解释了如何去做,所以这是我遵循的教程之一:https://vitux.com/opencv_ubuntu/
接下来我拿了老师发给我的程序来检查安装:
#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;
}
我也写了一个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
但是当我编译的时候,我得到了这个:
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
我还发现我的 opencv 安装有问题:在 /usr/local/include 中,我有一个 opencv4 文件夹,其中包含...一个 opencv2 文件夹,然后整个安装。
我的解释是:模块包含的内容没有在正确的地方读取。我的程序实际上识别了 highgui.hpp 的位置,但该模块无法识别 core.hpp[= 的正确位置17=]
我没有看到任何人遇到这个问题,所以这可能很奇怪,但有人可以帮我解决这个问题吗?
此外,我在 VSCode 配置中添加了 "/usr/local/include/opencv4/**"
。
感谢您的回答,感谢您投入的时间。
编辑 1:
这是我的 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
}
以及如何获取“tasks.json”文件?
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
漂亮且正确的分析,对于新手!!
你老师的测试程序已经不正确了
#include <opencv4/opencv2/highgui.hpp>
应该是:
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp> // added for clarity
#include <opencv2/imgcodecs.hpp> // same
并且您可能应该从您的 makefile 中删除(不透明且支持不佳的)pkg-config
部分,并手动添加路径/库,这样您至少“知道,您在做什么”:
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
我需要一些帮助。我想在我的计算机上安装 Opencv4(在 Ubuntu 上运行)并将其与 VSCode.
一起使用很多教程都解释了如何去做,所以这是我遵循的教程之一:https://vitux.com/opencv_ubuntu/
接下来我拿了老师发给我的程序来检查安装:
#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;
}
我也写了一个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
但是当我编译的时候,我得到了这个:
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
我还发现我的 opencv 安装有问题:在 /usr/local/include 中,我有一个 opencv4 文件夹,其中包含...一个 opencv2 文件夹,然后整个安装。
我的解释是:模块包含的内容没有在正确的地方读取。我的程序实际上识别了 highgui.hpp 的位置,但该模块无法识别 core.hpp[= 的正确位置17=]
我没有看到任何人遇到这个问题,所以这可能很奇怪,但有人可以帮我解决这个问题吗?
此外,我在 VSCode 配置中添加了 "/usr/local/include/opencv4/**"
。
感谢您的回答,感谢您投入的时间。
编辑 1: 这是我的 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
}
以及如何获取“tasks.json”文件?
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
漂亮且正确的分析,对于新手!!
你老师的测试程序已经不正确了
#include <opencv4/opencv2/highgui.hpp>
应该是:
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp> // added for clarity
#include <opencv2/imgcodecs.hpp> // same
并且您可能应该从您的 makefile 中删除(不透明且支持不佳的)pkg-config
部分,并手动添加路径/库,这样您至少“知道,您在做什么”:
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