Opencv中出现“imread is not a member of cv”错误的解决方案

目前本菜鸟正在跟随高翔博士的《视觉SLAM十四讲》学习基本的SLAM框架及相关算法理论。
在运行ch5的imageBasics程序(即利用opencv实现图像读取、显示、遍历像素、赋值及拷贝)时出现了错误。
原代码如下:

#include 
#include 
using namespace std;
#include 
#include 
#include 

int main ( int argc, char** argv )
{
    // 读取argv[1]指定的图像
   cv::Mat image;  //此处为初始化,定义变量image
    image = cv::imread( argv[1] ); //cv::imread函数读取指定路径下的图像    argv[1]表示第一个参数
    // 判断图像文件是否正确读取
    if ( image.data == nullptr ) //数据不存在,可能是文件不存在
    {
        cerr<<"文件"< ( y );  // row_ptr是第y行的头指针
	//image.ptr(y) 表示取出第y行的指针   
	for ( size_t x=0; x time_used = chrono::duration_cast>( t2-t1 );
    cout<<"遍历图像用时:"<

使用Kdevelop构建后报错:

/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:16:13: error: ‘imread’ is not a member of ‘cv’
     image = cv::imread( argv[1] ); //cv::imread函数读取指定路径下的图像    argv[1]表示第一个参数
             ^
/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:32:5: error: ‘imshow’ is not a member of ‘cv’
     cv::imshow( "image", image );      // 用cv::imshow显示图像
     ^
/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:33:5: error: ‘waitKey’ is not a member of ‘cv’
     cv::waitKey ( 0 );                  // 暂停程序,等待一个按键输入
     ^
/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:70:94: error: ‘t1’ was not declared in this scope
     chrono::duration time_used = chrono::duration_cast>( t2-t1 );
                                                                                              ^
/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:81:5: error: ‘imshow’ is not a member of ‘cv’
     cv::imshow ( "image", image );
     ^
/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:82:5: error: ‘waitKey’ is not a member of ‘cv’
     cv::waitKey ( 0 );
     ^
/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:87:5: error: ‘imshow’ is not a member of ‘cv’
     cv::imshow ( "image", image );
     ^
/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:88:5: error: ‘imshow’ is not a member of ‘cv’
     cv::imshow ( "image_clone", image_clone );
     ^
/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:89:5: error: ‘waitKey’ is not a member of ‘cv’
     cv::waitKey ( 0 );
     ^
/home/darrick/SLAM-book-master/ch5/imageBasics/imageBasics.cpp:93:5: error: ‘destroyAllWindows’ is not a member of ‘cv’
     cv::destroyAllWindows();   //关闭窗口
     ^
make[2]: *** [CMakeFiles/imageBasics.dir/imageBasics.cpp.o] Error 1
make[1]: *** [CMakeFiles/imageBasics.dir/all] Error 2
make: *** [all] Error 2

在网上查找诸多教程仍没有成功解决,直到某个机缘巧合之下,终于找到了解决方法
Opencv中出现“imread is not a member of cv”错误的解决方案_第1张图片
可以看到在cv::imread这里出现了错误,编译后下面打了波浪线

于是我们把cv::imread删掉几个字母,重新键入imread,此时我们可以看到,出现了辅助选择框:
Opencv中出现“imread is not a member of cv”错误的解决方案_第2张图片
点击第一个选项,后波浪线即可消失。并且在库函数引用声明中,多了一段:

#include "../../../opencv-3.1.0/modules/imgcodecs/include/opencv2/imgcodecs.hpp"

Opencv中出现“imread is not a member of cv”错误的解决方案_第3张图片
对后面的cv::imshow等执行同样的操作,即可解决问题。

分析:

原来的库引用申明中包括三个:

#include 
#include 
#include 

但实际上因为安装的原因,imread等函数并非在上述三个中,因此可以通过kdevelop编译器的智能编辑,将正确的库引用进来。

Good luck !

你可能感兴趣的:(opencv使用,Ubuntu,Opencv,imread,SLAM十四讲)