Code not displaying figure

3 次查看(过去 30 天)
Gad Licht
Gad Licht 2024-7-22
评论: Voss 2024-7-26
I was told to implement this code form a texbok, but it isn't displaying figure like in textbook. Last lines separated by enter has display figure part
I prefer to keep it together as one code. I am very new to matlab.
close all
%image parameters
W = 400;
H = 400;
% object parameters
rho = 200;
A = 0.4;
B = 0.2;
alpha = pi /10;
xo = 0.3;
yo = 0.5;
% backprojection parameters
ntheta = 100;
srange = 2;
ns = 100;
function [P] = ellipseproj(A, B, rho, theta, s, alpha, xo, yo)
gamma = atan2(yo, xo);
d = sqrt( xo * xo + yo * yo);
thetanew = theta - alpha;
snew = s - d * cos(gamma - theta);
% use translated/rotated values
s = snew;
theta = thetanew;
% find a^2 (theta)
ct = cos(theta);
st = sin(theta);
a2 = A*A*ct*ct + B*B*st*st;
atheta = sqrt(a2);
% return value if outside ellipse
P = 0;
if( abs(s) <= atheta )
% inside ellipse
P = 2 * rho * A * B / a2 * sqrt(a2 - s * s);
end
end %added
function [projmat, svals, thetavals] = ...
ellipseprojmat(A, B, ntheta, ns, srange, rho, alpha, xo, yo)
thetamin = 0;
thetamax = pi;
% each row is a projection at a certain angle
projmat = zeros(ntheta, ns);
smin = -srange;
smax = srange;
dtheta = pi/(ntheta - 1);
ds = (smax - smin)/(ns - 1);
svals = smin:ds:smax;
thetavals = thetamin:dtheta:thetamax;
pn = 1;
for theta = thetavals
% calculate all points on the projection line
P = zeros(ns, 1);
ip = 1;
for s = svals
% simple ellipse
[p] = ellipseproj(A, B, rho, theta, s, alpha, xo, yo);
P(ip) = p;
ip = ip + 1;
end
% save projection as one row of matrix
projmat(pn, :) = P';
pn = pn + 1;
end
end %added
function [b] = bpsolve(W, H, projmat, svals, thetavals)
ntheta = length(thetavals);
ns = length(svals);
srange = svals(ns);
b = zeros(H, W);
for iy = 1:H
for ix = 1:W
x = 2 * (ix - 1)/(W - 1) - 1;
y = 1 - 2 * (iy - 1)/(H - 1);
% projmat is the P values, each row is P(s) for a given theta
bsum = 0;
for itheta = 1:ntheta
theta = thetavals(itheta);
s =x*cos(theta) + y*sin(theta);
is = (s + srange)/(srange*2)*(ns - 1) + 1;
is = round(is);
if(is < 1)
is = 1;
end
if(is > ns)
is = ns;
end
Ptheta = projmat(itheta, is);
bsum = bsum + Ptheta;
end
b(iy, ix) = bsum;
end
end
% image parameters
W = 400;
H = 400;
% object parameters
% rho = 200;
A = 0.4;
B = 0.2;
alpha = pi/10;
xo = 0.3;
yo = 0.5;
% backprojection parameters
ntheta = 100;
srange = 2;
ns = 100;
% generate projections
[projmat, svals, thetavals] = ...
ellipseprojmat(A, B, ntheta, ns, srange, rho, alpha, xo, yo); % solve using backprojection
[b] = bpsolve(W, H, projmat, svals, thetavals);
% scale for image display
b = b/max(max(b)); b = b*255;
bi = uint8(b);
figure(1);
clf
image(bi);
colormap(gray(256));
box('on');
axis('off');
shg;
saveas(gcf,'CompileData.png');
end %added

采纳的回答

