Unable to use a value of type string as an index

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

This syntax is not supported by MATLAB:
ext == ".jpg" || ".png" || ".jpeg" || ".PNG"
You should use ISMEMBER or similar.

Iniciar sesión para comentar.

 Respuesta aceptada

Cris LaPierre
Cris LaPierre el 31 de Jul. de 2021
Editada: Cris LaPierre el 31 de Jul. de 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 comentarios

when I run this without quotes give another error:
Unrecognized function or variable 'camera_matrix'.
Error in undistortion>read_wp2c (line 7)
camera_matrix = input_params(camera_matrix);
Error in undistortion>undistorb_images (line 26)
[tvec, rvec, camera_matrix, dist] = read_wp2c(input_name);
Error in undistortion (line 1)
undistorb_images([], []);
Cris LaPierre
Cris LaPierre el 31 de Jul. de 2021
Editada: Cris LaPierre el 31 de Jul. de 2021
Sorry, I was going too fast apparently. Your are both defining and using your variable camera_matrix in the same line. Since it does not yet exist, you get an error. What are you trying to do? What do you expect the values of camera_matrix to be? Can you define that variale before you use it?
Thank you for your response. Actually I want like this result:
camera_matrix = [[2.36606437e+03 0.00000000e+00 1.26952719e+03]
[0.00000000e+00 2.36563009e+03 9.77625078e+02]
[0.00000000e+00 0.00000000e+00 1.00000000e+00]]
It's read from my json file (output_wp2camera.json).My json file is below :
{
"rms":0.7903896745795049,
"camera_matrix":[
[
2366.0643665497655,
0.0,
1269.5271860793966
],
[
0.0,
2365.6300916139744,
977.6250778865366
],
[
0.0,
0.0,
1.0
]
],
"dist_coefs":[
[
-0.015143991938566,
-0.6476016093124787,
-0.00289474550099209,
0.0008351959540152184,
2.565006966391666
]
],
"rotational_vectors":{
"image0":[
[
-0.010988322261568878
],
[
-0.04754866852622909
],
[
0.01771396934802694
]
],
"image1":[
[
-0.5107935364370293
],
[
-0.016489322822587776
],
[
0.09167866355224037
]
],
"image2":[
[
-0.5259404588083765
],
[
0.19969075396412633
],
[
0.13193670834115254
]
],
"image3":[
[
0.11020184430814861
],
[
-0.35089379829500467
],
[
0.24973801895755296
]
],
"image4":[
[
0.2543753052637152
],
[
-0.2774085492632369
],
[
0.3122903673226887
]
],
"image5":[
[
0.004204697441565756
],
[
-0.2336191123270117
],
[
0.22721384394659583
]
],
"image6":[
[
0.12190922459040796
],
[
-0.016420617555973856
],
[
0.22431184960635064
]
],
"image7":[
[
-0.5889431505755814
],
[
-0.444997875685499
],
[
-0.07227872912481802
]
],
"image8":[
[
0.2172853927815233
],
[
0.025025984885379375
],
[
0.22555104658127095
]
],
"image9":[
[
-0.08900209598160268
],
[
0.01849912185836718
],
[
0.20581810229492248
]
],
"image10":[
[
0.11732185892357068
],
[
0.5348770185193281
],
[
0.6408701578469761
]
],
"image11":[
[
0.029320180881672712
],
[
-0.13927650355632729
],
[
-0.08415484535998083
]
],
"image12":[
[
-0.10132343554299154
],
[
-0.1134662943568563
],
[
-0.06712135113770556
]
],
"image13":[
[
-0.061078050896541614
],
[
-0.08513658952341076
],
[
-0.031273848158253674
]
],
"image14":[
[
0.00355486236692061
],
[
0.14035881806646475
],
[
0.21197312109318914
]
],
"image15":[
[
0.052103123949682244
],
[
-0.03260648336807801
],
[
-0.09983398236407495
]
],
"image16":[
[
-0.061488018649106926
],
[
-0.013791825131343007
],
[
-0.06548416017552736
]
],
"image17":[
[
-0.13554431905398404
],
[
0.030743749824206846
],
[
-0.024044293620168505
]
],
"image18":[
[
-0.03445414926417389
],
[
-0.03563171834367628
],
[
-0.07428169612363332
]
]
},
"translational_vectors":{
"image0":[
[
-49.99044630774815
],
[
-39.7190405880838
],
[
167.95558251589298
]
],
"image1":[
[
-55.60047564670453
],
[
-48.49392365505262
],
[
202.57752298563167
]
],
"image2":[
[
-46.9994685334486
],
[
-48.050133528478284
],
[
196.90713734440828
]
],
"image3":[
[
-34.80155810236431
],
[
-67.20204592250651
],
[
186.71750716223926
]
],
"image4":[
[
-32.66915931520995
],
[
-50.611671677739146
],
[
199.6915185105951
]
],
"image5":[
[
-39.00968044486636
],
[
-51.49377788890596
],
[
180.55295501749632
]
],
"image6":[
[
-41.54283422170167
],
[
-53.39569274571019
],
[
173.70513816135983
]
],
"image7":[
[
-51.97987537914102
],
[
-35.56284011225603
],
[
203.83766067018558
]
],
"image8":[
[
-43.07036741762413
],
[
-50.60735912421525
],
[
171.70912128951693
]
],
"image9":[
[
-41.16313223529565
],
[
-54.598221974127306
],
[
180.8686502794212
]
],
"image10":[
[
-18.029840502930888
],
[
-69.09649950729586
],
[
202.08271930015974
]
],
"image11":[
[
-53.778267096995386
],
[
-35.93973234364238
],
[
181.0921060440921
]
],
"image12":[
[
-49.79025821431097
],
[
-42.41079703012109
],
[
177.85971847328798
]
],
"image13":[
[
-55.00026829034094
],
[
-43.86516711158646
],
[
172.24183249101324
]
],
"image14":[
[
-43.07752430607493
],
[
-57.15258141082155
],
[
183.0049404259796
]
],
"image15":[
[
-53.06739287949906
],
[
-38.88656949168771
],
[
176.15219666110005
]
],
"image16":[
[
-52.01611182605524
],
[
-42.64028053386663
],
[
175.50916056348285
]
],
"image17":[
[
-55.65707768039284
],
[
-43.236942736349576
],
[
196.21755385043963
]
],
"image18":[
[
-58.36383472730503
],
[
-42.024987732332804
],
[
199.83106767504063
]
]
},
"image_files_names":[
[
0,
"p01.png"
],
[
1,
"p02.png"
],
[
2,
"p03.png"
],
[
3,
"p04.png"
],
[
4,
"p05.png"
],
[
5,
"p06.png"
],
[
6,
"p07.png"
],
[
7,
"p08.png"
],
[
8,
"p09.png"
],
[
9,
"p10.png"
],
[
10,
"p11.png"
],
[
11,
"p12.png"
],
[
12,
"p13.png"
],
[
13,
"p14.png"
],
[
14,
"p15.png"
],
[
15,
"p16.png"
],
[
16,
"p17.png"
],
[
17,
"p18.png"
],
[
18,
"p19.png"
]
]
}
You need to find a way to take the information from the json file and move it into MATLAB before you can use the variables. Is this a scenario where you can hard code camera_matrix yourself?
Thank you for your time. I already put my json file in same folder with matlab code but it doesn't work. Actually I trying to convert my python code into MATLAB. If you want, I can show my python code.
If I can piece together what you have shared, it is my undertsanding that your function read_wp2c is extracting values from your json file output_wp2camera.json, the contents of which you have copied and pasted above. Correct?
What I am saying is that, along the way, you try to define a variable by setting it equal to itself. Since it doesn't exist, you get an error. It doesn't matter that the variable is defined in the json file, You have to extract that data from the json file and load it into MATLAB.
A=A
Unrecognized function or variable 'A'.
You say your python code works? Or are you encountering issues there as well? If it works, perhaps you are using a python function that can read the contents of the json file?
Cris LaPierre
Cris LaPierre el 3 de Ag. de 2021
Editada: Cris LaPierre el 3 de Ag. de 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.
Thank you so much sir

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Preguntada:

el 31 de Jul. de 2021

Comentada:

el 3 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by