问题描述
我只想得到我的概念清楚 - 访问所有的矩阵元素cv :: Mat意味着我实际上访问图像的所有像素值(灰度-1通道和颜色-3通道)?像假设我的代码打印灰度矩阵的值是1通道图像加载和类型CV_32FC1,如下所示,那么这意味着我只访问cv :: mat的成员或我正在访问的图像的像素值(1通道 - 灰度和类型CV_32FC1)?
I just want to get my concept clear that - is accessing all the matrix elements of cv::Mat means I am actually accessing all the pixel values of an image (grayscale - 1 channel and for colour - 3 channels)? Like suppose my code for printing the values of matrix of gray scale that is 1 channel image loaded and type CV_32FC1, is as shown below, then does that mean that I am accessing only the members of the cv::mat or I am accessing the pixel values of the image (with 1 channel - grayscale and type CV_32FC1) also?
cv::Mat img = cv::imread("lenna.png");
for(int j=0;j<img.rows;j++)
{
for (int i=0;i<img.cols;i++)
{
std::cout << "Matrix of image loaded is: " << img.at<uchar>(i,j);
}
}
我对使用OpenCV的图像处理很陌生清除我的想法。如果我错了,那么如何访问图像的每个像素值?
I am quite new to image processing with OpenCV and want to clear my idea. If I am wrong, then how can I access each pixel value of an image?
推荐答案
并且您也访问图像本身。在您的代码中,执行此操作后:
You are accessing the elements of the matrix and you are accessing the image itself also. In your code, after you do this:
cv::Mat img = cv::imread("lenna.png");
矩阵img表示图像lenna.png。 (如果已成功打开)
the matrix img represents the image lenna.png. ( if it is successfully opened )
为什么不通过更改一些像素值来实验自己:
Why don't you experiment yourself by changing some of the pixel values:
cv::Mat img = cv::imread("lenna.png");
//Before changing
cv::imshow("Before",img);
//change some pixel value
for(int j=0;j<img.rows;j++)
{
for (int i=0;i<img.cols;i++)
{
if( i== j)
img.at<uchar>(j,i) = 255; //white
}
}
//After changing
cv::imshow("After",img);
注意:这只会更改易失性存储器中的图像值,即当前加载的mat img 。修改mat img的值,不会改变存储在磁盘中的实际映像lenna.png中的值(除非您进行了imwrite)
Note: this only changes the image values in volatile memory, that is where the mat img is currently loaded. Modifying the values of the mat img, not going to change value in your actual image "lenna.png",which is stored in your disk, (unless you do imwrite)
但是在1通道灰度图像的情况下,它是CV_8UC1而不是CV_32FC1
But in case of 1-channel grayscale image it is CV_8UC1 not CV_32FC1
这篇关于访问OpenCV中灰度图像的像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!