C++ 在 OpenCV 中访问灰度图像的像素值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17919399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:33:07  来源:igfitidea点击:

accessing pixel value of gray scale image in OpenCV

c++opencvimage-processing

提问by user227666

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 的所有矩阵元素意味着我实际上正在访问图像的所有像素值(灰度 - 1 通道和颜色 - 3 通道)?就像假设我的用于打印已加载 1 通道图像并键入 CV_32FC1 的灰度矩阵值的代码如下所示,那么这是否意味着我只访问了 cv::mat 的成员,或者我正在访问图像的像素值(具有 1 个通道 - 灰度和类型 CV_32FC1)?

    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);
      }
    }

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?

我对使用 OpenCV 进行图像处理很陌生,想澄清我的想法。如果我错了,那么如何访问图像的每个像素值?

回答by Barshan Das

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");

the matrix img represents the image lenna.png. ( if it is successfully opened )

矩阵 img 表示图像 lenna.png。(如果成功打开)

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);

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)

注意:这只会更改易失性内存中的图像值,即当前加载 mat img 的位置。修改 mat img 的值,不会更改存储在磁盘中的实际图像“lenna.png”中的值(除非您执行 imwrite)

But in case of 1-channel grayscale image it is CV_8UC1 not CV_32FC1

但在 1 通道灰度图像的情况下,它是 CV_8UC1 而不是 CV_32FC1

回答by zeerak

In order to get the pixel value of the grayscale image (an integer between 0 and 255), the answer also needs to be typecasted.

为了得到灰度图像的像素值(0到255之间的整数),答案也需要进行类型转换。

int pixelValue = (int)img.at<uchar>(i,j);