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

1.073 views (last 30 days)

Show older comments

I need the values of the pixels from an image. I need values for each pixel separately.

##### 2 Comments

Image Analyst
on 11 Jan 2021

### Answers (7)

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

##### 7 Comments

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.

Ben11
on 21 Aug 2014

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

##### 8 Comments

Guillaume
on 22 Nov 2017

@mohd akmal masud, please start your own question instead of spamming other people questions

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.

##### 8 Comments

Image Analyst
on 24 Oct 2019

Youssef Khmou
on 21 Aug 2014

Accessing a pixel is similar to retrieving element from matrix, here are two examples :

for gray scale image :

X=imread('circuit.tif');

X(10,60)

for multi channel image :

Y=imread('autumn.tif');

Y(10,60,1) %R

Y(10,60,2) %G

##### 0 Comments

snehal jaipurkar
on 23 Nov 2016

##### 2 Comments

Image Analyst
on 10 Nov 2018

Use sum:

sumOfCounts = sum(counts(index1:index2));

where index1 and index2 define the "range of pixel values" that you want to sum over.

Umar Awan
on 25 Feb 2019

how can i convert an 28*28 pixel image into 1*784 ? means in 1 row

##### 2 Comments

muhammad choudhry
on 10 Jun 2020

hi,

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.

Asad Alam
on 25 Feb 2021

How can i compare pixel value of an image

pixelvalue<300

And i want all the pixels whose values are above 300. can anyone help

##### 3 Comments

### See Also

### Products

### Community Treasure Hunt

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

Start Hunting!