Unable to use a value of type string as an index
Mostrar comentarios más antiguos
There have already an answer about this error. But I cannot solve this problem by this way. Please see my code and give me a solution.
My code:
undistorb_images([], []);
function [tvec, rvec, camera_matrix, dist] = read_wp2c(input_name)
f = fopen(input_name);
input_params = fread(f,inf);
camera_matrix = input_params("camera_matrix");
dist = input_params("dist_coefs");
tvec_json = input_params("translational_vectors");
rvec_json = input_params("rotational_vectors");
tvec = [];
rvec = [];
for i = 1:range(len(tvec_json))
tvec.append(array(tvec_json("image" + str(i))));
rvec.append(array(rvec_json("image" + str(i))));
end
end
function undistorb_images(inputParams, result)
%if result is None:
if isempty(result)
input_name = "output_wp2camera.json";
[tvec, rvec, camera_matrix, dist] = read_wp2c(input_name);
else
tvec = result(4);
rvec = result(3);
camera_matrix = result(1);
dist = result(2);
end
if isempty(inputParams)
image_path = "images";
else
image_path = inputParams("opencv_storage","settings","Images_Folder");
end
image_files = [];
for f = os.listdir(image_path)
ext = os.image_path.splitext(f.lower());
if ext == ".jpg" || ".png" || ".jpeg" || ".PNG"
image_files.append(os.image_path.join(image_path, image_files));
end
end
image_file_name = [];
if ~isempty(image_files)
for image_file = image_files
image_file_name.append(image_file);
image = imread(image_path + "/" + image_file);
[height, width] = size(image);
print(str(height) + " " + str(width))
[newcameramtx, roi] = getOptimalNewCameraMatrix(camera_matrix, dist, [width,height], 0, [width,height]);
% dst = cv2.undistort(image, camera_matrix, dist, None, newcameramtx)
[mapx, mapy] = cv.initUndistortRectifyMap(camera_matrix, dist, None, newcameramtx, [width,height], 5);
dst = cv.remap(image, mapx, mapy, INTER_LINEAR);
x, y, w, h = roi;
dst = dst(y:y+h, x:x+w);
[height, width] = size(dst);
print(str(height) + " " + str(width));
imwrite("undistortion/" + image_file, dst);
end
end
end
I find this error:

1 comentario
Stephen23
el 3 de Ag. de 2021
This syntax is not supported by MATLAB:
ext == ".jpg" || ".png" || ".jpeg" || ".PNG"
You should use ISMEMBER or similar.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Convert Image Type en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!