# How to plot pixel intensity as a function of pixel position (beginner)

36 views (last 30 days)
Alfredo Scigliani on 27 Apr 2022
Commented: Alfredo Scigliani on 27 Apr 2022
Hi I am basically a beginner with imaging processing, I have seen many demos about analyzing the rhinos.avi video but as a beginner I find it a bit complicated. So to start I would like to take a single column of pixels from frame and create an intensity vs position plot.
I would like to know how to select a region of the frame because right now I am just selecting the whole picture.
As of now, I am just getting an empty plot, this is how I am trying to do it: (attached is the frame image)
clear; clc; clf; close all; imtool close all;
grayImage = rgb2gray(myImage);
meanGrayLevel = mean2(grayImage);
plot(meanGrayLevel)
Alfredo Scigliani on 27 Apr 2022
I have fixed the image

Riccardo Scorretti on 27 Apr 2022
Hi. You obtain an empty plot because you use mean2, which returns the average of the full image (= a single value):
% Load the image and convert it to BW
clear; clc; clf; close all; imtool close all;
grayImage = rgb2gray(myImage);
size(grayImage)
ans = 1×2
334 638
% This will give you the average value of the whole intensity = a single value
meanGrayLevel = mean2(grayImage);
By using mean, you should obtain what you want:
% This will you give the average for each colum = N values, which can be plotted
meanGrayLevel = mean(grayImage, 1);
% Plot the image together with the average intensity on each column
subplot(2, 1, 1) ; imagesc(grayImage) ; colormap gray
subplot(2, 1, 2) ; plot(meanGrayLevel) ; axis([1 size(grayImage,2) -inf inf]); Finally, you can get a portion of the image by using the usual synpsis for matrix:
% Take a subset of the image
rhino = grayImage(100:250, 200:300);
figure
imagesc(rhino) ; colormap gray ; axis image Alfredo Scigliani on 27 Apr 2022
This is exactly what I was looking for, thanks!!

### Categories

Find more on Image Category Classification in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!