opencv 报错:Error: Assertion failed (data) in cv::Mat::at, file ... mat.inl.hpp, line 897(访问了不存在矩阵的像素)
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv 报错:Error: Assertion failed (data) in cv::Mat::at, file ... mat.inl.hpp, line 897(访问了不存在矩阵的像素)相关的知识,希望对你有一定的参考价值。
文章目录
opencv4报错
这是源代码:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main(int argc, const char* argv[])
Mat src, dst;
src = imread("./test.jpg");
//if (src.empty())
if (!src.data)
printf("could not load image...\\n");
return -1;
//namedWindow("input img"); //默认自动窗口大小
//imshow("input img", src);
//三个for循环,执行运算 g_dstImage(i,j) =a*g_srcImage(i,j) + b
for (int y = 0; y < src.rows; y++)
for (int x = 0; x < src.cols; x++)
for (int c = 0; c < 3; c++)
//g_dstImage.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y, x)[c]) + g_nBrightValue);
dst.at<Vec3b>(y, x)[c] = src.at<Vec3b>(y, x)[c];
namedWindow("output img"); //默认自动窗口大小
imshow("output img", dst);
waitKey(0);
return 0;
vs编译运行报错:
原因
访问数组的不存在的下标了(因为我们没在上面代码中初始化Mat数组dst的大小和数据类型。。。)
解决方法
加上代码:dst = Mat::zeros(src.size(), src.type()); //第二个参数是位深度,目前值为16
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main(int argc, const char* argv[])
Mat src, dst;
src = imread("./test.jpg");
//if (src.empty())
if (!src.data)
printf("could not load image...\\n");
return -1;
namedWindow("input img"); //默认自动窗口大小
imshow("input img", src);
dst = Mat::zeros(src.size(), src.type()); //第二个参数是位深度,目前值为16
//三个for循环,执行运算 g_dstImage(i,j) =a*g_srcImage(i,j) + b
for (int y = 0; y < src.rows; y++)
for (int x = 0; x < src.cols; x++)
for (int c = 0; c < 3; c++)
//g_dstImage.at<Vec3b>(y, x)[c] = saturate_cast<uchar>((g_nContrastValue*0.01)*(g_srcImage.at<Vec3b>(y, x)[c]) + g_nBrightValue);
dst.at<Vec3b>(y, x)[c] = 255 - src.at<Vec3b>(y, x)[c];
namedWindow("output img"); //默认自动窗口大小
imshow("output img", dst);
waitKey(0);
return 0;
vs编译运行结果:
参考文章:opencv 错误:cv::Mat::at 文件中的断言失败:mat.inl.hpp 第 930 行
以上是关于opencv 报错:Error: Assertion failed (data) in cv::Mat::at, file ... mat.inl.hpp, line 897(访问了不存在矩阵的像素)的主要内容,如果未能解决你的问题,请参考以下文章
OpenCV报错:color.hpp:253: error: (-215:Assertion failed)
opencv cv2.imread()报错: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor‘
VS报错:LINK : fatal error LNK1104: 无法打开文件“msvcp100d.lib”
opencv 报错:Error: Assertion failed (data) in cv::Mat::at, file ... mat.inl.hpp, line 897(访问了不存在矩阵的像素)
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor