致命错误:opencv2/opencv.hpp:no这样的文件或目录#include; opencv2/opencv.hpp;

发布于 2025-02-12 06:51:12 字数 4237 浏览 2 评论 0原文

我是C ++和OpenCV的新手,我对这个问题感到困惑。我正在使用VS代码版本1.68.1,并且当我尝试运行一个main.cpp文件时,我总是会遇到此错误:

main.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory 

    2 | #include "opencv2/opencv.hpp"
      |          ^~~~~~~~~~~~~~~~~~~~

这是main.cpp代码:

#include <stdio.h>
#include "opencv2/opencv.hpp"

using namespace cv;
int main(int argc, char** argv )
{

    Mat image;
    image = imread("lenna.jpg");
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

我的cmakelists.txt文件:

cmake_minimum_required(VERSION 3.0.0)
project(opencvTest)

include(CTest)
enable_testing()

set("OpenCV_DIR" "C:\\opencv4")
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable(opencvTest main.cpp)
target_link_libraries(opencvTest ${OpenCV_LIBS} )

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-ID","C:\\opencv4\\install\\include",
                "-LD","C:\\opencv4\\install\\x64\\mingw\\bin",                
                "-llibopencv_calib3d411",
                "-llibopencv_core411",
                "-llibopencv_dnn411",
                "-llibopencv_features2d411",
                "-llibopencv_flann411",
                "-llibopencv_highgui411",
                "-llibopencv_imgcodecs411",
                "-llibopencv_imgproc411",
                "-llibopencv_ml411",
                "-llibopencv_objdetect411",
                "-llibopencv_photo411",
                "-llibopencv_stitching411",
                "-llibopencv_video411",
                "-llibopencv_videoio411"
            ],
            "options": {
                "cwd": "C:\\msys64\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:/msys64/mingw64/bin/gcc.exe"
        }
    ]
}

aunpair.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++.exe build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "g++.exe build active file"
    }
  ]
}

c_cpp_properties:c_cpp_properties .json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\opencv4\\install\\include",
                "C:\\opencv4\\install\\x64\\mingw\\lib"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/msys64/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

标题的定义指向此位置

我注意到的是,由于某种原因, href =“ https://i.sstatic.net/owwo2.png” rel =“ nofollow noreferrer”>正在运行的路径 但是我所有的标题文件都位于OpenCV4位置。我不知道该怎么办,并且在过去的几个小时里尝试找到解决方案。任何帮助将不胜感激。

I am new to C++ and OpenCV and I am stuck on this problem. I am using VS Code version 1.68.1 and when I try to run a main.cpp file, I always get this error:

main.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory 

    2 | #include "opencv2/opencv.hpp"
      |          ^~~~~~~~~~~~~~~~~~~~

This is the main.cpp code:

#include <stdio.h>
#include "opencv2/opencv.hpp"

using namespace cv;
int main(int argc, char** argv )
{

    Mat image;
    image = imread("lenna.jpg");
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

My CMakelists.txt file:

cmake_minimum_required(VERSION 3.0.0)
project(opencvTest)

include(CTest)
enable_testing()

set("OpenCV_DIR" "C:\\opencv4")
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable(opencvTest main.cpp)
target_link_libraries(opencvTest ${OpenCV_LIBS} )

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-ID","C:\\opencv4\\install\\include",
                "-LD","C:\\opencv4\\install\\x64\\mingw\\bin",                
                "-llibopencv_calib3d411",
                "-llibopencv_core411",
                "-llibopencv_dnn411",
                "-llibopencv_features2d411",
                "-llibopencv_flann411",
                "-llibopencv_highgui411",
                "-llibopencv_imgcodecs411",
                "-llibopencv_imgproc411",
                "-llibopencv_ml411",
                "-llibopencv_objdetect411",
                "-llibopencv_photo411",
                "-llibopencv_stitching411",
                "-llibopencv_video411",
                "-llibopencv_videoio411"
            ],
            "options": {
                "cwd": "C:\\msys64\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:/msys64/mingw64/bin/gcc.exe"
        }
    ]
}

launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++.exe build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "g++.exe build active file"
    }
  ]
}

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\opencv4\\install\\include",
                "C:\\opencv4\\install\\x64\\mingw\\lib"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/msys64/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

What I noticed is that the definitions of the header point to this location for some reason

path being run
but all my header files are in the opencv4 location. I do not know what to do about this and I have spent the last couple of hours trying to find a solution. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白馒头 2025-02-19 06:51:14

对于任何有这个问题的人来说,这就是我开始使用OpenCV所做的。

我切换到使用Microsoft Visual Studio而不是VS代码来构建开放的简历项目。
您可以从此链接下载它:
https://visalstudio.microsoft.com/vs/community/”

