how to find pixel values and intensity of grayscale image and plot them (x-axis as pixel value and y-axis as pixel intensity)

40 views (last 30 days)
I have an grayscale image with values ranging from -0.86 to 0.53. I want to extract pixel values of whole image and intensity of each pixel and plot them. The x axis should be pixel value and y-axis should be intensity and the x-axis interval is 0.2.
I got some solutions before to store pixel values by impixel but it store values according to row and column. Can anyone help me in this matter.
  3 Comments
DGM
DGM on 3 Oct 2023
Edited: DGM on 3 Oct 2023
(replying to a comment that doesn't exist anymore)
If you want a histogram, then I don't see why the distinction between value and intensity is relevant. It's still not clear that it even exists.
If you want a histogram, just use histogram().
% some arbitrarily-scaled array
A = randn(100) + 4*randi([0 1],100,100);
% displayed with scaled colormapping
imshow(A,[])
% show the histogram
figure
h = histogram(A);
If you want the bin edges and counts, you can either get that information from the histogram object h, or you can use histcounts() instead.
Image Analyst
Image Analyst on 3 Oct 2023
I agree with @DGM - pixel value and pixel intensity are the same thing (to everyone except you). It makes no sense to plot one vs the other. Clarify not only what (you think) you need but why you want it (in other words what will you do with that information once you have it)

Sign in to comment.

Accepted Answer

DGM
DGM on 3 Oct 2023
Edited: DGM on 3 Oct 2023
I'm going to go out on a limb and guess that this is where it's going. If the relationship is that between the data values and the graylevels used to render the image, then:
% some arbitrarily-scaled array
A = randn(10);
% displayed with scaled colormapping
imshow(A,[])
% input and output ranges
inrange = caxis(gca);
outrange = [0 1];
% some test input in the same range
x = linspace(inrange(1),inrange(2),1000);
% interpolate
grayval = interp1(inrange,outrange,x);
% quantize
nlevels = size(get(gca,'colormap'),1);
grayval = round((grayval*nlevels - 0.5)*(1-eps))/(nlevels-1);
grayval = imclamp(grayval);
% plot
plot(x,grayval)
xlabel('data value')
ylabel('gray level')
It's hard to see with the default map length, but that's not a straight line. It's a simple piecewise-constant approximation of a linear function. The parameters are input extrema and the colormap length. All other properties of the data are irrelevant. This graph tells us about the colormapping functionality. It doesn't tell us anything about the data other than its extrema. I don't know why we'd want to plot it.
Obviously this is not true for colormaps other than gray().
This is also only accurate if the map length is either 16 or 256. The above code associates the input levels with the actual gray value specified in the gray() colormap. What's actually displayed on screen will have been quantized once more to 256 levels as it's rendered on screen, and so the gray levels will no longer match the specified colormap exactly. They will no longer be uniformly distributed, and the result may no longer even represent the specified number of gray levels.
If you were using a map length other than 16 or 256, and you wanted to simulate the degraded ephemeral display copy instead of the ideal colormapped image, you could add one extra step:
% degrade the estimate to match the display
grayval = floor(grayval*255)/255;
I have no idea if this is even an answer to the question.
  10 Comments
DGM
DGM on 9 Oct 2023
@Image Analyst I could see how a majority population of near-Inf values would make display and processing of an image misleading when all the actual content is hidden near zero. In any effort to find a threshold point, it would take some fussing to get histogram() to show anything that wasn't confusing.
Image Analyst
Image Analyst on 10 Oct 2023
Right but he said he believes he has a "grayscale image with values ranging from -0.86 to 0.53". So once this threshold between to get rid (mask out) bogus values less than -0.86, you could take the histogram of the valid remaining values like we both suggested.

Sign in to comment.

More Answers (2)

Tanmoyee Bhattacharya
Tanmoyee Bhattacharya on 4 Oct 2023
Edited: Tanmoyee Bhattacharya on 4 Oct 2023
Thankyou so much for your inconsistent help.
I have little knowledge about this (If anything I asked irrelevant please ignore). My matlab version is R2013a. The image I have used is too large (more than 5mb) so that I cannot attach it. I attached the image screenshot and the histogram which I want to plot.
If you provide any option to attach my file (by mail also if possible) I will attach my image file.

Image Analyst
Image Analyst on 5 Oct 2023
I'm not sure why you're taking a histogram of the binary image. I think what you meant was to take the histogram of the masked part of the image, so why don't you try this:
grayImage = imread('E:\waterbody_project\processed_sen\mnwi.tif');
mask = grayImage < 255; % Non white parts only.
histogram(grayImage(mask));

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!