Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing pixel value of gray scale image in OpenCV

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

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?

like image 896
user227666 Avatar asked Jul 29 '13 08:07

user227666


People also ask

How do I get the RGB value of a pixel in Python OpenCV?

Image can be read using imread() function which returns the matrix of pixels (default is RGB mode).

How do I find the pixel value of a photo?

To view the values of pixels in a specific region of an image displayed in Image Viewer, use the Pixel Region tool. The Pixel Region tool superimposes a rectangle, called the pixel region rectangle, over the image displayed in Image Viewer.

What is grayscale pixel value?

For a grayscale images, the pixel value is a single number that represents the brightness of the pixel. The most common pixel format is the byte image, where this number is stored as an 8-bit integer giving a range of possible values from 0 to 255. Typically zero is taken to be black, and 255 is taken to be white.


2 Answers

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 )

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)

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

like image 76
Barshan Das Avatar answered Sep 22 '22 12:09

Barshan Das


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

int pixelValue = (int)img.at<uchar>(i,j);
like image 13
zeerak Avatar answered Sep 22 '22 12:09

zeerak