< a href =“ 确保选择安装“ Visual C ++构建工具”的选项,

您还需要安装CMAKE在您的计算机上,可以从

。您将需要下载 opencv and opencv_contrib 来自其各自的github页面。您可以通过单击页面“右上方”处的绿色按钮来做到这一点。
将这些文件夹提取到方便的位置并创建一个新文件夹。

接下来,您必须使用CMAKE生成Visual Studio项目。启动CMAKE打开GUI。在顶部,将有两个文本框,一个用于源文件夹,另一个用于构建二进制文件的位置。源文件夹是您之前提取的 opencv_master 文件夹,构建文件夹是新创建的 build 文件夹。完成此操作后,单击“配置”按钮。

这将打开另一个对话框,以配置我们要配置的项目。在下拉菜单中,选择您的Visual Studio版本。完成此操作后,单击完成并等待配置完成。

完成此步骤后,将为您提供一大堆配置选项。在大多数情况下,我们将把它们作为其默认值。除非您知道自己在做什么(专门用于添加新库),否则我不会更改这些。

也就是说,除了我们需要更改的一个选项外。在顶部的搜索框中,键入单词路径。这将仅将显示器限制为一些选项。我们要调整的选项是称为 opencv_extra_modules_path 的选项。我们希望将此值更改为通往我们之前创建的OpenCV_Contrib-Master目录中的模块目录的路径。

输入此新值后,点击“配置”按钮并再次等待。这次的过程应该更快。

如果一切顺利,您应该在日志输出的底部看到配置已完成的消息。如果出现任何错误,您将必须将这些错误弄清楚,然后再继续进行。

作为在此处的最后一步,请点击“生成”按钮,以创建Visual Studio项目,我们将用来构建OpenCV。

现在,这是该过程中最长的部分。查看构建目录,然后向下滚动后,您应该找到一个称为 opencv.sln 的文件,这是Visual Studio Project的解决方案文件。双击它在Visual Studio中打开项目。

注意:Visual Studio可能需要一段时间才能充分加载该项目。它有很多文件要索引!

现在,我们必须在此处构建库的调试和发布版本。首先,请确保将构建配置设置为如下图中的首次亮相,然后从构建菜单中选择“构建解决方案”,或在键盘上点击F6。

注意:构建库可能需要一些时间。

当此调试构建完成后,请继续并更改构建配置以释放(与图片中相同的位置),然后再次构建。

这两个构建完成后,将构建配置更改为调试。在解决方案资源管理器中找到安装条目,右键单击它,然后选择“构建”。

再一次,您将需要对发布和调试配置进行此步骤。这个构建过程的时间不如前两个时间!

这应该在构建文件夹中创建很多文件。严格必要的唯一文件夹是安装文件夹。您可以复制此文件夹的内容,并将其移至容易访问并粘贴内容的位置。

您现在拥有OpenCV库。

要开始创建项目,您可以在此链接上查看视频:
https://www.youtube.com/watch?v=p-6rg6rg6rg6zgu4u

总结,打开视觉工作室,并确保您指定包含目录,库目录和输入目录.lib文件。

For anyone having this issue, this is what I did to start using OpenCV.

I switched to using Microsoft Visual Studio instead of VS Code for building Open CV projects.
You can download it from this link:
https://visualstudio.microsoft.com/vs/community/

When installing, you must make sure that you select the option to install the "Visual C++ build tools"

You will also need to have CMake installed on your computer which can be downloaded from the CMake website

The Next Step is to download the source files for OpenCV. You will need to download both OpenCV and OpenCV_Contrib from their respective Github Pages. You can do this by clicking the green button at the 'top-right' of the page.
Extract these folders to a convenient location and create a New Folder.

Next, you have to generate the Visual Studio Project with CMake. Launch CMake to open the GUI. at the top, there will be two text boxes, one for the source folder and another for the location where to build the binaries. The source folder is the opencv_master folder you extracted previously and the build folder is the newly created build folder. Once that's done, Click the Configure Button.

This will open another dialogue box to configure what sort of project we want to configure. In the dropdown, choose your version of Visual Studio. Once that is done, click finish and wait for the configuration to complete.

Once this step completes you will be presented with a whole bunch of configuration options. For the most part, we will be leaving these as their default values. I wouldn’t change these unless you know what you’re doing (specifically for adding new libraries).

That is, except for one option that we do need to change. In the search box at the top, type the word path. This will limit the display to just a few options. The option we want to adjust is the one called OPENCV_EXTRA_MODULES_PATH. We want to change this value to be the path to the modules directory located within the opencv_contrib-master directory we created earlier.

Once you enter this new value hit the Configure button and wait once again. This process should be much quicker this time around.

