Unable to use a value of type string as an index
70 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Md Shahidul Islam
le 31 Juil 2021
Commenté : Stephen23
le 3 Août 2021
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 commentaire
Stephen23
le 3 Août 2021
This syntax is not supported by MATLAB:
ext == ".jpg" || ".png" || ".jpeg" || ".PNG"
You should use ISMEMBER or similar.
Réponse acceptée
Cris LaPierre
le 31 Juil 2021
Modifié(e) : Cris LaPierre
le 31 Juil 2021
Correct. An index must be a positive integer value. If you instead intended to pass in a variable containing the index values, remove the quotes.
camera_matrix = input_params(camera_matrix);
8 commentaires
Cris LaPierre
le 3 Août 2021
Modifié(e) : Cris LaPierre
le 3 Août 2021
Did a little searching and have come up with the following MATLAB syntax for reading your json file.
raw = fileread(input_name);
input_params = jsondecode(raw);
This adds each id to a field in the structure input_params.
input_params =
rms: 0.7904
camera_matrix: [3×3 double]
dist_coefs: [-0.0151 -0.6476 -0.0029 8.3520e-04 2.5650]
rotational_vectors: [1×1 struct]
translational_vectors: [1×1 struct]
image_files_names: {19×1 cell}
You access those using dot notation.
camera_matrix = input_params.camera_matrix;
dist = input_params.dist_coefs;
tvec_json = input_params.translational_vectors;
rvec_json = input_params.rotational_vectors;
Note that there are other issues in your code where you are using python syntax instead of MATLAB syntax.
Clearly you know what you are doing and just need some syntax help. It might be worth familiarizing yourself with the basics of MATLAB syntax by going through at least some of MATLAB Onramp.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!