Opencv函数只能以C代码方式调用,而不能以C ++方式调用 [英] Opencv functions can only be invoked in C code fashion but not in C++ fashion

查看:634
本文介绍了Opencv函数只能以C代码方式调用,而不能以C ++方式调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是新的Opencv。下载并按照说明安装Opencv 2.4后,我开始写我的第一个Opencv程序,这基本上是在网络上的教程的副本。

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

#includecv.h
#includehighgui.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;
}

代码工作得很好,但是你可能注意到, Opencv函数是以C代码的方式。因此,我决定使用以下代码继续使用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<用法:display_image ImageToLoadAndDisplay< endl;
return -1;
}

Mat image;
image = imread(argv [1],CV_LOAD_IMAGE_COLOR); //读取文件

if(!image.data)//检查无效的输入
{
cout< 无法打开或找到图像< std :: endl;
return -1;
}

namedWindow(Display window,CV_WINDOW_AUTOSIZE); //创建一个显示窗口。
imshow(Display window,image); //在里面显示我们的图片。

waitKey(0); //等待窗口中的击键
return 0;但是,在这种情况下,程序有几个链接错误,虽然编译似乎很好。但是,在这种情况下,程序有几个链接错误。我收到的链接错误如下:

 错误2错误LNK2019:未解析的外部符号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)在函数_main中引用C:\Research\OpencvTest\OpencvTest.obj 
错误1错误LNK2019:unresolved外部符号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&在函数_main中引用的?(?imshow @ cv @@ YAXABV?$ basic_string @ DV?$ char_traits @ D @ stlp_std @@ V?$ allocator @ D @ 2 @@ stlp_std @@ ABV_InputArray @ 1 @ \\ Research\OpencvTest\OpencvTest.obj

我相信我已经添加了必要的Opencv库在我的程序(我使用VC10),我添加的额外的库如下:

  stl_port.lib 
opencv_highgui242d.lib
opencv_core242d.lib

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

解决方案

它与混合STLPort和MSVC STL有关。你可能没有自己生成OpenCV库,所以他们使用VC10 STL。对于C接口,只有 char * ,但是使用C ++接口链接器与方法中的 std :: string 混淆。如果你将它的输入转换为 string ,你应该会看到与 imread 相同的结果。



我可以在我的项目中混用STL实施吗? p>

I am really new to Opencv. After downloading and installing Opencv 2.4 according to the instruction, I began writing my first Opencv program, which was basically a copy of the tutorial on the web.

#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;
}

The codes work very well, but you may notice that in the above code invoking Opencv function is in a C code fashion. I therefore decide to proceed with C++ code fashion with the following codes:

#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;
}

However, in this case the program has several link errors although compiling seems fine. The link errors I have received are as follows:

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

I am quite sure that I have added necessary Opencv libraries in my program (I use VC10), and the additional libraries I have added are as follows:

stl_port.lib
opencv_highgui242d.lib
opencv_core242d.lib

I was wondering what's wrong with my setting. Why does it work for the first program but not the second one? Any ideas will be appreciated. Thanks!

解决方案

It has something to do with mixing STLPort and MSVC STL. You probably didn't build OpenCV libraries yourself, so they're using VC10 STL. With C interface there's just char* but with C++ interface linker gets confused with std::string in methods. You should see the same result with imread if you cast it's input to string too.

Can I mix STL implementations in my project?

这篇关于Opencv函数只能以C代码方式调用,而不能以C ++方式调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