When I run this code an error occurs that says array indices must be positive or logical values.

2 views (last 30 days)
clc;
close all;
clear;
PD=phantom('Modified Shepp-Logan');
padimage = [2,2];
PD= padarray(PD,padimage);
subplot(2,3,1)
imagesc(PD);
colormap('gray');
title('Modified Shepp-Logan')
freq = 2;
thetas = 0:freq:180;
gtheta = length(thetas);
gl = size(PD,1);
sinogram = zeros(gl,gtheta);
for i = 1:length(thetas)
tmpImage = imrotate(PD,-thetas(i),'bilinear','crop');
sinogram(:,i) = sum(tmpImage);
end
subplot(2,3,2)
imagesc(sinogram);
title('Circle Sinogram');
thetas=0;
Fl = size(sinogram,1);
Ftheta = length(thetas);
thetas = (pi/180)*thetas;
g0 = zeros(Fl,Fl);
Fmid = ceil(Fl/2);
[x,y] = meshgrid(ceil((-Fl)/2):ceil(Fl/2-1));
for i = 1:Ftheta
rotCoords = Fmid+round(x*sin(thetas(i)) + y*cos(thetas(i)));
rotCoords=floor(abs(rotCoords));
g0 = g0 + sinogram(rotCoords,i)./Ftheta;
end
subplot(2,3,3);
imagesc(g0);
title('Simple backprojection')
% Here is the error
Index in position 1 is invalid. Array indices must be positive integers or logical
values.
Error in P4P2 (line 41)
g0 = g0 + sinogram(rotCoords,i)./Ftheta;

Answers (2)

David Hill
David Hill on 22 Mar 2022
Not sure what you are trying to do, but rotCoords is 260x260 and sinogram is a 260x91. You cannot index inside sinogram with rotCoords, you will get that error message.
  3 Comments
David Hill
David Hill on 22 Mar 2022
g0 = g0 + sinogram(rotCoords+1)./Ftheta;%rotCoords has values of 0 to 259. You cannot linear index with zero. Adding 1 will allow linear indexing into sinogram.
Levi Stevens
Levi Stevens on 23 Mar 2022
I'm trying to get the sample back projection to be similar to Modified Shepp-Logan as I increase the number of angles used. If I comment out the thetas=0 part of the code, I get the resulting image shown here. This is the closest I can seem to get to the modified Shepp-Logan.

Sign in to comment.


Image Analyst
Image Analyst on 23 Mar 2022
This is one of our most asked FAQ's. So see the FAQ on this error for a thorough discussion:

Categories

Find more on Spline Postprocessing 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!