# How to plot intensity profile of an image

429 views (last 30 days)
Viron Gil Estrada on 22 Mar 2018
Commented: Walter Roberson on 9 Jun 2020 at 21:23
I want to plot the intensity profile of this image. I tried using improfile but I don't really know how it works. Can anyone help? #### 1 Comment

Walter Roberson on 22 Mar 2018
An intensity profile along which direction?
If you want a profile along a horizontal or vertical line then just use array indexing to extract the vector, and then plot it.

Akira Agata on 22 Mar 2018
Like this?
x = [0 size(I,2)];
y = [size(I,1)/2 size(I,1)/2];
c = improfile(I,x,y);
figure
subplot(2,1,1)
imshow(I)
hold on
plot(x,y,'r')
subplot(2,1,2)
plot(c(:,1,1),'r')
hold on
plot(c(:,1,2),'g')
plot(c(:,1,3),'b') Show 1 older comment
Akira Agata on 22 Mar 2018
Thanks, I'm glad to hear that!
If you use grayscale image, improfile function returns N-by-1 vector. So you should change the last 4 lines by plot(c).
Viron Gil Estrada on 22 Mar 2018
Thanks a lot! That's all I need.
Warid Islam on 9 Jul 2019
Hi,
How can we plot the intensity profile of the same image along the vertical direction instead of the horizontal direction? Thank you.

Akira Agata on 9 Jul 2019
Hi Warid-san,
You can do it by the same way, like the following.
% Since size(I,2)/2 = 215.5, I have set the value '215' directly for x
x = [215 215];
y = [0 size(I,1)];
c = improfile(I,x,y);
figure
subplot(2,1,1)
imshow(I);
hold on
plot(x,y,'r')
subplot(2,1,2)
plot(c(:,1,1),'r')
hold on
plot(c(:,1,2),'g')
plot(c(:,1,3),'b') Warid Islam on 10 Jul 2019
Hi Akira,
Thank you very much for the help.
Manoj Kumar on 23 Mar 2020
Hi,
if there are more than one spot, say 5 (randomly distributed), then how to plot the intensity across all of them?
Thanks
Akira Agata on 23 Mar 2020
Hi Manoj-san,
Could you upload a sample image file for trying?

Lorenzo Gentili on 20 Aug 2019
Hi Akira,
I'm trying to obtain an intesity profile of the image below, but I need more than a line. I would like to have at least 5 vertical line in order to understand how the intesity vary in different point of the image ( lets say the 2 boundaries, the center and 2 point between the boundaries and the center). Is it possible to obtain an unique intensity profile that is the sum of 5 intesity profile obtained by the verical lines ?
Lorenzo Image Analyst on 20 Aug 2019
To get a mean vertical profile between two columns:
verticalProfile = mean(grayImage(:, column1:column2), 2);
To get a mean horizontal profile between two rows:
horizontalProfile = mean(grayImage(row1:row2, :), 1);
Lorenzo Gentili on 20 Aug 2019

r r on 15 May 2020
Edited: Walter Roberson on 16 May 2020
I have image of a tumor in it and I want to do it the same way https://www.mathworks.com/matlabcentral/answers/389889-how-to-plot-intensity-profile-of-an-image
, but every time the response is
'improfile' requires Image Processing Toolbox.
Error in line (line 4)
c = improfile(I,x,y);

#### 1 Comment

Walter Roberson on 16 May 2020
You need the Image Processing Toolbox to use improfile()
improfile() uses linspace() to create vectors of coordinates between the two endpoints, and uses interp2() to interpolate at the coordinates; it does the interpolation once for each color channel.
The question then becomes what coordinates you should interplate at

fransec on 9 Jun 2020 at 11:15
Hello everybody,
Anyone knows how to make an image profile like ones you showed, but but along the diagonal of an image?
Thank you so much.

#### 1 Comment

Walter Roberson on 9 Jun 2020 at 21:23
[rows, cols, ~] = size(YourImage);
n = min(rows,cols);
prof_diag = improfile(YourImage, [1 n], [1 n]); %diagonal
prof_antidiag = improfile(YourImage, [n 1], [1 n]); %anti-diagonal