OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow

来源:互联网 发布:阿里云 马云 王坚 编辑:程序博客网 时间:2024/09/21 09:27

刚开始学习OpenCV编程,写了一个程序:

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>void colorReduce( cv::Mat &image, int div=64 ){    int nl = image.rows;    int nc = image.cols * image.channels();    for(int j=0; j<nl; j++)    {        uchar *data = image.ptr<uchar>(j);        for( int i=0; i<nc; i++)        {            data[i] = data[i]/div*div + div/2;        }    }}int main(void){    cv::Mat image = cv::imread("/home/fighting324/Pictures/lena.jpg");    colorReduce( image );        cv::namedWindow("colorReduced", CV_WINDOW_AUTOSIZE);    cv::imshow("colorReduced", image);    cv::waitKey();}


后来运行时出现了这样的一个错误:OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/fighting324/app/opencv/modules/highgui/src/window.cpp, line 261
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/fighting324/app/opencv/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow

怎么搞都没有解决,搜索了将近一个小时之后发现了这个帖子:http://answers.opencv.org/question/18135/opencv-error-assertion-failed-sizewidth0/

我也照猫画虎,将我代码中读取图像的这一句:

    cv::Mat image = cv::imread("~/Pictures/lena.jpg");
中的~改成了/home/fighting324,竟然就好了!