c++ - Microsoft Visual C++ 2010 Express 中的 OpenCV 2.4.8 代码错误

标签 c++ visual-studio-2010 opencv

我是 OpenCV 和 C++ 编程新手。 我最近通过以下链接使用 Visual Studio 2010 安装并配置了 OpenCV 2.4.8: http://opencv-srf.blogspot.com/2013/05/installing-configuring-opencv-with-vs.html

然后我通过测试这段代码来测试是否正确:

#include "opencv2/highgui/highgui.hpp"
#include "StdAfx.h"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
    Mat img = imread("C:\Users\jay\Google Drive\profilepic", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'

    if (img.empty()) //check whether the image is loaded or not
    {
        cout << "Error : Image cannot be loaded..!!" << endl;
        //system("pause"); //wait for a key press
        return -1;
    }

    namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
    imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

    waitKey(0); //wait infinite time for a keypress

    destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

    return 0;
}

不幸的是,它给出了错误:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>  ConsoleApplication1.cpp
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(1): warning C4627: '#include "opencv2/highgui/highgui.hpp"': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(5): error C2871: 'cv' : a namespace with this name does not exist
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C2065: 'Mat' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C2146: syntax error : missing ';' before identifier 'img'
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C2065: 'img' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): warning C4129: 'j' : unrecognized character escape sequence
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): warning C4129: 'G' : unrecognized character escape sequence
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): warning C4129: 'p' : unrecognized character escape sequence
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C2065: 'CV_LOAD_IMAGE_UNCHANGED' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C3861: 'imread': identifier not found
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(12): error C2065: 'img' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(12): error C2228: left of '.empty' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(19): error C2065: 'CV_WINDOW_AUTOSIZE' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(19): error C3861: 'namedWindow': identifier not found
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(20): error C2065: 'img' : undeclared identifier
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(20): error C3861: 'imshow': identifier not found
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(22): error C3861: 'waitKey': identifier not found
1>c:\users\jay\documents\visual studio 2010\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(24): error C3861: 'destroyWindow': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

希望你能告诉我我的代码有什么问题。 我对 OpenCV 和 C++ 很陌生。

最佳答案

线路

#include "StdAfx.h"

应该是第一个,在任何其他包含之前。
这就是编译器警告您它跳过了 highgui header 的原因。

关于c++ - Microsoft Visual C++ 2010 Express 中的 OpenCV 2.4.8 代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22226770/

相关文章:

c++ - 避免在不同的结构中声明相同的数据类型

c# - 错误 SIPEPS,版本 = 5.0.0.0 UCMA 4.0 VS2010/VS2013

c - include_next 预处理器指令导致 MSVC 出现问题

python - 如何在光学测距仪中测量图像重合度

python - "Nonmatching transport in server reply"当 cv2.VideoCapture rtsp onvif 相机时,如何修复?

c++ - opencv 将网络摄像头输出写入 avi 文件

线程的 C++11 vector 参数似乎未初始化

c++ - 具有权重系数的二维函数的逼近

.net - 如何处理 Entity Framework 中缺少主键的问题?

c++ - Windows 性能调整的资源建议(实时)