Unable to use a value of type string as an index

57 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2021-8-3
This syntax is not supported by MATLAB:
ext == ".jpg" || ".png" || ".jpeg" || ".PNG"
You should use ISMEMBER or similar.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2021-7-31
编辑:Cris LaPierre 2021-7-31
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 个评论
Cris LaPierre
Cris LaPierre 2021-8-3
编辑:Cris LaPierre 2021-8-3
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by