无法访问灰度图像中的像素值

时间:2016-08-13 11:38:53

标签: c++ opencv

我正在读取尺寸为1600x1200的图像作为灰度,然后尝试访问位置(1201,0)处的像素值。我最终得到了段错误,如评论所示:

Mat gray_image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);   // Read the file

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

const int width = gray_image.cols; 
const int height = gray_image.rows;

cout<<gray_image.cols<<endl; // 1600
cout<<gray_image.rows<<endl; // 1200
cout<<(int)gray_image.at<uchar>(1201,0)<<endl; //SEGFAULT

2 个答案:

答案 0 :(得分:2)

您已经声明您正在阅读大小为1600x1200的图片,那么如何才能从总共1200行中访问1201元素,实际上您误解了{{1}的约定}。我建议你以这种方式使用mat.at<>(),你永远不会混淆要在mat.at<>(cv::Point(p_x, p_y))中传递的行和列。

编辑但是,根据@Micka的建议,不建议创建新的mat.at<>()访问像素,因此请将其视为解决方法,并且完全不依赖在cv::Point上访问像素。在Opencv documentation中定义了迭代像素的最​​佳方法。

答案 1 :(得分:0)

它应该是.at(row,col)而不是(col,row)并且你没有第1201行,这就是为什么它是一个seg错误

相关问题