How to extract the value pixel values from an image or masked image?

1.073 views (last 30 days)
Dhanya on 21 Aug 2014
Commented: maram maji on 15 Sep 2021
I need the values of the pixels from an image. I need values for each pixel separately.
Image Analyst
Image Analyst on 11 Jan 2021
This is so vague, just like the original question. If you have read in your image with imread() or gotten it some other way, like from a video camera, then you have your pixels already and you just need to reference them like the Answers below. Please explain in detail why the answers below do not give you any pixel values.

Sign in to comment.

Answers (7)

Image Analyst
Image Analyst on 21 Aug 2014
Not sure what you mean. The image itself is a collection of pixel values. To get the pixel value at one particular (row, column) location, you can just specify the index:
grayLevel = grayImage(row, column);
or you can use impixel():
rgbColor = impixel(rgbImage, column, row);
Image Analyst
Image Analyst on 16 Jul 2020
That's the way ginput works - it gives you floating point values. You need to round to get array indexes of row and column
[x, y] = ginput(1);
row = round(y);
column = round(x);
Remember that row is y and column is x so don't make the mistake of saying yourImage(x, y) to reference pixels -- it's yourImage(y, x) which is yourImage(row, column).
pixelValue = yourImage(row, column); % If it's a gray scale image.
pixelValue = yourImage(row, column, :); % If it's an RGB Color image. pixelValue will be a 1x3 vector of [r,g,b] values.

Sign in to comment.

Ben11 on 21 Aug 2014
You can get the histogram of pixel values using imhist.
For example:
1) Grayscale image
Image = imread('coins.png');
[count,x] = imhist(Image);
2) RGB image
Image = imread('peppers.png');
[count,x] = imhist(Image(:,:,1)); % select one of 3 channels
or use rgb2gray:
Image = imread('peppers.png');
[count,x] = imhist(rgb2gray(Image));
Guillaume on 22 Nov 2017
@mohd akmal masud, please start your own question instead of spamming other people questions

Sign in to comment.

yonatan gerufi
yonatan gerufi on 21 Aug 2014
Hi Dhanya,
you can access to a specific pixel by typing : figure_name(x_pos,y_pos) .
In the MATLAB workspace, most images are represented as two-dimensional arrays (matrices), in which each element of the matrix corresponds to a single pixel in the displayed image. (from Matlab documentation )
This matrix can be represented in several types as double, uint8, uint16. It can also be RGB, intensity, or indexed types.
I highly recommend reading to understand the differences.
Image Analyst
Image Analyst on 24 Oct 2019
I was told by a Mathworker that (:) does not reshape the array into a column vector. So passing it into size() or length() would not reshape it. He said that if you assign that to a NEW variable, that new variable will be of a columnar shape, but that is a new variable and there is no temporary variable that is created with a column shape nor is the original array reshaped into a column vector. Only a new variable would have that shape. Fine point though.

Sign in to comment.

Youssef  Khmou
Youssef Khmou on 21 Aug 2014
Accessing a pixel is similar to retrieving element from matrix, here are two examples :
for gray scale image :
for multi channel image :
Y(10,60,1) %R
Y(10,60,2) %G

snehal jaipurkar
snehal jaipurkar on 23 Nov 2016
after finding the count of each pixel value seperately, i want to add the counts of a range of pixel values and get the total, how to write its code in matlab??

Umar Awan
Umar Awan on 25 Feb 2019
how can i convert an 28*28 pixel image into 1*784 ? means in 1 row
muhammad choudhry
muhammad choudhry on 10 Jun 2020
what is a code of finding the rgb values of the image.I actually extract frames from the video and in the frames red dye is spreading so I want to know the change in rgb values as the more dye comes in.

Sign in to comment.

Asad Alam
Asad Alam on 25 Feb 2021
How can i compare pixel value of an image
And i want all the pixels whose values are above 300. can anyone help

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by