Voss
Voss 2024-7-26
The last end is in the wrong place. Placing it where you have it makes the projection generation and figure creation code part of bpsolve function, so none of that would be called.
Also, in my opinion it's clearer to have all the script code (i.e., all code that's not in any function) at the top of the script, followed by all the function definitions. See below:
close all
%image parameters
W = 400;
H = 400;
% object parameters
rho = 200;
A = 0.4;
B = 0.2;
alpha = pi /10;
xo = 0.3;
yo = 0.5;
% backprojection parameters
ntheta = 100;
srange = 2;
ns = 100;
% generate projections
[projmat, svals, thetavals] = ...
ellipseprojmat(A, B, ntheta, ns, srange, rho, alpha, xo, yo); % solve using backprojection
[b] = bpsolve(W, H, projmat, svals, thetavals);
% scale for image display
b = b/max(max(b)); b = b*255;
bi = uint8(b);
figure(1);
clf
image(bi);
colormap(gray(256));
box('on');
axis('off');
shg;
saveas(gcf,'CompileData.png');
function [P] = ellipseproj(A, B, rho, theta, s, alpha, xo, yo)
gamma = atan2(yo, xo);
d = sqrt( xo * xo + yo * yo);
thetanew = theta - alpha;
snew = s - d * cos(gamma - theta);
% use translated/rotated values
s = snew;
theta = thetanew;
% find a^2 (theta)
ct = cos(theta);
st = sin(theta);
a2 = A*A*ct*ct + B*B*st*st;
atheta = sqrt(a2);
% return value if outside ellipse
P = 0;
if( abs(s) <= atheta )
% inside ellipse
P = 2 * rho * A * B / a2 * sqrt(a2 - s * s);
end
end %added
function [projmat, svals, thetavals] = ...
ellipseprojmat(A, B, ntheta, ns, srange, rho, alpha, xo, yo)
thetamin = 0;
thetamax = pi;
% each row is a projection at a certain angle
projmat = zeros(ntheta, ns);
smin = -srange;
smax = srange;
dtheta = pi/(ntheta - 1);
ds = (smax - smin)/(ns - 1);
svals = smin:ds:smax;
thetavals = thetamin:dtheta:thetamax;
pn = 1;
for theta = thetavals
% calculate all points on the projection line
P = zeros(ns, 1);
ip = 1;
for s = svals
% simple ellipse
[p] = ellipseproj(A, B, rho, theta, s, alpha, xo, yo);
P(ip) = p;
ip = ip + 1;
end
% save projection as one row of matrix
projmat(pn, :) = P';
pn = pn + 1;
end
end %added
function [b] = bpsolve(W, H, projmat, svals, thetavals)
ntheta = length(thetavals);
ns = length(svals);
srange = svals(ns);
b = zeros(H, W);
for iy = 1:H
for ix = 1:W
x = 2 * (ix - 1)/(W - 1) - 1;
y = 1 - 2 * (iy - 1)/(H - 1);
% projmat is the P values, each row is P(s) for a given theta
bsum = 0;
for itheta = 1:ntheta
theta = thetavals(itheta);
s =x*cos(theta) + y*sin(theta);
is = (s + srange)/(srange*2)*(ns - 1) + 1;
is = round(is);
if(is < 1)
is = 1;
end
if(is > ns)
is = ns;
end
Ptheta = projmat(itheta, is);
bsum = bsum + Ptheta;
end
b(iy, ix) = bsum;
end
end
end %added

更多回答(2 个)

Steven Lord
Steven Lord 2024-7-26
The behavior you described is correct. While your script does define three functions, nowhere in the lines of code in the script prior to the start of the first of those functions do you call any of them. In essence, your script is functionally equivalent to:
close all
%image parameters
W = 400;
H = 400;
% object parameters
rho = 200;
A = 0.4;
B = 0.2;
alpha = pi /10;
xo = 0.3;
yo = 0.5;
% backprojection parameters
ntheta = 100;
srange = 2;
ns = 100;
which defines a number of variables then finishes executing. You need to actually call ellipseproj, ellipseprojmat, and/or bpsolve in the script part of your code if you want them to run. Move this code to before the definition of ellipseproj (and note that the last end that ends the bpsolve function is not included in this section of code; ellipseproj does need to end with an end.)
% image parameters
W = 400;
H = 400;
% object parameters
% rho = 200;
A = 0.4;
B = 0.2;
alpha = pi/10;
xo = 0.3;
yo = 0.5;
% backprojection parameters
ntheta = 100;
srange = 2;
ns = 100;
% generate projections
[projmat, svals, thetavals] = ...
ellipseprojmat(A, B, ntheta, ns, srange, rho, alpha, xo, yo); % solve using backprojection
[b] = bpsolve(W, H, projmat, svals, thetavals);
% scale for image display
b = b/max(max(b)); b = b*255;
bi = uint8(b);
figure(1);
clf
image(bi);
colormap(gray(256));
box('on');
axis('off');
shg;
saveas(gcf,'CompileData.png');

Image Analyst
Image Analyst 2024-7-26
Well you never plot anything and evidently you never get to the line where you call image to display an image.
Try debugging and stepping through the code one line at a time. See this link. YOui'll have to learn how to debug things yourself eventually.
In the meantime if you still need help, show us a photo of the textbook so we know what it's supposed to look like.
  2 个评论
Gad Licht
Gad Licht 2024-7-26
This isn't useful, because basically you told me have you tried fixing it? Which anser is yes. This response is so generic it could apply to any code. Nor is figure would be needed when it is not displaying anything. And yes, I have tried what you suggest but often when beginning to learn you need some help and after awhile debugging on your own becomes easier.
Gad Licht
Gad Licht 2024-7-26
You could have just moved on if did not want to help.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by