Try   HackMD

建置VScode的C++及OpenCV環境 by076

Step 1: 下載VScode Windows x64 Stable

Step 2: 下載Mingw64

Step 3: 下載OpenCV

Step4: 建置環境變數

編輯系統環境變數

環境變數

系統變數

Path

新增Mingw的bin路徑

新增OpenCV的bin路徑

編輯系統環境變數➜ 環境變數 ➜ 系統變數 ➜ Path ➜ 新增 Mingw的bin路徑 (ex: D:\APP\Mingw\mingw64\bin) ➜ 新增OpenCV的bin路徑 (ex: D:\APP\Opencv\build\bin)

Step5: 把OpenCV裡的include和lib分別複製到mingw64中的include和lib

  • include

D:\APP\Opencv\build\include內的"opencv2"文件複製到 D:\APP\Mingw\mingw64\include

  • lib

D:\APP\Opencv\build\lib 內的 "全部資料"複製到 D:\APP\Mingw\mingw64\lib

Step6: 建置一個資料夾並建立test.cpp,在資料夾中右鍵"以Code開啟"

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Step7: 點開test.cpp,貼上範例程式

  • include出現紅線先呼略啦
  • test.png記得準備啦
#include "opencv2/opencv.hpp" #include "iostream" int main(int argc, char const *argv[]) { cv::Mat img = cv::imread("test.png"); if (img.empty()) std::cout << "image is empty or the path is invalid!" << std::endl; cv::imshow("Origin", img); cv::waitKey(0); cv::destroyAllWindows(); return 0; }

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Hint: 終端機的開法

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Step8: 測試時間

  • 打在終端機上

g++ -g test.cpp -I D:\\APP\\Mingw\\mingw64\\include -lopencv_core452 -lopencv_imgcodecs452 -lopencv_imgproc452 -lopencv_calib3d452 -lopencv_dnn452 -lopencv_features2d452 -lopencv_flann452 -lopencv_gapi452 -lopencv_highgui452 -lopencv_ml452 -lopencv_objdetect452 -lopencv_photo452 -lopencv_stitching452 -lopencv_video452 -lopencv_videoio452 -o test.exe

紅色的地方改成自己mingw64 include路徑

Step9: 執行資料夾內生成的test.exe

  • 成功顯示test.png ➜ 測試成功

Step10: 在Coding用資料夾內建立名為.vscode的資料夾,在裡面新增tasks.json

Step11: 撰寫tasks.json

  • 從範例修改

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Step12: 存檔後到test.cpp執行結果,有以下兩種方式:

1. Shift+ctrl+B : 只編譯出exe檔,要再手動執行exe。

2.
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

案下去就會編譯加執行exe啦!!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Hint: 若出現此現象,則選擇g++編譯器

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

<補充>: 出現紅線 (ex: include出現紅線) 的解決辦法

1. 使用shift+ctrl+P叫出搜尋欄,輸入C++ config

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2. 修改內文

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →