MATLAB Answers

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

조회 수: 1,595(최근 30일)
Dhanya
Dhanya 21 Aug 2014
댓글: Image Analyst 12 Dec 2019
I need the values of the pixels from an image. I need values for each pixel separately.

  댓글 수: 0

로그인 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);

  댓글 수: 5

표시 이전 댓글 수: 2
mohd akmal masud
mohd akmal masud 12 Dec 2019
It's work, but the result is the location of pixelm. How extract the value of pixel that point?
subImage = a(75:79, 61:61)
mohd akmal masud
mohd akmal masud 12 Dec 2019
Hi Image Analyst.
I have been using your code for free hand masking. But how do I extract the list of pixel values from within the masked region?
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.

로그인 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

로그인 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.

로그인 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

로그인 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.

로그인 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

  댓글 수: 1

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.

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by