MATLAB Answers

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

조회 수: 1,346(최근 30일)
Dhanya
Dhanya 21 Aug 2014
댓글: Image Analyst 16 Jul 2020 1:51
I need the values of the pixels from an image. I need values for each pixel separately.

  댓글 수: 0

Sign in to comment.

답변(6개)

Image Analyst
Image Analyst 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

표시 이전 댓글 수: 4
Image Analyst
Image Analyst 12 Dec 2019
To get the value within a mask, for a grayscale image you can do
pixelValues = grayImage(mask);
For an RGB image I think you should split it into the 3 separate color channels then get them for each color channel.
To get the gray scale or RGB values of all the pixels in an image (not just the masked area though you could modify it to call fprintf only within the mask if you want), see the attached script which lets you read in an image and write out all the values to a CSV text file.
Claudio Ignacio Fernandez
Claudio Ignacio Fernandez 16 Jul 2020 0:31
Hello @Image Analyst
Today I used ginput over my image to get the x y coordinates. However, instead getting for example an x y values [40 80] I'm getting [40.12 80.07]. Why this is happening? any lead?
Anyway when using the weird X Y coordinates with the command impixel I get pixel values, however How can I know if I'm getting the value of the pixel I really want??
Image Analyst
Image Analyst 16 Jul 2020 1:51
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
Ben11 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));

  댓글 수: 8

표시 이전 댓글 수: 5
mohd akmal masud
mohd akmal masud 22 Nov 2017
Dar all,
I try this code to calculate summation value pixel in range 1000-30000. But error.
index1 = find(x == grayLevel1); index2 = find(x == grayLevel2); numPixels = sum(count(index1:index2));
Anyone can help me to write the code please??
Guillaume
Guillaume 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 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

표시 이전 댓글 수: 5
Image Analyst
Image Analyst 24 Oct 2019
numel() is a good way for grayscale images. For color images, numel doesn't give you the number of pixels since most people consider the number of lateral points pixels, so a single pixel would have 3 elements for a color image.
Not sure what was meant by reshaping the image, but size() does not reshape the image, nor does doing riceImage(:) or riceImage(1:end). I also thought it did until a Mathworks developer assured me that it does not reshape or cause any movement of values in RAM memory or allocating additional memory even though (:) gives a 1-D column vector.
Guillaume
Guillaume 24 Oct 2019
My comment was addressed to Gustavo whose code is just a more convoluted
numPixelsInImage = numel(riceImage);
His length(array(1:end)) (which is the same as length(array(:)) by the way) reshapes the image into a vector just to know how many pixels there are. Indeed it (and numel) only works with grayscale images.
And, yes reshape does not reorder the pixels. However, it does use some cpu cycles for a completely unnecessary operation.
Image Analyst
Image Analyst 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 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

Sign in to comment.


snehal jaipurkar
snehal jaipurkar 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??

  댓글 수: 2

Image Analyst
Image Analyst 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.

Sign in to comment.


Umar Awan
Umar Awan 25 Feb 2019
how can i convert an 28*28 pixel image into 1*784 ? means in 1 row

  댓글 수: 2

Image Analyst
Image Analyst 25 Feb 2019
If grayImage is your 2-D image, do
rowImage = reshape(grayImage, 1, []);
to reshape the 2-D gray scale image into a 1-D row vector.
muhammad choudhry
muhammad choudhry 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.

Sign in to comment.


Translated by