c++ - 无法从灰度图像访问像素值

标签 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

最佳答案

您已经声明您正在阅读尺寸为 1600x1200 的图像, 那么你如何访问 1201总共只有1200行的元素,其实你误解了mat.at<>()的约定.我建议您使用 mat.at<>(cv::Point(p_x, p_y))通过这种方式,您永远不会对要在 mat.at<>() 中传递的行和列感到困惑。 .

编辑: 但是,创建一个新的 cv::Point不是@Micka 建议的访问像素的推荐方式,因此将其视为一种解决方法,不要完全依赖 cv::Point()访问一个像素。迭代像素的最​​佳方法在 Opencv documentation 中定义。 .

关于c++ - 无法从灰度图像访问像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38932316/

相关文章:

c++ - 哈希的 unordered_map

python - 在函数 'inRange'中(-215:断言失败)! _src.empty()

opencv - 让撕破的纸恢复原样

c++ - 错误 : size of array ‘u’ has non-integral type ‘double’ |

c++ - 如何将 vector<Mat> 转换为 vector<float>?

c++ - MinGW/msys,链接器 (ld.exe) 没有找到 libpng (-lpng),即使它已安装,正在运行 xpdf 配置脚本

python opencv去除验证码中的噪音

c++ - 在 Windows 的 GUI 应用程序中使用控制台,仅当它从控制台运行时

windows - qt创建者错误: program exited with code -1073741819

image - 使用 OpenCV 进行水平线检测