Trying to plot 20 points on top of an image for a curve. After solution in class was posted, when running it I get "Conversion to double from cell is not possible." Why?

2 次查看(过去 30 天)
Assignment Question:
Take a screenshot of the following curve and save it in the same folder as your MATLAB script. You will need to submit the saved image along with your MATLAB script. Accepted formats for the image are .jpg and .png. with width between 400 and 800 pixels and height between 250 and 500 pixels.
The image can then be viewed in MATLAB as follows:
%% Question 3
filename = % the file name of your screenshot as a string
[y,z] = imread(filename);
figure, imshow(y);
Implement the function mybezier that traces the Bézier curve with control points P0, P1, P2, and P3 so that the following code shows 20 circles on the curve where the first and last circles are at the ends of the curve. The following image is a sample of what the final output should look like. (Note that the curve is not necessarily your curve for this question.)
N = 20;
t = linspace(0,1,N);
P0 = % Control point P0
P1 = % Control point P1
P2 = % Control point P2
P3 = % Control point P3
mybezier = @(t) % Fill in the rest of the code.
pts = arrayfun(mybezier, t, 'UniformOutput', false); % Generate points on the Bézier curve
% Extract the x-y coordinates of the points
x=zeros(N,1); y=zeros(N,1);
for i=1:N
x(i)=pts{i}(1); y(i)=pts{i}(2);
end
% Plot the points on top of the image
hold on; scatter(x, y);
Given Solution to Problem that causes "Conversion to double from cell is not possible" error in line 25.
filename = "A3_Exemplar_Curve.png"
[y,z] = imread(filename);
figure, imshow(y);
% Implement the function mybezier that traces the Bézier curve with control
% points P0, P1, P2, and P3 so that the following code shows 20 circles on
% the curve where the first and last circles are at the ends of the curve.
N = 20;
t = linspace(0,1,N);
P0 = [65 230];
P1 = [935 -325];
P2 = [235 1225];
P3 = [1000 240];
mybezier = @(t) {(1-t)^3*P0+3*(1-t)^2*t*P1+3*(1-t)*t^2*P2+t^3*P3};
pts = arrayfun(mybezier, t, 'UniformOutput', false); % Generate points on the Bézier curve
% Extract the x-y coordinates of the points
x=zeros(N,1); y=zeros(N,1);
for i=1:N
x(i)=pts{i}(1); y(i)=pts{i}(2);
end
% Plot the points on top of the image
hold on; scatter(x, y);
The Error...
Conversion to double from cell is not possible.
Error in untitled (line 25)
x(i)=pts{i}(1); y(i)=pts{i}(2);

采纳的回答

Voss
Voss 2023-3-9
Change the function mybezier to return a double rather than a cell array, i.e., remove the {}
filename = "peppers.png"
filename = "peppers.png"
[y,z] = imread(filename);
figure, imshow(y);
% Implement the function mybezier that traces the Bézier curve with control
% points P0, P1, P2, and P3 so that the following code shows 20 circles on
% the curve where the first and last circles are at the ends of the curve.
N = 20;
t = linspace(0,1,N);
P0 = [65 230];
P1 = [935 -325];
P2 = [235 1225];
P3 = [1000 240];
% mybezier = @(t) {(1-t)^3*P0+3*(1-t)^2*t*P1+3*(1-t)*t^2*P2+t^3*P3};
mybezier = @(t) (1-t)^3*P0+3*(1-t)^2*t*P1+3*(1-t)*t^2*P2+t^3*P3;
pts = arrayfun(mybezier, t, 'UniformOutput', false); % Generate points on the Bézier curve
% Extract the x-y coordinates of the points
x=zeros(N,1); y=zeros(N,1);
for i=1:N
x(i)=pts{i}(1); y(i)=pts{i}(2);
end
% Plot the points on top of the image
hold on; scatter(x, y, 'w'); % I changed to white so it shows up better on this image

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by