我真的是Opencv的新手。根据说明下载并安装了Opencv 2.4之后,我开始编写我的第一个Opencv程序,该程序基本上是网上教程的副本。

#include <stdio.h>
#include <iostream>
#include <vector>

#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
    char* filename = "C:\\Research\abc.pgm";
     IplImage *img0;

    if( (img0 = cvLoadImage(filename,-1)) == 0 )
        return 0;

    cvNamedWindow( "image", 0 );
    cvShowImage( "image", img0 );
    cvWaitKey(0);
    cvDestroyWindow("image");
    cvReleaseImage(&img0);



    return 0;
}

这些代码可以很好地工作,但是您可能会注意到,在上面的代码中,以C代码的方式调用Opencv函数。因此,我决定使用以下代码继续进行C++代码编写:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

但是,在这种情况下,尽管编译看起来不错,但程序存在多个链接错误。我收到的链接错误如下:
Error   2   error LNK2019: unresolved external symbol "void __cdecl cv::namedWindow(class stlp_std::basic_string<char,class stlp_std::char_traits<char>,class stlp_std::allocator<char> > const &,int)" (?namedWindow@cv@@YAXABV?$basic_string@DV?$char_traits@D@stlp_std@@V?$allocator@D@2@@stlp_std@@H@Z) referenced in function _main    C:\Research\OpencvTest\OpencvTest.obj
Error   1   error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class stlp_std::basic_string<char,class stlp_std::char_traits<char>,class stlp_std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXABV?$basic_string@DV?$char_traits@D@stlp_std@@V?$allocator@D@2@@stlp_std@@ABV_InputArray@1@@Z) referenced in function _main    C:\Research\OpencvTest\OpencvTest.obj

我非常确定我已经在程序中添加了必要的Opencv库(我使用VC10),并且添加的其他库如下:
stl_port.lib
opencv_highgui242d.lib
opencv_core242d.lib

我想知道我的设置有什么问题。为什么它对第一个程序有效,但对第二个程序无效?任何想法将不胜感激。谢谢!

最佳答案

它与混合STLPort和MSVC STL有关。您可能没有自己构建OpenCV库,因此它们正在使用VC10 STL。使用C接口(interface)时只有char*,但是使用C++接口(interface)链接器会使方法中的std::string混淆。如果您也将imread的输入也转换为string,则应该看到与ojit_code相同的结果。

Can I mix STL implementations in my project?

10-04 21:15
查看更多