How to extract the value pixel values from an image or masked image?
Afficher commentaires plus anciens
I need the values of the pixels from an image. I need values for each pixel separately.
3 commentaires
Zainab Afreen
le 11 Jan 2021
Hello I also have the same problem. Can anyone help me how do I detect pixels in an image automatically. Please reply
Image Analyst
le 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.
DGM
le 12 Fév 2023
Oh that's easy. Say you have an image called myimage. Then to detect pixels:
haspixels = ~isempty(myimage);
You're welcome.
Réponse acceptée
Plus de réponses (6)
Ben11
le 21 Août 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));
6 commentaires
snehal jaipurkar
le 23 Nov 2016
this was helpful to me.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?
Image Analyst
le 23 Nov 2016
try this:
index1 = find(x == grayLevel1);
index2 = find(x == grayLevel2);
numPixels = sum(count(index1:index2));
snehal jaipurkar
le 24 Nov 2016
Modifié(e) : snehal jaipurkar
le 24 Nov 2016
thanks sir:) it worked :) i want to run this same program on suppose 50 images stored in a folder and based on the total count of pixels found as such in a certain range of graylevels is more than the total count of pixels found for other range of graylevels, i want to categorize that image into some category.how can i do dis sir???
Image Analyst
le 24 Nov 2016
In the loop use an if statement to categorize:
if numPixels > threshold1
% Do something
elseif numPixels > threshold2
% Do something else
elseif numPixels > threshold3
% Do a third thing
end
and so on.
snehal jaipurkar
le 24 Nov 2016
and sir how to run this program on 50 images stored in a folder?
Image Analyst
le 24 Nov 2016
yonatan gerufi
le 21 Août 2014
0 votes
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 commentaires
Image Analyst
le 21 Août 2014
That's not exactly correct. The correct indexing is given in my answer. Mixing up x,y with row,column is a very common error. To fix your reference you'd do figure_name(y_pos,x_pos) (reversing the indexes as you had them) where figure_name is the the name of the image variable rather than the figure. In short, first index = row = y (not x), and second index = column = x (not y).
Madhava Teja Munagala
le 24 Fév 2018
Modifié(e) : Madhava Teja Munagala
le 24 Fév 2018
For example I have rice image, How to find no of pixels in image,,, How to write code for that
Image Analyst
le 24 Fév 2018
Multiply the rows by the columns.
[rows, columns, numberOfColorChannels] = size(riceImage);
numPixelsInImage = rows * columns;
This gives you the total number of pixels in the entire image, rice and background included.
Gustavo Liñán Cembrano
le 23 Oct 2019
If your image is single channel (grayscale), then you simply do:
NumPixelsInImage=length(riceImage(1:end));
Getting familiar with the (1:end) operation on images is useful sometimes.
Guillaume
le 23 Oct 2019
Actually, getting familiar with numel which in all respects is much better than length would be better. Your code is simply:
numPixelsInImage = numel(riceImage);
which will be faster as it doens't ask matlab to reshape the image (a waste of time) just so you can know how many elements there are.
Image Analyst
le 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.
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
le 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.
Youssef Khmou
le 21 Août 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
snehal jaipurkar
le 23 Nov 2016
0 votes
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 commentaires
SHILPA K K
le 10 Nov 2018
Modifié(e) : madhan ravi
le 10 Nov 2018
Image Analyst
le 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
le 25 Fév 2019
0 votes
how can i convert an 28*28 pixel image into 1*784 ? means in 1 row
1 commentaire
Image Analyst
le 25 Fév 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.
Asad Alam
le 25 Fév 2021
0 votes
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
2 commentaires
Gustavo Liñan
le 25 Fév 2021
% Since you're specifying a single scalar threshold, I assume your image is grayscale
% If you want a binary of the same size just use, assuming your image is already in memory
% in variable YourImage;
YourThreshold=300;
RESULT=YourImage<YourThreshold;
%If your image is in a File;
YourImage=imread(fullfile(YourImagePath,YourImageFileName));
RESULT=YourImage<YourThreshold;
%Now if you want this result as a vector of Ncols x Nrows elements, just do
RESULT_VECTOR=RESULT(1:end);
%The indexes for these pixels are simply
MY_PIX_IDX=find(RESULT_VECTOR)
%Now if you want to extract the pixel values satisfying the condition
YOUR_PIXELS_BELOW_THRES=YourImage(MY_PIX_IDX);
The question is indeed a bit vague to give a completely operative solution, hope these hints help.
Image Analyst
le 25 Fév 2021
Modifié(e) : Image Analyst
le 25 Fév 2021
To get a binary image "map" of where those pixels are, you can do
binaryImage = yourImage < 300;
Now these might be some irregularly-shaped "blobs" as we call them in image processing. To extract those pixels in the blobs into one giant list (vector), you can do
listOfPixelValues = yourImage(binaryImage);
If you want the values of each blob separate from all other blobs, you need to call regionprops
props = regionprops(binaryImage, 'PixelValues');
props is a structure array. You can also ask for the data to come back in a table instead of a structure array if you want.
Catégories
En savoir plus sur Region and Image Properties dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
