Image Processing Dimension Error

1 view (last 30 days)
Zahryn Martinez
Zahryn Martinez on 10 Jul 2023
Commented: Zahryn Martinez on 10 Jul 2023
Here is the Question I am working on. Below is also the matlab code I have written, but towards the end I get an error talking about how the "index in position 2 exceeds array bounds". Any ideas?
%* Part A *%
% Translation vector
translation = [10; -25; 40];
% Euler angles representing the orientation (REPLACE THESE ANGLES)
theta_x = -1; % Angle around X-axis in radians
theta_y = 0.7; % Angle around Y-axis in radians
theta_z = -0.5; % Angle around Z-axis in radians
% Compute the rotation matrices
Rx = [1, 0, 0; 0, cos(theta_x), -sin(theta_x); 0, sin(theta_x), cos(theta_x)];
Ry = [cos(theta_y), 0, sin(theta_y); 0, 1, 0; -sin(theta_y), 0, cos(theta_y)];
Rz = [cos(theta_z), -sin(theta_z), 0; sin(theta_z), cos(theta_z), 0; 0, 0, 1];
% Combine the translation and rotation matrices
cW_H = [Rx * Ry * Rz, translation; 0, 0, 0, 1];
% Display the transformation matrix
disp("Part A")
Part A
cW_H
cW_H = 4×4
0.6712 0.3667 0.6442 10.0000 -0.7348 0.2143 0.6436 -25.0000 0.0980 -0.9053 0.4132 40.0000 0 0 0 1.0000
%* Part B *%
% Compute the inverse transformation matrix
wC_H = inv(cW_H);
% Display the inverse transformation matrix
disp("Part B")
Part B
wC_H
wC_H = 4×4
0.6712 -0.7348 0.0980 -28.9996 0.3667 0.2143 -0.9053 37.9032 0.6442 0.6436 0.4132 -6.8822 0 0 0 1.0000
%* Part C *%
% Image size
image_width = 256;
image_height = 170;
% Focal length in pixels
focal_length = 400;
% Image center coordinates
image_center_x = image_width / 2;
image_center_y = image_height / 2;
% Intrinsic camera calibration matrix K
K = [focal_length, 0, image_center_x; 0, focal_length, image_center_y; 0, 0, 1];
% Display the intrinsic matrix
disp("Part C")
Part C
K
K = 3×3
400 0 128 0 400 85 0 0 1
%*Part D *%
% Create blank image
image = zeros(image_height, image_width);
% Project the 7 points onto the image
points_world = [6.8158 -35.1954 43.0640;
7.8493 -36.1723 43.7815;
9.9579 -25.2799 40.1151;
8.8219 -38.3767 46.6153;
9.5890 -28.8402 42.2858;
10.8082 -48.8164 56.2858;
13.2690 -58.0988 59.1422;]
points_world = 7×3
6.8158 -35.1954 43.0640 7.8493 -36.1723 43.7815 9.9579 -25.2799 40.1151 8.8219 -38.3767 46.6153 9.5890 -28.8402 42.2858 10.8082 -48.8164 56.2858 13.2690 -58.0988 59.1422
points_world2 = transpose(points_world)
points_world2 = 3×7
6.8158 7.8493 9.9579 8.8219 9.5890 10.8082 13.2690 -35.1954 -36.1723 -25.2799 -38.3767 -28.8402 -48.8164 -58.0988 43.0640 43.7815 40.1151 46.6153 42.2858 56.2858 59.1422
points_camera = cW_H(1:3, 1:3) * points_world2 + cW_H(1:3, 4)
points_camera = 3×7
29.4118 30.2095 33.2570 31.8796 33.1022 35.6147 35.7028 -9.8336 -10.3405 -11.9156 -9.7036 -11.0104 -7.1761 -9.1348 90.3273 91.6095 80.4397 94.8716 84.5238 108.5139 118.3390
for i = 1:size(points_camera, 2)
point_camera = points_camera(:, i);
projected_point = K * point_camera;
projected_point = round(projected_point ./ projected_point(3));
x = abs(projected_point(1));
y = abs(projected_point(2));
image(y, x) = 255; % Set pixel value to white
end
% Display the image with white dots
imshow(image);
% Define point indices for drawing lines
point_indices = [1, 2; 2, 3; 3, 4; 4, 1; 5, 6; 6, 7; 7, 8; 8, 5];
% Draw lines between the points
for i = 1:size(point_indices, 1)
point1 = points_camera(:, point_indices(i, 1));
point2 = points_camera(:, point_indices(i, 2));
projected_point1 = K * point1;
projected_point2 = K * point2;
projected_point1 = round(projected_point1 ./ projected_point1(3));
projected_point2 = round(projected_point2 ./ projected_point2(3));
line_points = [projected_point1(1:2)'; projected_point2(1:2)'];
image = insertShape(image, 'Line', line_points, 'LineWidth', 1, 'Color', 'white');
end
Index in position 2 exceeds array bounds. Index must not exceed 7.
% Show the resulting image
imshow(image);

