Unable to use a value of type string as an index
조회 수: 50 (최근 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/699512/image.png)
댓글 수: 1
Stephen23
2021년 8월 3일
This syntax is not supported by MATLAB:
ext == ".jpg" || ".png" || ".jpeg" || ".PNG"
You should use ISMEMBER or similar.
채택된 답변
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
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 Center 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!