
How to create a Radius vs. Intensity plot of an image (matrix)
2 views (last 30 days)
Show older comments
I have an intensity matrix size=[86 86] and I need to find the radii of all positions in the matrix with respect to a corner. The matlab index values are all exactly 0.25cm apart. How do I find the total intensity summed over all matrix positions with the same radius?
I need to create a plot of Radius (x) vs. Intensity (y).
Let any given position in the matrix be P(i,j) and the corner location P(a,b).
I know Radius = sqrt((i-a)^2 + (j-b)^2)
I am assuming I will also need to create a nested FOR loop so that I can calculate the radius at every matrix position (FOR i<=84 and j<=84).
Can anyone help?
0 Comments
Answers (1)
Image Analyst
on 14 May 2015
Try using accumarray()
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%===============================================================================
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'moon.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Get distances for every pixel
[c, r] = meshgrid(1:rows, 1:columns);
calibrationFactor = 0.25; % There are 0.25 real world units per index.
distances = sqrt((c-1).^2 + (r-1).^2) * calibrationFactor;
% Round it to the nearest single unit (nearest 4 pixel)
distances = round(distances);
% Add 1 so we can use that as indexes in accumarray
distances = distances + 1;
% Now get sum of intensity at each distance
out = accumarray(distances(:), grayImage(:));
subplot(1, 2, 2);
plot(out, 'b-', 'LineWidth', 3);
grid on;
xlabel('Distance', 'FontSize', fontSize);
ylabel('Sum of gray levels', 'FontSize', fontSize);
title('GL sum vs. distance', 'FontSize', fontSize, 'Interpreter', 'None');

See Also
Categories
Find more on Matrix Indexing 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!