iT邦幫忙

0

[VS Code]如何用Visual Studio Code 架設Open CV (C++)環境?(已解決)

  • 分享至 

  • xImage

小弟最近想用VS Code 建置Open CV(C++)環境,但是爬了很多文卻一直架不起來,希望能有大大能幫助我。希望能回答的愈詳細越好,包括:

  1. MINGW、Open CV的版本(X64,X32好像有差)
  2. VSCODE 的extentions 選擇
  3. c_cpp_proporties.json, task.json, launch.json的配置
  4. 如果用cmake的大大也請告訴我要如何用cmd,而非vscode的terminal輸出

感激不盡!!!

在fysh711426大大的幫助下,本問題已解決。
本人的配置方法:https://github.com/chenyus0609/opencv-configuration-with-vscode

能提供更多的資訊嗎?
例如 Open CV 版本
或希望環境具有那些功能
您好,我用open cv版本是3.41、mingw32。
我希望可以編譯/執行基本的opencv 功能(例如輸出圖片,將彩色的照片轉為灰階),我目前遇到的問題是vscode(應該說mingw)讀不到open cv的library, 甚至讀不到header file,即使我已經將他們加入path裡面了。
https://blog.csdn.net/zoeou/article/details/80934367
以上網址是我參考的配置方法,因為某些資料夾的路徑不同做了點更動,其他都一樣
然後就:
fatal error: opencv2/opencv.hpp: No such file or directory
#include<opencv2/opencv.hpp>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
小碼農米爾
iT邦高手 1 級 ‧ 2019-03-31 01:36:53

目前測試結果(失敗)

  1. 安裝 OpenCV-MinGW-Build (3.4.1)
  2. 新增環境變數 C:\MinGW\binC:\opencv\bin
  3. 設定 VSCode

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:\\MinGW\\include",
                "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include",
                "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++",
                "C:\\opencv\\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=5",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    "C:\\MinGW\\include",
                    "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include",
                    "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++",
                    "C:\\opencv\\include"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.exe",
                "-I","C:\\opencv\\include",
                "-L","C:\\opencv\\bin",
                "-llibopencv_calib3d341",
                "-llibopencv_core341",
                "-llibopencv_dnn341",
                "-llibopencv_features2d341",
                "-llibopencv_flann341",
                "-llibopencv_highgui341",
                "-llibopencv_imgcodecs341",
                "-llibopencv_imgproc341",
                "-llibopencv_ml341",
                "-llibopencv_objdetect341",
                "-llibopencv_photo341",
                "-llibopencv_shape341",
                "-llibopencv_stitching341",
                "-llibopencv_superres341",
                "-llibopencv_video341",
                "-llibopencv_videoio341",
                "-llibopencv_videostab341"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

不過還是失敗了,雖然有編譯成功,但執行會出錯。 /images/emoticon/emoticon70.gif

https://ithelp.ithome.com.tw/upload/images/20190331/20106865va4J3hIWGw.jpg

參考文章

Opencv C++ MinGW VSCode fatal error to compile

我試了你的方法了,好像抓的到<opencv2/opencv.hpp>,
因為intellisense 有提示出來,開心~
但是沒有編譯成功QQ
dll file not recognized file format not recognized
https://ithelp.ithome.com.tw/upload/images/20190403/201165364Hnlmlz2mc.png
上網查了一下,如果把tasks.json 中的這部分:

"args": [
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.exe"
        ]

改成

"args": [
                "-c", //change!!
                "${file}",
                "-o", 
                "${fileBasenameNoExtension}.exe"
        ]

則會變成:
https://ithelp.ithome.com.tw/upload/images/20190403/20116536pf9Tet2xed.png
還是不行

我最後成功了,謝謝您!
以下是我的配置方法:https://github.com/chenyus0609/opencv-configuration-with-vscode

原來要安裝 MinGW-64,MinGW 缺少了一些 dll。
libwinpthread-1.dll
/images/emoticon/emoticon12.gif

我要發表回答

立即登入回答