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

32 views (last 30 days)

Show older comments

Tanmoyee Bhattacharya
on 3 Oct 2023

Commented: Image Analyst
on 10 Oct 2023

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

% 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
on 3 Oct 2023

### Accepted Answer

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
on 9 Oct 2023

Image Analyst
on 10 Oct 2023

### More Answers (2)

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));

##### 0 Comments

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!