If all goes well, you should see a Configuring done message at the bottom of the log output. If it fails with any errors you’ll have to get those worked out before continuing on.

As a final step here, hit the Generate button to create the Visual Studio project that we will use to build OpenCV.

Now, this is the longest part of the process. Take a look in the build directory and after scrolling down a bit you should find a file called OpenCV.sln This is the Solution file for the Visual Studio Project. Double click it to open the project in Visual Studio.

NOTE: It may take a while for Visual Studio to fully load this project. It has a whole bunch of files to index!

Now, we have to build both the debug and release versions of the library here. To start with, ensure that your build configuration is set to debut like in the image below, then either choose Build Solution from the Build Menu or hit F6 on your keyboard.

Debug

Note: Building the library can take a bit of time.

When this debug build completes, go ahead and change the build configuration to Release (same place as in the picture) and build again.

After both of these builds are complete, change the build configuration back to Debug. Find the INSTALL entry in the solution explorer, right-click on it, and choose Build.

Once again, you will need to do this step for both the Release and Debug configurations. This build process shouldn’t take nearly as much time as the two previous ones!

enter image description here

This should create a lot of files in the build folder. The only folder that is strictly necessary is the install folder. You can copy the contents of this folder and move it to a location that is readily accessible and paste the contents.

You now have the OpenCV library.

To start creating projects, you can have a look at the video at this link:
https://www.youtube.com/watch?v=p-6rG6Zgu4U

In summary, open Visual Studio and ensure that you specify the include directories, Library Directories and the Input directories for the .lib files.

再见回来 2025-02-19 06:51:14

由于我们没有那么多信息,因此我将如何解释CMAKE中的目录如何工作。

当您执行#include“ OpenCv2/Opencv.hpp”时,您告诉它在提供的include_directories() path ../ opencv.hpp其中../是以前的目录。因此,问题可能是目录路径,OPENCV的安装不当,或者是因为 find_package()在Windows的软件包注册表中查找软件包,这导致了一个不同的路径。因此,您可以尝试使用$ {OPENCV_INCLUDE_DIRS}中的

include_directories( ${OpenCV_INCLUDE_DIRS} )

完整路径,该文件夹名为“ Incless”,其中包含文件opencv.hpp。请注意,这样做可能要求您指定OpenCV库的库文件路径,其中包括文件名和扩展名。

target_link_libraries("C:/Example/libs/example.lib")

示例cmakelists.txt文件:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)


project("EXAMPLE" VERSION 0.1 LANGUAGES CXX)


message("CMake is running for windows version")
file(GLOB_RECURSE SRC_FILES src/*.cpp)  #the /* made it a comment lol*/

add_executable("${PROJECT_NAME}" ${SRC_FILES})

target_include_directories("${PROJECT_NAME}" PUBLIC
    "${CMAKE_SOURCE_DIR}/Dependencies/Example/include"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/include"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2_image-2.0.5/include"
)

target_link_libraries("${PROJECT_NAME}"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/lib/x64/SDL2.lib"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/lib/x64/SDL2main.lib"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/lib/x64/SDL2test.lib"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2_image-2.0.5/lib/x64/SDL2_image.lib"
)

Since we don't have that much information, I will just explain how include directories in CMake works.

When you do #include "opencv2/opencv.hpp", you are telling it to look in the provided include_directories() path ../include/opencv2/opencv.hpp where ../ are the previous directories. So the problem can be the directories path, improper installation of OpenCV, or because find_package() look for the packages in windows' package registry and that results in a different include path. So you can try to replace ${OpenCV_INCLUDE_DIRS} in

include_directories( ${OpenCV_INCLUDE_DIRS} )

with the full path to the folder named "include" that contains the file opencv.hpp. Note that doing it this way would likely require you to specify the library file path of the OpenCV library, which would include file name and the extension.

target_link_libraries("C:/Example/libs/example.lib")

example CMakeLists.txt file:

cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)


project("EXAMPLE" VERSION 0.1 LANGUAGES CXX)


message("CMake is running for windows version")
file(GLOB_RECURSE SRC_FILES src/*.cpp)  #the /* made it a comment lol*/

add_executable("${PROJECT_NAME}" ${SRC_FILES})

target_include_directories("${PROJECT_NAME}" PUBLIC
    "${CMAKE_SOURCE_DIR}/Dependencies/Example/include"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/include"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2_image-2.0.5/include"
)

target_link_libraries("${PROJECT_NAME}"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/lib/x64/SDL2.lib"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/lib/x64/SDL2main.lib"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2-2.0.22/lib/x64/SDL2test.lib"
    "${CMAKE_SOURCE_DIR}/Dependencies/SDL2_image-2.0.5/lib/x64/SDL2_image.lib"
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文