Freeman chain code object plot

4 views (last 30 days)
I am trying to generate an image based on an initial x and y coordinates and a freeman chain code as shown in code below but it does not plot anything, any idea why?
Code:
cc = '03222232232232332334435455455556555545555555454545454454445444444444344444344434443444344344344434434434434434434344343443434343443434343434434343434343434334343434343343434334343343433434334334343343343343343343343343343343343343334334334333433433433343334334333433343333433343333433334333334333334333333433333333343333333333333333333333333333333333333323333333233333323333323333233323332333233323323323323323323323233233232323323232323232332232323232323223232322323223232232232232232223223222322232222322232222232222222322222222232222222222222222221221221221221221212121212121212112121212121212121212121212121223343343343343433433556565556556565565565655655656556556565565565565555655565556565565565565565656556565655656566565656566565665665656656665665665666566656666566665666665666666566666666665666666666666666666666666666666676666666666766666667666667666667666676667666766676667667666766766766766767667667676676767667676767676767676767676767767677676767767767767767767767776776777767776777776777777677777777777777776778777777777778777777787777877778777877787778778778778778778778778787787877878787787878787787878787878787787878787878877887878787878788787878878787887878878788787887887887887887878887887887887888788788878887888878887888878888788888788888878888888788888888887888888888888888888888888888818888888888188888818888818888188818881888188188188188181881818188181818181811818181181181181181181111811111181111111111122222'
sz = 25;
c = 'r';
x = 100;
y = 50;
for i = 1 : strlength(cc)
if cc(i) == 0
x = x+1;
scatter(x,y,sz,c,'filled')
elseif cc(i) == 1
x = x+1;
y = y+1;
scatter(x,y,sz,c,'filled')
elseif cc(i) == 2
y = y+1;
scatter(x,y,sz,c,'filled')
elseif cc(i) == 3
x = x-1;
y = y+1;
scatter(x,y,sz,c,'filled')
elseif cc(i) == 4
x = x-1;
scatter(x,y,sz,c,'filled')
elseif cc(i) == 5
x = x-1;
y = y-1;
scatter(x,y,sz,c,'filled')
elseif cc(i) == 6
y = y-1;
scatter(x,y,sz,c,'filled')
elseif cc(i) == 7
x = x+1;
y = y-1;
scatter(x,y,sz,c,'filled')
end
end
  1 Comment
Nolan Canegallo
Nolan Canegallo on 24 Jan 2021
Edited: Nolan Canegallo on 24 Jan 2021
Firstly, why are there 8's in the input string? What do they correspond to?
Secondly, the reason the points are not staying is because you need to put a
hold on
after you create your figure so that repeated calls to scatter will be added to the plot instead of being overwritten.
Thirdly, if you are not animating your chain, then saving the data points in a variable and then plotting all at once could save time/efficiency.
Finally, if possible, switching the input from a string to a column or row vector of integers would be beneficial.

Sign in to comment.

Accepted Answer

Nolan Canegallo
Nolan Canegallo on 24 Jan 2021
Edited: Nolan Canegallo on 24 Jan 2021
clear; clc; close all;
%Standard clearing of workspace
cc = '03222232232232332334435455455556555545555555454545454454445444444444344444344434443444344344344434434434434434434344343443434343443434343434434343434343434334343434343343434334343343433434334334343343343343343343343343343343343343334334334333433433433343334334333433343333433343333433334333334333334333333433333333343333333333333333333333333333333333333323333333233333323333323333233323332333233323323323323323323323233233232323323232323232332232323232323223232322323223232232232232232223223222322232222322232222232222222322222222232222222222222222221221221221221221212121212121212112121212121212121212121212121223343343343343433433556565556556565565565655655656556556565565565565555655565556565565565565565656556565655656566565656566565665665656656665665665666566656666566665666665666666566666666665666666666666666666666666666666676666666666766666667666667666667666676667666766676667667666766766766766767667667676676767667676767676767676767676767767677676767767767767767767767776776777767776777776777777677777777777777776778777777777778777777787777877778777877787778778778778778778778778787787877878787787878787787878787878787787878787878877887878787878788787878878787887878878788787887887887887887878887887887887888788788878887888878887888878888788888788888878888888788888888887888888888888888888888888888818888888888188888818888818888188818881888188188188188181881818188181818181811818181181181181181181111811111181111111111122222';
sz = 25;
c = 'r';
x = zeros(strlength(cc)+1,1); %Consider storing values and pre-allocating to save time
y = zeros(strlength(cc)+1,1);
x(1) = 100;
y(1) = 50;
If saving the values is required, this is one way to accomplish that. It also allows for faster plotting.
figure %Creates a blank figure object
for i = 1 : strlength(cc)
switch str2double(cc(i)) %Switches are faster than if/elseif with this many options
case {0 8} %Assumes 0 and 8 are the same case
x(i+1) = x(i)+1;
y(i+1) = y(i);
case 1
x(i+1) = x(i)+1;
y(i+1) = y(i)+1;
case 2
x(i+1) = x(i);
y(i+1) = y(i)+1;
case 3
x(i+1) = x(i)-1;
y(i+1) = y(i)+1;
case 4
x(i+1) = x(i)-1;
y(i+1) = y(i);
case 5
x(i+1) = x(i)-1;
y(i+1) = y(i)-1;
case 6
x(i+1) = x(i);
y(i+1) = y(i)-1;
case 7
x(i+1) = x(i)+1;
y(i+1) = y(i)-1;
otherwise
error('Invalid')
end
end
%One plot for efficiency
scatter(x,y,sz,c,'filled')
%Sets plot area and aspect ratio to display image properly
axis tight
daspect([1 1 1])
Output:

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!