Issues using retrieving image in for loop using improfile
Show older comments
Hi everyone, I have a for-loop, which is used to translate objects on an image.
I'm trying to retrieve the pixel intensity data on the lines using and have tried to place improfile in the for loop.
The problem is it's giving me the data for only one of the objects and not every single iteration of the for loop.
clear %Clear Workspace
clc %Clear Commands
% line coordinates
x = [200,0,0];
y = [200,0,0];
% set up rotation matrix
nLine = 20;
theta = 2 * pi / nLine;
A = [cos(theta), sin(theta), 0;...
-sin(theta), cos(theta), 0;...
0, 0, 1];
hFig = figure; %figure handle
hL(1) = plot(x,y); % line object handle
hAx = hL(1).Parent; % axes handle
%Image
figure(1)
slp=phantom(250);
set(gca, 'ydir', 'reverse');
J=imtranslate(slp,[0,0],'OutputView','full');
hold on
imshow(J);
%make starburst
for k = 1:nLine;
X = [hL.XData(end-2:end); hL.YData(end-2:end); ones(1,3)];
Y = A * X;
hL.XData = cat(2,hL.XData,Y(1,:));
hL.YData = cat(2,hL.YData,Y(2,:));
end
% translate the starbursts
nStarburst = 250;
dx = 0:50:nStarburst; % horizontal step
dy = dx; % vertical step
for k = 1:nStarburst;
hL(k+1) = copyobj(hL(1), hAx);
hL(k+1).XData = hL(k+1).XData + dx(k);
hL(k+1).YData = hL(k+1).YData + dy(k);
for o = 2:7
figure(o)
improfile(J,hL(k+1).XData,hL(k+1).YData)
end
end
Any help would be appreciated!
1 Comment
Walter Roberson
on 30 Apr 2019
?
You start with hFig = figure(), giving you a figure number of the next unusued figure, a figure number we do not know in advance (we cannot assume no figures are open.) You implicitly write to it with the plot(). There is no "hold on" in effect for this figure.
You then access figure(1), which might or might not happen to be the same figure. You display an image onto figure 1 without destroying any existing content, which might or might not happen to be displaying on top of what you previously plot()'d
You then loop copying objects from the original figure hFig into an axes on the same figure. This might be figure 1 but it might not be.
Within that, you loop accessing figures 2 through 7 . They might or might not happen to already exist; any that do not exist would be created as empty, possibly all at the same place on the display. For each of those figures, you ask that the same profile information be extracted from the array J and displayed as a plot. If hFig happens to be one of these, you will have accidentally cleared the plot you are relying on to exist to create your starburst figure. Each of these improfile() would replace the existing improfile(). It is not obvious why you would want 6 copies of the same improfile figure.
You do not have an explicit drawnow() or pause(), but it happens that figure() is one of the other calls that is defined to trigger drawing, so you do not need that.
Answers (0)
Categories
Find more on Entering Commands 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!