我需要一些幫助。我想在我的計算機(在 Ubuntu 上運行)上安裝 Opencv4 并將其與 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的好位置
我沒有看到有人遇到這個問題,所以這可能很奇怪,但有人可以幫我解決這個問題嗎?
另外,我添加"/usr/local/include/opencv4/**"到我的 VSCode 配置中。
感謝您的回答,以及您將投入的時間。
編輯 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”檔案?
uj5u.com熱心網友回復:
我將其解釋為:模塊所包含的內容沒有在好地方閱讀。我的程式實際上識別出了 highgui.hpp 的位置,但是那個模塊沒有識別出 core.hpp 的好位置
好的和正確的分析,對于一個菜鳥!
你老師的測驗程式已經不正確
#include <opencv4/opencv2/highgui.hpp>
應該:
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp> // added for clarity
#include <opencv2/imgcodecs.hpp> // same
你可能應該pkg-config從你的makefile中洗掉(不透明和嚴重支持的)部分,并手動添加路徑/庫,所以你至少“知道,你在做什么”:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/424710.html
