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

186 ビュー (過去 30 日間)
Dhanya
Dhanya 2014 年 8 月 21 日
コメント済み: DGM 2023 年 2 月 12 日
I need the values of the pixels from an image. I need values for each pixel separately.
  3 件のコメント
Image Analyst
Image Analyst 2021 年 1 月 11 日
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
DGM 2023 年 2 月 12 日
Oh that's easy. Say you have an image called myimage. Then to detect pixels:
haspixels = ~isempty(myimage);
You're welcome.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2014 年 8 月 21 日
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 件のコメント
Claudio Ignacio Fernandez
Claudio Ignacio Fernandez 2020 年 7 月 16 日
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 2020 年 7 月 16 日
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.

サインインしてコメントする。

その他の回答 (6 件)

Ben11
Ben11 2014 年 8 月 21 日
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));
  6 件のコメント
snehal jaipurkar
snehal jaipurkar 2016 年 11 月 24 日
and sir how to run this program on 50 images stored in a folder?
Image Analyst
Image Analyst 2016 年 11 月 24 日
Code samples are in the FAQ

サインインしてコメントする。


yonatan gerufi
yonatan gerufi 2014 年 8 月 21 日
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 件のコメント
Guillaume
Guillaume 2019 年 10 月 24 日
編集済み: Guillaume 2019 年 10 月 24 日
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 2019 年 10 月 24 日
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
Youssef Khmou 2014 年 8 月 21 日
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
snehal jaipurkar 2016 年 11 月 23 日
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 件のコメント
SHILPA K K
SHILPA K K 2018 年 11 月 10 日
編集済み: madhan ravi 2018 年 11 月 10 日
Image Analyst
Image Analyst 2018 年 11 月 10 日
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
Umar Awan 2019 年 2 月 25 日
how can i convert an 28*28 pixel image into 1*784 ? means in 1 row
  1 件のコメント
Image Analyst
Image Analyst 2019 年 2 月 25 日
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
Asad Alam 2021 年 2 月 25 日
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 件のコメント
Gustavo Liñan
Gustavo Liñan 2021 年 2 月 25 日
% 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
Image Analyst 2021 年 2 月 25 日
編集済み: Image Analyst 2021 年 2 月 25 日
This is not an answer to @Dhanya and should have been it's own question. Anyway...
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by