在执行opencv操作图像时出现错误,编译无法成功进行,求请教解决方法
In file included from /usr/include/c++/5/chrono:35:0,
from imageBasics.cpp:2:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
^
imageBasics.cpp: In function ‘int main(int, char**)’:
imageBasics.cpp:14:24: error: ‘nullptr’ was not declared in this scope
if ( image.data == nullptr ) //数据不存在,可能是文件不存在
^
imageBasics.cpp:34:5: error: ‘chrono’ has not been declared
chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
^
imageBasics.cpp:50:5: error: ‘chrono’ has not been declared
chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
^
imageBasics.cpp:51:5: error: ‘chrono’ has not been declared
chrono::duration<double> time_used = chrono::duration_cast<chrono::duration
^
imageBasics.cpp:51:22: error: expected primary-expression before ‘double’
chrono::duration<double> time_used = chrono::duration_cast<chrono::duration
^
imageBasics.cpp:52:36: error: ‘time_used’ was not declared in this scope
cout<<"遍历图像用时:"<<time_used.count()<<" 秒。"<<endl;
我的代码如下
#include <iostream>
#include <chrono>//时间相关的库
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc, char **argv) {
// 读取argv[1]指定的图像
cv::Mat image;
image = cv::imread(argv[1]); //cv::imread函数读取指定路径下的图像
// 判断图像文件是否正确读取
if (image.data == nullptr) { //数据不存在,可能是文件不存在
cerr << "文件" << argv[1] << "不存在." << endl;
return 0;
}
// 文件顺利读取, 首先输出一些基本信息
cout << "图像宽为" << image.cols << ",高为" << image.rows << ",通道数为" << image.channels() << endl;
cv::imshow("image", image); // 用cv::imshow显示图像
cv::waitKey(0); // 暂停程序,等待一个按键输入
// 判断image的类型
if (image.type() != CV_8UC1 && image.type() != CV_8UC3) {//单通道灰度,三通道彩色
// 图像类型不符合要求
cout << "请输入一张彩色图或灰度图." << endl;
return 0;
}
// 遍历图像, 请注意以下遍历方式亦可使用于随机像素访问
// 使用 std::chrono 来给算法计时
chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
for (size_t y = 0; y < image.rows; y++) { //高对应行
// 用cv::Mat::ptr获得图像的行指针
unsigned char *row_ptr = image.ptr<unsigned char>(y); // row_ptr是第y行的头指针
for (size_t x = 0; x < image.cols; x++) { //宽对应行
// 访问位于 x,y 处的像素
unsigned char *data_ptr = &row_ptr[x * image.channels()]; // data_ptr 指向待访问的像素数据
// 输出该像素的每个通道,如果是灰度图就只有一个通道
for (int c = 0; c != image.channels(); c++) {
unsigned char data = data_ptr[c]; // data为I(x,y)第c个通道的值
}
}
}
chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
chrono::duration<double> time_used = chrono::duration_cast < chrono::duration < double >> (t2 - t1);
cout << "遍历图像用时:" << time_used.count() << " 秒。" << endl;
// 关于 cv::Mat 的拷贝
// 直接赋值并不会拷贝数据
cv::Mat image_another = image;
// 修改 image_another 会导致 image 发生变化
image_another(cv::Rect(0, 0, 100, 100)).setTo(0); // 将左上角100*100的块置零,左上角100*100变黑色
cv::imshow("image", image);
cv::waitKey(0);
// 使用clone函数来拷贝数据
cv::Mat image_clone = image.clone();
image_clone(cv::Rect(0, 0, 100, 100)).setTo(255);//左上角100*100变白色
cv::imshow("image", image);
cv::imshow("image_clone", image_clone);
cv::waitKey(0); // 暂停程序,等待一个按键输入
// 对于图像还有很多基本的操作,如剪切,旋转,缩放等,限于篇幅就不一一介绍了,请参看OpenCV官方文档查询每个函数的调用方法.
cv::destroyAllWindows();//关闭所有打开窗口
return 0;
}
其中cmakelists文件也添加啦一下内容
#添加c++ 11标准支持
add_definitions(-std=c++11)
#寻找opencv库
find_package( OpenCV REQUIRED )
#添加头文件
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( imageBasics imageBasics.cpp )
#链接 OpenCV库
target_link_libraries( imageBasics ${OpenCV_LIBS} )