通过OpenCV访问图像中的像素时,若先访问列再访问行的话,会出现如下错误:
OpenCV(3.4.2) Error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv
::Mat::at, file d:\diyprogram\buildopencvvs\install\include\opencv2\core\mat.inl
.hpp, line 1098
OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(3.4.2) Error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv
::Mat::at, file d:\diyprogram\buildopencvvs\install\include\opencv2\core\mat.inl
.hpp, line 1098
若改为先行后列则可正确运行
代码如下:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//OpenCV头文件
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main( int argc, const char** argv )
{
//读图像
Mat color = imread("1.jpg");
Mat gray = imread("1.jpg", 0);
//写图像
imwrite("1Gray.jpg", gray);
//通过OpenCV函数获取相同像素
int myRow = color.rows-1; //访问图像像素是需要先访问行再访问列
int myCol = color.cols-1;
Vec3b pixel = color.at<Vec3b>(myRow, myCol);
cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," <<
(int)pixel[1] << (int)pixel[2] << ")" << endl;
//显示图像
namedWindow("Lena BGR",WINDOW_NORMAL);
namedWindow("Lena Gray",WINDOW_NORMAL);
imshow("Lena BGR", color);
imshow("Lena Gray", gray);
//等待按键
waitKey(0);
return 0;
}
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//OpenCV头文件
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main( int argc, const char** argv )
{
//读图像
Mat color = imread("1.jpg");
Mat gray = imread("1.jpg", 0);
//写图像
imwrite("1Gray.jpg", gray);
//通过OpenCV函数获取相同像素
int myRow = color.rows-1; //访问图像像素是需要先访问行再访问列
int myCol = color.cols-1;
Vec3b pixel = color.at<Vec3b>(myRow, myCol);
cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," <<
(int)pixel[1] << (int)pixel[2] << ")" << endl;
//显示图像
namedWindow("Lena BGR",WINDOW_NORMAL);
namedWindow("Lena Gray",WINDOW_NORMAL);
imshow("Lena BGR", color);
imshow("Lena Gray", gray);
//等待按键
waitKey(0);
return 0;
}