Image Processing Dimension Error
1 view (last 30 days)
Show older comments
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")
cW_H
%* Part B *%
% Compute the inverse transformation matrix
wC_H = inv(cW_H);
% Display the inverse transformation matrix
disp("Part B")
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")
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_world2 = transpose(points_world)
points_camera = cW_H(1:3, 1:3) * points_world2 + cW_H(1:3, 4)
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
% Show the resulting image
imshow(image);
0 Comments
Answers (1)
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.
See Also
Categories
Find more on Read, Write, and Modify Image 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!