使用at获取opencv中的像素值
get pixel values in opencv with at
我已经使用 imread 函数成功读取了灰度图像,我正在尝试访问所有像素值并将它们复制到二维矢量。我收到此错误:
gray image channels: 1
OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si
ze.p[0] && (unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * cha
nnels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1
<< 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file C:\opencv_3\opencv\bui
ld\include\opencv2/core/mat.inl.hpp, line 894
我似乎不明白以下代码有什么问题:
Mat img = imread("C:\digitalImageProcessing\lion2.png", 0);
if (!img.data)
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
// know the number of channels the image has
cout << "gray image channels: " << img.channels() << endl;
// ******************* READ the Pixel intensity *********************
int no_of_cols = img.cols;
int no_of_rows = img.rows;
int initial_value = 0;
std::vector<std::vector<int>> image_matrix;
image_matrix.resize(no_of_rows, std::vector<int>(no_of_cols,initial_value));
for (int j = 0; j < img.rows; j++)
{
for (int i = 0; i < img.cols; i++)
{
Scalar value = img.at<uchar>(i, j);
cout << "value = " << endl << " " << value.val[0] << endl << endl;
image_matrix[i][j] = intensity.val[0];
}
}
可能有比我下面的代码更好的方法,但我认为它可能比你的每像素循环更好
#include "opencv2/opencv.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main()
{
Mat img = Mat(3,3,CV_8UC1);
randu(img,0,255);
cout << "img pixels \n\n" << img << endl;
vector<vector<int> > image_matrix;
vector<int> image_matrix_row;
for( int i = 0; i < img.rows; i++)
{
img.row(i).copyTo(image_matrix_row);
image_matrix.push_back(image_matrix_row);
}
cout << "\nimage_matrix[1][1] : " << image_matrix[1][1];
return 0;
}
输出:
img pixels
[ 91, 2, 79;
179, 52, 205;
236, 8, 181]
image_matrix[1][1] : 52
我已经使用 imread 函数成功读取了灰度图像,我正在尝试访问所有像素值并将它们复制到二维矢量。我收到此错误:
gray image channels: 1
OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si
ze.p[0] && (unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * cha
nnels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1
<< 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file C:\opencv_3\opencv\bui
ld\include\opencv2/core/mat.inl.hpp, line 894
我似乎不明白以下代码有什么问题:
Mat img = imread("C:\digitalImageProcessing\lion2.png", 0);
if (!img.data)
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
// know the number of channels the image has
cout << "gray image channels: " << img.channels() << endl;
// ******************* READ the Pixel intensity *********************
int no_of_cols = img.cols;
int no_of_rows = img.rows;
int initial_value = 0;
std::vector<std::vector<int>> image_matrix;
image_matrix.resize(no_of_rows, std::vector<int>(no_of_cols,initial_value));
for (int j = 0; j < img.rows; j++)
{
for (int i = 0; i < img.cols; i++)
{
Scalar value = img.at<uchar>(i, j);
cout << "value = " << endl << " " << value.val[0] << endl << endl;
image_matrix[i][j] = intensity.val[0];
}
}
可能有比我下面的代码更好的方法,但我认为它可能比你的每像素循环更好
#include "opencv2/opencv.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main()
{
Mat img = Mat(3,3,CV_8UC1);
randu(img,0,255);
cout << "img pixels \n\n" << img << endl;
vector<vector<int> > image_matrix;
vector<int> image_matrix_row;
for( int i = 0; i < img.rows; i++)
{
img.row(i).copyTo(image_matrix_row);
image_matrix.push_back(image_matrix_row);
}
cout << "\nimage_matrix[1][1] : " << image_matrix[1][1];
return 0;
}
输出:
img pixels
[ 91, 2, 79;
179, 52, 205;
236, 8, 181]
image_matrix[1][1] : 52