Answers (1)

KSSV
KSSV on 10 Jul 2023
This line:
point_indices = [1, 2; 2, 3; 3, 4; 4, 1; 5, 6; 6, 7; 7, 8; 8, 5];
should have indices/ values which should be less than or equal to size(points_camera). Note that the dimesnions of size_camera are 3x7. Here in the lines
point1 = points_camera(:, point_indices(i, 1));
point2 = points_camera(:, point_indices(i, 2));
When you extract 7, 8 indices an error will be throws because there is no 8th index in points_camera.
  1 Comment
Zahryn Martinez
Zahryn Martinez on 10 Jul 2023
Here is the updated code, but I am still getting the same error:
% Part A
% Translation vector
translation = [10; -25; 40];
% Euler angles representing the orientation (REPLACE THESE ANGLES)
theta_x = -1; % Angle around X-axis in radians
theta_y = 0.7; % Angle around Y-axis in radians
theta_z = -0.5; % Angle around Z-axis in radians
% Compute the rotation matrices
Rx = [1, 0, 0; 0, cos(theta_x), -sin(theta_x); 0, sin(theta_x), cos(theta_x)];
Ry = [cos(theta_y), 0, sin(theta_y); 0, 1, 0; -sin(theta_y), 0, cos(theta_y)];
Rz = [cos(theta_z), -sin(theta_z), 0; sin(theta_z), cos(theta_z), 0; 0, 0, 1];
% Combine the translation and rotation matrices
cW_H = [Rx * Ry * Rz, translation; 0, 0, 0, 1];
% Display the transformation matrix
disp("Part A")
disp(cW_H)
% Part B
% Compute the inverse transformation matrix
wC_H = inv(cW_H);
% Display the inverse transformation matrix
disp("Part B")
disp(wC_H)
% Part C
% Image size
image_width = 256;
image_height = 170;
% Focal length in pixels
focal_length = 400;
% Image center coordinates
image_center_x = image_width / 2;
image_center_y = image_height / 2;
% Intrinsic camera calibration matrix K
K = [focal_length, 0, image_center_x; 0, focal_length, image_center_y; 0, 0, 1];
% Display the intrinsic matrix
disp("Part C")
disp(K)
% Part D
% Create blank image
image = zeros(image_height, image_width);
% Project the 7 points onto the image
points_world = [6.8158, -35.1954, 43.0640;
7.8493, -36.1723, 43.7815;
9.9579, -25.2799, 40.1151;
8.8219, -38.3767, 46.6153;
9.5890, -28.8402, 42.2858;
10.8082, -48.8164, 56.2858;
13.2690, -58.0988, 59.1422];
points_camera = (cW_H(1:3, 1:3) * points_world' + cW_H(1:3, 4))';
for i = 1:size(points_camera, 1)
point_camera = points_camera(i, :);
projected_point = K * point_camera';
projected_point = round(projected_point ./ projected_point(3));
x = projected_point(1);
y = projected_point(2);
image(y, x) = 255; % Set pixel value to white
end
% Display the image with white dots
disp("Part D")
imshow(image);
% Part E
% Define point indices for drawing lines
point_indices = [1, 2; 2, 3; 3, 4; 4, 1; 5, 6; 6, 7; 7, 8; 8, 5];
% Draw lines between the points
for i = 1:size(point_indices, 1)
point1 = points_camera(point_indices(i, 1), :);
point2 = points_camera(point_indices(i, 2), :);
projected_point1 = K * point1';
projected_point2 = K * point2';
projected_point1 = round(projected_point1 ./ projected_point1(3));
projected_point2 = round(projected_point2 ./ projected_point2(3));
line_points = [projected_point1(1:2)'; projected_point2(1:2)'];
image = insertShape(image, 'Line', line_points, 'LineWidth', 1, 'Color', 'white');
end
% Show the resulting image
disp("Part E")
imshow(image);

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!